I am working on an application that has a health monitor page to give insights about current memory space used by the application, different threads open, and many other useful information.
Our users sometimes get puzzled by the difference in memory space shown by JVM (on this screen) and the one shown in Windows task manager tool. They suspect there is some memory leak and get jittery. I googled the problem and here is what I found out-
As this post points out, the memory of the JVM is roughly divided into 3 main areas:
1. Heap memory (for Java objects)
2. Non-heap memory (for meta data/ loaded classes)
3. Native Memory (for Native java code, dll etc)
Reasons for Inconsistencies:
1. The task manager displays the combination of above 3 (plus a lot more, we’ll come to that shortly), but the jconsole or the Java API shows only the heap memory.
2. OS reserves the memory for processes who demand more memory, even after that memory is freed from the proess. These memory pages are mapped as part of process’s address space. This is why the memory usage in task manager is not decreased in this case, sometimes causing a false alarm and panic among developers that there is some kind of memory leak.
3. The Task manager not just shows heap/ non-heap memory, but many other numbers determined by the OS. These can be checked in Windows Resource Monitor tools. (resmon.exe)
Understanding Memory in Task Manager
As described in in Tech Republic article and visible in Resmon.exe tool in Windows, a process’s memory footprint constitutes-
1. Committed memory (Virtual memory)
2. Working Set (Physical memory, divided into shareable and private memory. Heap is a small part of it)
This blog on Microsoft further breaks this usage down at granular level by using the tool called VMMap, and shows how to get more insight into memory usage pattern by a process.
1. jConsole or Java API shows a process’s Heap memory, but the OS’s handling of memory involves a lot more factors determined by it. That’s the reason OS will report significantly higher memory usage than the one reported by JVM.
2. For the purpose of better resource utilization, OS reserves the memory even though the actual process has freed the memory.
3. It’s a myth that more free memory in Task Manager means better performing system. In reality, it’s a wasted resource for the OS, that’s why Windows tries to use as much of available physical memory as possible for better performance and resource utilization. More memory shown in the task manager doesn’t indicate a resource leak, rather it means your processes have more space in RAM to execute any current or future requests.