Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to monitor free memory ( including buffers and cache) in java?

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.

like image 969
bbaja42 Avatar asked Jan 10 '12 20:01

bbaja42


People also ask

What is buffer and cache in free command?

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.

What is RAM cache and buffer?

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.


3 Answers

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.

like image 152
rob Avatar answered Oct 19 '22 03:10

rob


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

like image 23
Bob Kuhar Avatar answered Oct 19 '22 03:10

Bob Kuhar


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...

like image 37
Michal Karm Babacek Avatar answered Oct 19 '22 01:10

Michal Karm Babacek