How to find Java Heap Size and Memory Used (Linux)?
Solution 1
Each Java process has a pid
, which you first need to find with the jps
command.
Once you have the pid, you can use jstat -gc [insert-pid-here]
to find statistics of the behavior of the garbage collected heap.
jstat -gccapacity [insert-pid-here]
will present information about memory pool generation and space capabilities.jstat -gcutil [insert-pid-here]
will present the utilization of each generation as a percentage of its capacity. Useful to get an at a glance view of usage.
See jstat docs on Oracle's site.
Solution 2
This command shows the configured heap sizes in bytes.
java -XX:+PrintFlagsFinal -version | grep HeapSize
It works on Amazon AMI on EC2 as well.
Solution 3
jvmtop is a command-line tool which provides a live-view at several metrics, including heap.
Example output of the VM overview mode:
JvmTop 0.3 alpha (expect bugs) amd64 8 cpus, Linux 2.6.32-27, load avg 0.12
http://code.google.com/p/jvmtop
PID MAIN-CLASS HPCUR HPMAX NHCUR NHMAX CPU GC VM USERNAME #T DL
3370 rapperSimpleApp 165m 455m 109m 176m 0.12% 0.00% S6U37 web 21
11272 ver.resin.Resin [ERROR: Could not attach to VM]
27338 WatchdogManager 11m 28m 23m 130m 0.00% 0.00% S6U37 web 31
19187 m.jvmtop.JvmTop 20m 3544m 13m 130m 0.93% 0.47% S6U37 web 20
16733 artup.Bootstrap 159m 455m 166m 304m 0.12% 0.00% S6U37 web 46
Solution 4
Try this it worked in Ubuntu and RedHat:
java -XX:+PrintFlagsFinal -version | grep -iE 'HeapSize|PermSize|ThreadStackSize'
For Windows:
java -XX:+PrintFlagsFinal -version | findstr /i "HeapSize PermSize ThreadStackSize"
For Mac
java -XX:+PrintFlagsFinal -version | grep -iE 'heapsize|permsize|threadstacksize'
The output of all this commands resembles the output below:
uintx InitialHeapSize := 20655360 {product}
uintx MaxHeapSize := 331350016 {product}
uintx PermSize = 21757952 {pd product}
uintx MaxPermSize = 85983232 {pd product}
intx ThreadStackSize = 1024 {pd product}
java version "1.7.0_05"
Java(TM) SE Runtime Environment (build 1.7.0_05-b05)
Java HotSpot(TM) 64-Bit Server VM (build 23.1-b03, mixed mode)
To find the size in MB, divide the value with (1024*1024).
Solution 5
Without using JMX, which is what most tools use, all you can do is use
jps -lvm
and infer that the settings will be from the command line options.
You can't get dynamic information without JMX by default but you could write your own service to do this.
BTW: I prefer to use VisualVM rather than JConsole.
Related videos on Youtube
Jasper
Areas of interest and skill: Java / J2EE / MongoDB / Big Data/ Hadoop / Machine Learning / Cloud / Amazon EC2
Updated on July 17, 2022Comments
-
Jasper almost 2 years
How can I check Heap Size (and Used Memory) of a Java Application on Linux through the command line?
I have tried through jmap. But it gives info. about internal memory areas like Eden/ PermGen etc., which is not useful to me.
I am looking for something like:
- Max Memory: 1GB
- Min Memory: 256 MB
- Heap Memory: 700 MB
- Used Memory: 460 MB
That's all. I know that I can see this in JConsole etc., but I need to do it via command-line. (can't enable JMX etc.)
-
basZero over 10 yearsIs there a recommendation which options of
jstat
one should use in order to verify just the overall memory usage of a JVM? Let's say you start the JVM withXms=4g
andXmx=4g
and you want to see, how much memory of that is already used? -
Kerem about 9 years"jstat -gcutil <pid> 250 N" was very useful to take N samples with 250ms intervals and display the output as percentages for the corresponding spaces. Thanks.
-
oski86 about 9 yearsIt is indeed a great tool, kind of htop but with metrics from jstat. Thanks for suggestion, @MRalwasser .
-
patryk.beza almost 9 yearsWorth noting quote from
jstat
Oracle Java 8 manual page:This command is experimental and unsupported
. -
Madbreaks over 7 yearsThis doesn't answer the question, which specifically asks how to check heap usage of a process. The command here lists JVM defaults across all processes.
-
Gary Gauh over 7 yearsHow to find the memory usage separated by heap, permsize,... of specific java process by pid ?
-
padippist over 7 years@GaryGauh This is the default heap size. To find usage of running application you should do it within code or you can use jconsole. This is what I know there should also be many other ways.
-
cybersoft over 7 years
awk 'print {$3+$4+$6+$8}'
can print summarized usage on Java 8's jstat columns -
tmanolatos over 6 yearsJust to note that jvm-mon runs only for Java8
-
xdhmoore over 6 yearsHad trouble with the other answers, but a basic
ps -ef | grep java
showed me the vm args, which in my case included the -Xmx value, which was all I needed. -
Chris over 6 yearsdoes not seem to work out of the box on a SUSE Linux (line 38: declare: -A: invalid option)
-
amarjeetAnand over 6 yearssounds like you get error in associative array declaration which need bash >= 4. Also another problem may be due to running the script as "sh jpsstat.sh" . If so, try to run the script as "./jpsstat.sh".
-
linehrr over 6 yearstop command is showing how much OS is given to the JVM. this guys is asking how we can see heap space usage inside JVM. JVM is using 10g does not mean the real heap space is full of 10g data, because jvm almost never return memory back to OS from heap until you kill the process.
-
Johan over 6 yearsYet, it is a very helpful answer for me coming to this page via google search on how to find the global heap size.
-
Micha Wiedenmann almost 6 yearsUse
jstat -gc <vmid>
for running applications. -
Pavel Molchanov about 5 yearsYou need to correct your comment. GC.heap_info is available in Java 9 and above. It's not available in the Java 8. See another thread here: stackoverflow.com/questions/41891127/…
-
vaibhav gupta about 5 years@PavelMolchanov I am able to use the command in jdk1.8.0_172. /Library/Java/JavaVirtualMachines/jdk1.8.0_172.jdk/Contents/Home/bin/jcmd 98270 GC.heap_info. Please if you may, Add the info to referred thread as well as I don't have enough reputation as of now to add a comment there.
-
Pavel Molchanov about 5 yearsDo you use Mac? Do you use Oracle JDK? I don't know how it can be available in your jdk1.8.0_172, Oracle documented this feature only in Java 9 and up: docs.oracle.com/javase/9/tools/jcmd.htm. It's not in the Oracle JDK documentation for Java 8. It's not mentioned in the link that you gave at the bottom: docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/…
-
Pavel Molchanov about 5 yearsOne more question. Please check JDK version that is running process 98270 in your example. jcmd gets available commands from JVM of the process (in your case 98270). If process 98270 process is executed with different JDK (JDK 9 or above), you will see GC.heap_info command available even in JCMD itself is from Java 8. Available commands may be different for different processes. To get available commands execute: jcmp <PID> help.
-
vaibhav gupta about 5 yearsYes, I am using Mac. (Can you let me know, how it may make difference). Yes, I think i am using oracle jdk (can you help me know, what all others are available and how can i verify). Process is using jdk1.8.0_172 (verified on multiple java processes). I have these 5 extra commands than are documented in my referred link, on jcmd <PID> help -> VM.classloader_stats, GC.rotate_log, GC.finalizer_info, GC.heap_info, VM.dynlibs
-
linehrr about 5 years@cybersoft should be
awk '{print $3+$4+$6+$8}'
-
linehrr about 5 yearsand also, it should be
awk '{print $3+$9}'
aka NGC + OGC, if we don't consider perm space. -
arberg over 4 yearsor
awk 'NR>1 {print $3+$4+$6+$8}'
to skip first line with column titles -
Per Lundberg over 4 yearsFWIW,
GC.heap_info
is definitely available in OpenJDK 8 also. Maybe only in recent versions? I am using this one: 8u191-b12-2ubuntu0.18.04.1 -
Madbreaks over 4 years@jumping_monkey not indirect, incorrect. If what you're saying is true, the answer should be edited or you should feel free to add a new answer.
-
Andrejs almost 4 years^ There's a new version that now also supports Java 11.
-
Vladimir Filipchenko over 3 yearsThanks! Simple and just amazing tool for fast troubleshooting. Must have!
-
cdalxndr over 3 yearsonly works until java 8 because of tools.jar dependency
-
Suryaprakash Pisay about 3 years@amarjeetAnand, what is the default units of currHead and maxHeap? Is there any way to get these units in mb or user defined unit?
-
devildelta about 3 yearsInteresting thing is I checked there is
GC.heap_info
in 1.8.0_101-b13 but not in 1.8.0_144-b01. -
John Haberstroh almost 3 yearsThe initial question's title is ambiguous enough that this should remain a listed answer. Furthermore, it is the most findable answer on google to answer the configuration question.
-
Ronan over 2 yearsPerfect answer to the original question, which isn't to just display the max allocated heap size, but also the currently used heap. Not shown on the screenshot, but the jmap heap command displays free and used space in absolute and percentage values. You may have to use jhsdb rather than jmap directly : jhsdb jmap --heap --pid <your PID>