0

I want to get

  • free disk space
  • total disk space
  • total RAM
  • free RAM

using Java code. (df -h)

I have done several codes, but it does not match with the command terminal results.

1)

FileSystemView fsv = FileSystemView.getFileSystemView();

File[] drives = File.listRoots();
if (drives != null && drives.length > 0) {
    for (File aDrive : drives) {
        System.out.println("\tFree space: " + aDrive.getFreeSpace());
    }
}

2)

File file = new File("/");
double freeSize = file.getFreeSpace();

3) Also I use:

long freeMemory=Runtime.getRuntime().freeMemory();
long totalMemory = Runtime.getRuntime().totalMemory();
long maxMemory = Runtime.getRuntime().maxMemory();

Command terminal results:-
root@sumi:~# free -m
          total        used        free      shared  buff/cache available
Mem:       7896        5516        1121         279        1258      2110
Swap:      8108          36        8072                                                              

Java program results

totalmemory = 1029.177344;
maxmemory = 1908.932608;
freememory = 1013.071192;  

Please, help me to get the system's free disk space, total disk space, total RAM, and free RAM results to be same as the command terminal results using a Java program.

Little Helper
  • 1,870
  • 3
  • 12
  • 20
SumiSujith
  • 475
  • 5
  • 9
  • 2
    What is the result that you are getting and how does it differ from your expected result? – Angry Red Panda Feb 12 '18 at 08:15
  • 407G is the available memory shown in command terminal . but java program return 429.7GB. I need java code to get system's free disk space,total disk space,total RAM and free RAM. – SumiSujith Feb 12 '18 at 08:20
  • 1
    just to be sure, if you say `407G` do you mean `407GiB` or `407GB`? if so, you just need to convert your `429.7GB` to `GiB`. – Angry Red Panda Feb 12 '18 at 08:24
  • 2
    Java is not designed to provide *system specific* information. Especiall your approach to abuse the fact that in unix "everything is a file" has no guarantee of success. You migt better call `df` using the [ProcessBuilder](https://docs.oracle.com/javase/8/docs/api/java/lang/ProcessBuilder.html) and read its output: https://stackoverflow.com/questions/3643939/java-process-with-input-output-stream – Timothy Truckle Feb 12 '18 at 08:29
  • Using `file.getFreeSpace` works for me, but I would probably have to agree with Timothy Truckle, that reading `df` output would be the better choice. Otherwise you could try using `file.getUsableSpace();` instead of `file.getFreeSpace();`, since it will deliver a more accurate result. – Angry Red Panda Feb 12 '18 at 08:37
  • @AngryRedPanda i mean 407GB – SumiSujith Feb 12 '18 at 08:42
  • It should work in all operating system. I checked with file.getUsableSpace();It also doesnot give accurate results. Program results 1013.071192 MB and command terminal results 1121MB. – SumiSujith Feb 12 '18 at 08:54
  • 2
    I think this might be a MB versus MiB issue. – Stephen C Feb 12 '18 at 09:36
  • 1
    java.lang.Runtime provides information about JVM not OS. https://stackoverflow.com/questions/4335356/getting-os-memory-size-from-java – user158037 Feb 12 '18 at 09:42

0 Answers0