Friday, June 27, 2014

How to check JAVA memory usage

Many commands can check the memory utilization of JAVA processes, for example, pmap, ps, jmap, jstat.
What are the differences?
Before we move forward, we need to know heap and stack.
Simply saying, heap space contains 1 Eden space , 2 young generations and 1 old generation; stack space is the permanent generation.
Here is one great article about <Understanding Java Garbage Collection>.

Take "ResourceManager" process for example:
[root@admin ~]# jps
2304 ResourceManager

1. ps and pmap can show total reserved memory from OS.

[root@admin ~]# ps auwx|egrep "MEM|2304"|grep -v grep
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
yarn      2304  0.8  2.3 826132 192484 ?       Sl   08:54   4:02 /usr/java/jdk1.7.0_45-cloudera/bin/java -Dproc_resourcemanager
[root@admin ~]# pmap -x 2304
2304:   /usr/java/jdk1.7.0_45-cloudera/bin/java -Dproc_resourcemanager
Address           Kbytes     RSS   Dirty Mode   Mapping
0000000000400000       4       4       0 r-x--  java
0000000000600000       4       4       4 rw---  java
(......)
----------------  ------  ------  ------
total kB          826132  192404  172608
"ps auwx|grep <pid>" shows totally 800MB+ virtual memory(including 190MB physical memory) are reserved.
"pmap -x <pid>" shows the same.
Because reserved memory includes shared memory also, it is hard for you to know how much heap memory are used and allocated.

2. jmap and jstat can show used space of heap&stack.

[root@admin ~]#jmap -heap 2304|egrep ":|used     ="
Attaching to process ID 2304, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 24.45-b08
Heap Configuration:
Heap Usage:
New Generation (Eden + 1 Survivor Space):
   used     = 44627176 (42.559791564941406MB)
Eden Space:
   used     = 44264776 (42.21417999267578MB)
From Space:
   used     = 362400 (0.345611572265625MB)
To Space:
   used     = 0 (0.0MB)
concurrent mark-sweep generation:
   used     = 8410104 (8.020500183105469MB)
Perm Generation:
   used     = 32389728 (30.889251708984375MB)
[root@admin ~]# jstat -gc 2304
 S0C    S1C    S0U    S1U      EC       EU        OC         OU       PC     PU    YGC     YGCT    FGC    FGCT     GCT
8064.0 8064.0 353.9   0.0   65088.0  43227.3   163520.0    8213.0   31808.0 31630.6     32    0.254   0      0.000    0.254
To understand "jstat" output, please check article <Monitor Java Garbage Collection using jstat> firstly.
Here we can see the mapping relationship between outputs from jmap and jstat.

New Generation(used memory) = S0U+S1U+EU
concurrent mark-sweep generation(used memory) = OU
Perm Generation(used memory)=PU.

So the total used heap+stack memory size matches between "jmap" and "jstat":
>>> (44627176+8410104+32389728)/1024.0/1024.0
81.46954345703125
>>> (353.9+0.0+43227.3+8213.0+31630.6)/1024.0
81.469531250000003

3. jmap -histo can show top heap memory objects

This is helpful to troubleshoot heap memory leaking.
[root@admin ~]# jmap -histo -F 2304 |more
Attaching to process ID 2304, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 24.45-b08
Iterating over heap. This may take a while...
Object Histogram:

num    #instances #bytes Class description
--------------------------------------------------------------------------
1:  59262 7949504 * ConstMethodKlass
2:  59262 7595200 * MethodKlass
(......)
2721:  1 16 com.sun.research.ws.wadl.Doc$JaxbAccessorF_title
Total :  440785 44864280

27 comments:

  1. Thanks!
    Why "ps" shows 800M+ and jstat/jmap only 80M? Was it measured for different processes?

    ReplyDelete
    Replies
    1. 800M+ is for virtual memory, RSS is for physical memory, jmap only dumps heap memory which could be smaller than physical memory usage for that process.

      Delete
  2. How to check
    runtime usage memory in heap

    ReplyDelete
  3. Thus, if one keeps the few above stated key points in mind, he or she for sure is going to hire one of the best web portal development companies available.. web portal implementation A team of tech experts must be working round-the-clock to ensure the web portal is in fine shape.

    ReplyDelete
  4. You’ve captured the essence of the subject beautifully. It’s well-written and engaging. Amazing work!
    quickest way to sell a house Atlantic Beach FL

    ReplyDelete
  5. Very informative article. Thanks for providing such important and necessary information. Appreciate the work you're doing.
    chip guidelines

    ReplyDelete
  6. Wonderful information! Thanks so much for sharing such valuable content with us. Your blog always provides the best and most interesting insights on important topics.
    popular kid book series about investing

    ReplyDelete

Popular Posts