Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing class histogram programmatically

is there any way to print top used N classes on the current java application programmatically?

sample output: N=10

num   #instances    #bytes  class name
--------------------------------------
  1:        23     4723136  [I
  2:        19     4718928  [J
  3:        18     4718880  [D
  4:     73925     1774200  java.lang.String
  5:       208     1226400  [C
  6:        28     1205064  [B
  7:        18     1179936  [F
  8:        68      297040  [Ljava.lang.String;
  9:       332       14136  [Ljava.lang.Object;
 10:        32       10240  <objArrayKlassKlass>
like image 608
Trustin Avatar asked Sep 21 '10 12:09

Trustin


1 Answers

You could kick off jmap as part of your java wrapper script and run it continuously in a loop:

For example, if you are on Unix, you could do something like:

java MyMainClass ... &

pid=$!
while [ ! -z $pid ]
do
    jmap -histo $pid | head -13
    sleep 60

    #check pid
    kill -0 $pid > /dev/null 2>&1   
    if [ $? -ne 0 ]
    then
       pid=""
    fi  
done
like image 98
dogbane Avatar answered Nov 05 '22 13:11

dogbane