Explain Codes LogoExplain Codes Logo

How can I find Java heap size and memory used (Linux)?

java
memory-management
jvm-parameters
garbage-collection
Anton ShumikhinbyAnton Shumikhin·Aug 24, 2024
TLDR

To instantly get the Java heap size and memory usage, switch to jcmd, jmap, and jstat, targeting <PID> of your Java process. For real-time info, jstat -gc reports data related to garbage collection.

jcmd <PID> GC.class_histogram # prints details of objects on Java heap jmap -heap <PID> # delivers heap configuration and usage summary jstat -gc <PID> # gives statistics related to GC activities

The jest behind the commands

The jcmd command with GC.class_histogram gives an overview of the memory usage of different object classes, while jmap coupled with -heap offers an elaborated representation of how heap memory is used by the Java process. With a variety of options, jstat paints a dynamic picture and provides runtime statistics pertaining to memory consumption in the Java HotSpot VM.

Spotting your Java process

Before plunging into memory magic, you have to identify the process ID (PID) of your Java process. The jps (Java Virtual Machine Process Status Tool) command comes in handy for that:

jps -lvm # this command will perform an Abracadabra and reveal all hidden processes

Some more magical commands

Multiple monitoring with jvmtop

If you want to be a wizard, you will love jvmtop. It lets you monitor multiple Java applications simultaneously, providing real-time memory usage and CPU usage. Oh and it spills the beans about garbage collection too!

jvmtop # top tip: don't tell everyone about this one though... keep it as your secret weapon

Heap settings

Here's how you can get those intriguing heap configuration details without even knowing the PID:

java -XX:+PrintFlagsFinal -version | grep HeapSize # Whee! Info without PID. Isn't it an Aladdin's lamp?

Visual monitoring with VisualVM

If all the command line stuff is playing tricks on you, and you prefer a modern wizard's tool, you need VisualVM. It offers heap memory visualizations, garbage collection metrics and much more through a fancy GUI.

For extra magical prowess

Conjuring a custom service

If you fancy a script-writing Potter challenge, you could create a custom monitoring service with either JMX or without it. You just need magic to peek into /proc/<PID>/ and keep drawing memory usage metrics continuously.

Checking Java Heap Size

In wizarding language, checking the heap size is like gauging how much potion you have and how many times you can call upon the genie:

🏋️💧: Potion & Flask Analysis $> jmap -heap <PID> # "Reveals-the-capital! Petrificus totalus!"

Monitoring Memory Usage

Just as a wizard must be aware of his spells and resources, likewise, we must keep a track of consumed and free memory:

🕹️📖: Resource Usage Tracking $> jstat -gc <PID> # "Scan-nus read-a-lot-icus!"

Encounter with Dementors

Heap space errors

Your encounter with Dementors is like your application throwing an OutOfMemoryError. Keep your jmap ready to generate a heap dump. Use the Eclipse Memory Analyzer to investigate the haunting memory consumption.

Finding the Golden Snitch

In case jps doesn't fetch the correct PID, you might have to play Quidditch using ps or grep to search the Java process in Linux.

VisualVM connection refused

If you're facing a Dementor's kiss with VisualVM, check and amend your firewall rules. Ensure they allow connections to the JMX port without any veil.

Muggle level references

  1. VisualVMYou don't need a wand. Only a tool to monitor and troubleshoot Java applications.
  2. Monitoring and Management Using JMX TechnologyHow to use JMX for advanced Java SE monitoring.
  3. Java Garbage Collection BasicsThe undying game with the Beater's Bat (Java GC) is explained here.
  4. jmap - Memory MapExcogitate jmap usage for heap memory exploration.
  5. Java SE 6 HotSpot[tm] Virtual Machine Garbage Collection TuningYour guide to fine-tuning the GC and winning the Seeker's challenge!.
  6. top(1) - Linux manual pageMemory monitoring in Linux explained for Muggles.
  7. Guide to the Most Important JVM ParametersGet the basic of JVM parameters and cackle like a victorious Sorcerer!.