javamemoryoperating-systemjvm

Native memory consumed by JVM vs java process total memory usage


I have a tiny java console application which I would like to optimize in terms of memory usage. It is being run with Xmx set to only 64MB. The overall memory usage of the process according to different monitoring tools (htop, ps, pmap, Dynatrace) shows values above 250MB. I run it mostly on Ubuntu 18 (tested on other OS-es as well).

I've used -XX:NativeMemoryTracking java param and Native Memory Tracking with jcmd to find out why so much more memory is used outside of the heap.

The values displayed by NMT when summarized were more or less the same as the ones shown by htop as Resident Memory.

NMT:

Total: reserved=1518873KB, committed=255877KB

htop:

enter image description here

I've used several JVM parameters to reduce native memory consumption (reduced stack size, changed GC to serial, Class Data Sharing etc). Both reserved and committed memory metrics went down according to NMT (both malloced and mmaped) by let's say ~50MB in total.

NMT:

Total: reserved=1475110KB, committed=209218KB

All the tools that I'm using (htop, ps, pmap, Dynatrace) show no difference at all. Total memory used by the process is still 250MB.

  1. The question is, why is that? Why reducing native memory usage by JVM does not make any effect on the resident memory used by the java process? Is it reserved somehow upfront and not released?
  2. Is there any other way to effectively reduce memory consumption by the whole java process (outside of the heap which is already optimized and set to only 64MB)?

Solution

  • NativeMemoryTracking may report less committed memory than the actual Resident Set Size (RSS) of a process for multiple reasons.

    This answer and this video may give an idea what else takes memory, and how to analyze footprint of a Java process.

    This post describes some ideas (both reasonable and extreme) on reducing footprint.