Skip to content

How to find Java Heap Size and Memory Used (Linux)?

To find the Java heap size and the amount of memory used on a Linux system, you can use the jstat command and specify the process ID (PID) of the Java process.

Here is an example of how to do this:

bash
jstat -gc <PID>

This will display the Java heap size and the amount of memory used, as well as other information about the garbage collector. The output includes columns for Eden space (EC), Survivor spaces (S0C, S1C), Old generation (OC), and Metaspace (MC), along with garbage collection counts (YGC, FGC).

Alternatively, you can use the jmap command to get a detailed memory map of the Java process. Here is an example:

bash
jmap -heap <PID>

This will display the size and usage of the different memory regions in the Java heap, as well as the number of objects and the amount of memory used in each region. Note that jmap is deprecated in JDK 9+; use jcmd <PID> GC.heap_info as the modern alternative.

You can find the PID of the Java process by using the ps command and looking for the process that is running the Java executable. For example:

bash
ps aux | grep java

This will list all processes that have "java" in their command line, along with the PID of each process.

You can also use the top command to find the PID of the Java process. The top command will display a list of all running processes, along with the PID, the CPU and memory usage, and the command line of each process. You can use the top command to find the Java process and its PID.

To get the total Java heap size, you can use the -XX:+PrintFlagsFinal option with the java command and search for the MaxHeapSize flag. For example:

bash
java -XX:+PrintFlagsFinal -version | grep MaxHeapSize

This will print the value of the MaxHeapSize flag, which is the maximum heap size that can be used by the Java process.

Keep in mind that jstat and jmap (or jcmd) attach to running JVMs and do not require the process to be started with them. They will work as long as the JVM is running and you have the necessary permissions to access the process.

Dual-run preview — compare with live Symfony routes.