Coder Perfect

How can I determine the size of a Java heap and the amount of memory used (on Linux)?

Problem

How can I use the command line to examine the heap size (and used memory) of a Java application on Linux?

I attempted using jmap. However, it provides information about internal memory places such as Eden, PermGen, and others, which is not valuable to me.

I’m looking for anything along the lines of:

That is all there is to it. I know I can see it in JConsole and other tools, but I need to do it from the command line. (I can’t use JMX, etc.)

Asked by Jasper

Solution #1

Each Java process has a unique pid, which you can discover with the jps command.

Once you have the pid, run jstat -gc [insert-pid-here] to see statistics on the garbage collected heap’s behavior.

Oracle’s jstat documentation can be found here.

Answered by farmer1992

Solution #2

This command displays the heap sizes in bytes that have been specified.

java -XX:+PrintFlagsFinal -version | grep HeapSize

It also works with Amazon AMI on EC2.

Answered by Atma

Solution #3

jvmtop is a command-line utility that displays numerous metrics in real time, including heap.

 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

Answered by MRalwasser

Solution #4

This method worked in both 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 of these commands looks like this:

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)

Divide the value by (1024*1024) to get the size in MB.

Answered by padippist

Solution #5

Without using JMX, which is what most tools use, all you can do is use

jps -lvm

and deduce that the settings will be determined by command line options

By default, you can’t retrieve dynamic data without JMX, but you could develop your own service to do so.

By the way, I like VisualVM to JConsole.

Answered by Peter Lawrey

Post is based on https://stackoverflow.com/questions/12797560/how-to-find-java-heap-size-and-memory-used-linux