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.