1. Overview

In this quick tutorial, we’re going to get familiar with a few different ways to get the heap size of a running Java application.

2. jcmd

**To find the heap and metaspace related info of a running Java application, we can use the **jcmdcommand-line utility:

jcmd  GC.heap_info

First, let’s find the process id of a particular Java application using the jpscommand:

$ jps -l73170 org.jetbrains.idea.maven.server.RemoteMavenServer364309  quarkus.jar12070 sun.tools.jps.Jps

As shown above, the process id for our Quarkus application is 4309. Now that we have the process id, let’s see the heap info:

$ jcmd 4309 GC.heap_info4309: garbage-first heap   total 206848K, used 43061K  region size 1024K, 43 young (44032K), 3 survivors (3072K) Metaspace       used 12983K, capacity 13724K, committed 13824K, reserved 1060864K  class space    used 1599K, capacity 1740K, committed 1792K, reserved 1048576K

This app is using the G1 or garbage-first GC algorithm:

  • The first line reports the current heap size as 202 MB (206848 K) – also, 42 MB (43061 K) is being used
  • G1 regions are 1 MB, there are 43 regions marked as young, and 3 as survivors space
  • The current capacity of the metaspace is around 13.5 MB (13724 K). From that 13.5 MB, around 12.5 MB (12983 K) is used. Also, we can have up to 1 GB of metaspace (1048576 K). Moreover, 13842 KB guaranteed to be available for use by the Java virtual machine, also known as committed memory
  • The last line shows how much of the metaspace is used to store class information

#java #jvm

Command-Line Tools to Find the Java Heap Size
4.95 GEEK