Search code examples
javamemoryjvmjvm-hotspot

get real RAM usage of current JVM with Java


In HTOP you can see the RES value (resident size) which shows how much of your RAM your JVM process is really taking.

Now I want to get that value by only using pure Java. If you tell me it's just not possible, that's fine as well.

Let me explain all the attempts I made with an application that shows a RES value of 887M in htop:

  1. Combining committed HeapMemory and non Heapmemory via MemoryMXBean gives me 726M
  2. Adding up all commited memory of all ManagementFactory.getMemoryPoolMXBeans() gives me 737M
  3. runtime.totalMemory() gives 515M

Any ideas what else I could try?


Solution

  • Assuming you are running Linux, just read /proc/self/status file and look for VmRSS: line.

    Or parse /proc/self/smaps to get comprehensive memory information for the current process.