Java provides a Native Memory Tracking feature that enables you to monitor the memory usage of your Java application. This feature is particularly useful for identifying memory leaks and optimizing the memory usage of your application. In this post, we will discuss how to activate Native Memory Tracking for a Java jar application in Linux.

  • Step 1: Check Java Version

First, you need to check the version of Java installed on your system using the following command:

java --version

If your Java version is 8u92 or later, you can use Native Memory Tracking. If not, you need to upgrade your Java version

  • Step 2: Start the Java Application with Native Memory Tracking

To start your Java application with Native Memory Tracking, you need to add the following JVM option when you start the application:

-XX:NativeMemoryTracking=summary

This option enables Native Memory Tracking and specifies that only a summary report is generated. You can also specify “detail” instead of “summary” to generate a detailed report.

For example, if you want to start your Java jar application named “myapp.jar” with Native Memory Tracking, you can use the following command:

java -XX:NativeMemoryTracking=summary -jar myapp.jar
  • Step 3: View the Native Memory Tracking Report

After you start your Java application with Native Memory Tracking, you can view the report by using the following command:

jcmd <pid> VM.native_memory summary

NOTE: you can find your java PID by using ps command or jps eg:

[user@server usr]# jps
19664 Jps
14921 apps.jar

Further information related how to read the summary over here