How can I find Java heap size and memory used (Linux)?
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.
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:
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!
Heap settings
Here's how you can get those intriguing heap configuration details without even knowing the PID:
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:
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:
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
- VisualVM — You don't need a wand. Only a tool to monitor and troubleshoot Java applications.
- Monitoring and Management Using JMX Technology — How to use JMX for advanced Java SE monitoring.
- Java Garbage Collection Basics — The undying game with the Beater's Bat (Java GC) is explained here.
- jmap - Memory Map — Excogitate jmap usage for heap memory exploration.
- Java SE 6 HotSpot[tm] Virtual Machine Garbage Collection Tuning — Your guide to fine-tuning the GC and winning the Seeker's challenge!.
- top(1) - Linux manual page — Memory monitoring in Linux explained for Muggles.
- Guide to the Most Important JVM Parameters — Get the basic of JVM parameters and cackle like a victorious Sorcerer!.
Was this article helpful?