I've already searched for possible solutions, such as How to monitor the computer's cpu, memory, and disk usage in Java?. But memory spent in buffers and cache is not mentioned anywhere?
For example, on linux, output of free -m:
total used free shared buffers cached Mem: 2011 1835 175 0 309 847 -/+ buffers/cache: 678 1333 Swap: 1998 0 1998
I've already tested output of sigar, which reports free memory as 175 MB.
The cause of this question is how to detect when OS has little memory left? In the previous output of free -m
, 175 MB seems quite low; but by adding buffers and cache it is evident that free memory is actually 1333 MB out of 2 GB.
To summarise, using java, is it possible to get value 1333 MB as value of the free memory? As I've already mentioned, so far I know only how to get value of 175 MB.
shared: Memory specifically allocated for use by multiple processes. buffers: Temporary memory that is set aside to help some processes. cache: Memory that is available and 'borrowed' by the operating system to help speed up many linux OS operations.
Buffer is an area of memory used to temporarily store data while it's being moved from one place to another. Cache is a temporary storage area used to store frequently accessed data for rapid access.
If you are trying to get memory statistics for the OS itself (not the JVM), you can use JNA or JNI to make the platform-specific native API calls directly to the OS.
I don't know about free memory directly, but the JVM maintains JMX beans that, I believe, have this information and more: http://docs.oracle.com/javase/1.5.0/docs/guide/management/jconsole.html
I am convinced it is not possible to use Java API for obtaining cahce/buffers number. Even the guys from SIGAR use JNI wrappers and native system calls...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With