14

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.

Community
  • 1
  • 1
bbaja42
  • 2,099
  • 18
  • 34
  • 2
    Since the answer is likely to be OS-specific, you might want to specify which OS(es) you need to support. Just Linux? – NPE Jan 10 '12 at 20:49
  • 1
    Well, target is linux, but I'd prefer if it is a cross-platform solution. I don't have windows machine at hand to check, so I don't know do windows also report cached/buffered memory separately. Windows experts are welcome :D – bbaja42 Jan 10 '12 at 20:58
  • From what I gathered [here](http://www.codinghorror.com/blog/2006/09/why-does-vista-use-all-my-memory.html), based on picture showing windows task manager, Windows also have separation of total memory, free memory and cached memory. – bbaja42 Jan 10 '12 at 22:13

3 Answers3

1

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

Bob Kuhar
  • 10,838
  • 11
  • 62
  • 115
  • 1
    As shown [here](http://docs.oracle.com/javase/1.5.0/docs/guide/management/jconsole.html#summary), JMX can give following info : size of memory taken by JVM, total memory in OS, and free memory (without buffers and cache). All good. But still no mention of memory spent on buffers and cache. Cached memory can be quickly reclaimed if needed. – bbaja42 Jan 10 '12 at 21:03
  • If you are trying to get fine grained details of memory spent on particular classes, packages, etc., I think you are actually in need of a Profiler and not just a JMX monitor. Unless the classes you are interested in have their own MBeans providing this stuff to JMX. – Bob Kuhar Jan 10 '12 at 21:09
  • 1
    It seems we are straying a bit of course. I don't need any particular memory spent on java class or package. Non java way, using system command (`free`), I can measure free memory for whole OS, such measurement's show both free memory with and without buffers/cache. I'm wondering how to do it with java. – bbaja42 Jan 10 '12 at 21:13
  • oh, sorry. I don't know how you get detailed information on a running JVM in terms of the free command on linux. Details within the JVM, though, JMX and Profilers are your friend. Good luck. – Bob Kuhar Jan 11 '12 at 05:55
1

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.

rob
  • 6,147
  • 2
  • 37
  • 56
1

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