1

I try to monitor the total memory allocated to my Java application (OpenJDK 11.0.4) by the OS (Linux Ubuntu 19.04).

I take two approches:

  • Using the VmRSS size from ps or cat /proc/<pid>/status | grep VmRSS
  • Using Java NativeMemoryTracking functionality with the following properties: -XX:NativeMemoryTracking=summary -XX:+UnlockDiagnosticVMOptions -XX:+PrintNMTStatistics and using the Total committed size.

The numbers retrieved from both approaches are always different (sometimes of a few megabytes ... sometimes a lot more).

What I understand is that the VmRSS size reported by the OS is the real memory used by the process but why does Java NTM reports a different value?

If I want to be able to monitor the Resident Set Size of a Java application, should I rely on the OS reported size (VmRSS) of can I use something inside the JVM (like NTM) ? Ideally I want to be able to monitor the RSS size of my Java application from itself ...

loicmathieu
  • 5,181
  • 26
  • 31

1 Answers1

0

It's not a surprise that NMT and RSS numbers are different -- the JVM and the OS use different approaches to measure different things.

NMT can report much less memory than the actual usage, or it can also report more memory than the process consumes from the OS perspective. I explained the reasons in this answer.

Monitoring RSS inside a Java application is simple. You already know about /proc/<pid>/status, so why not just read /proc/self/status in Java code? (Linux automatically maps self to the current process ID). It's even easier to parse /proc/self/stat, since all the information is provided there in a single line in a predefined format.

It's also possible to get NMT report from within Java application as described in this answer.

apangin
  • 92,924
  • 10
  • 193
  • 247