How to check heap usage of a running JVM from the command line?

前端 未结 4 1301
忘了有多久
忘了有多久 2020-12-12 21:34

Can I check heap usage of a running JVM from the commandline, I mean the actual usage rather than the max amount allocated with Xmx.

I need it to be commandline beca

4条回答
  •  半阙折子戏
    2020-12-12 21:53

    All procedure at once. Based on @Till Schäfer answer.

    In KB...

    jstat -gc $(ps axf | egrep -i "*/bin/java *" | egrep -v grep | awk '{print $1}') | tail -n 1 | awk '{split($0,a," "); sum=a[3]+a[4]+a[6]+a[8]+a[10]+a[12]; print sum}'
    

    In MB...

    jstat -gc $(ps axf | egrep -i "*/bin/java *" | egrep -v grep | awk '{print $1}') | tail -n 1 | awk '{split($0,a," "); sum=(a[3]+a[4]+a[6]+a[8]+a[10]+a[12])/1024; print sum" MB"}'
    

    "Awk sum" reference:

     a[1] - S0C
     a[2] - S1C
     a[3] - S0U
     a[4] - S1U
     a[5] - EC
     a[6] - EU
     a[7] - OC
     a[8] - OU
     a[9] - PC
    a[10] - PU
    a[11] - YGC
    a[12] - YGCT
    a[13] - FGC
    a[14] - FGCT
    a[15] - GCT
    

    Thanks!

    NOTE: Works to OpenJDK!

    FURTHER QUESTION: Wrong information?

    If you check memory usage with the ps command, you will see that the java process consumes much more...

    ps -eo size,pid,user,command --sort -size | awk '{ hr=$1/1024 ; printf("%13.2f Mb ",hr) } { for ( x=4 ; x<=NF ; x++ ) { printf("%s ",$x) } print "" }' |cut -d "" -f2 | cut -d "-" -f1
    

提交回复
热议问题