翔子 发表于 2014-11-19 09:24:52

How to setup VisualVM and jstatd to take heap dump and thread dump in linux

How to setup JVisualVM

jvisualvm comes installed with JDK and is available in bin directory. File name is jvisualvm.exe
How to setup jstatd in linux

In order to do remote profiling, jstatd must be running on remote host. jstatd setup needs to complete in 6 steps

    Create a jstatd.sh file at any path

    #!/bin/sh
    policy=${HOME}/jstatd.all.policy
    [ -r ${policy} ] || cat >${policy} <<'POLICY'
    grant codebase "file:/usr/lib/jvm/jre-1.6.0-openjdk.x86_64/lib/tools.jar" {
      permission java.security.AllPermission;
    };
    POLICY
    echo ${policy}
    jstatd -J-Djava.security.policy=${policy} -J-Djava.rmi.server.hostname=192.169.31.224&

    Replace 192.169.31.224 with the IP address of the remote host
    Replace "/usr/lib/jvm/jre-1.6.0-openjdk.x86_64/lib/tools.jar" with the actual path of tools.jar
    To find actual path of tools.jar, if you have multiple jvm installed in linux, run following commands
      Check Java using following command

      # /usr/sbin/alternatives --config java

      There are 2 programs which provide 'java'.

          Selection    Command
      -----------------------------------------------
         1         /usr/lib/jvm/jre-1.4.2-gcj/bin/java
      *+ 2         /usr/lib/jvm/jre-1.6.0-openjdk.x86_64/bin/java

      Enter to keep the current selection[+], or type selection number:

      Here current JVM path is "/usr/lib/jvm/jre-1.6.0-openjdk.x86_64/bin/java" so enter this path in jstatd.sh
      Check the path to tools.jar. Here tools.jar is available at multiple locations

      # locate tools.jar
      /usr/java/jdk1.7.0_45/db/lib/derbytools.jar
      /usr/java/jdk1.7.0_45/lib/tools.jar
      /usr/java/jdk1.7.0_45/lib/visualvm/visualvm/modules/com-sun-tools-visualvm-tools.jar
      /usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/lib/tools.jar
      /usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/lib/tools.jar

      Use chmod 777 -R /usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/lib
      To find running jstatd use following commands:-

      # which jstatd
      /usr/bin/jstatd
      # ls -lart /usr/bin/jstatd
      lrwxrwxrwx 1 root root 24 Oct 162010 /usr/bin/jstatd -> /etc/alternatives/jstatd
      # ll /etc/alternatives/jstatd
      lrwxrwxrwx 1 root root 49 Oct 152010 /etc/alternatives/jstatd -> /usr/lib/jvm/java-1.6.0-openjdk.x86_64/bin/jstatd

    use chmod a+x jstatd.sh to make it executable
    Finally run ./jstatd.sh

How to setup jstatd in windows

    Run the "jstatd" command in a command window:

    C:\Program Files\Java\jdk1.7.0_45\bin>jstatd.exe
    Could not create remote object
    access denied ("java.util.PropertyPermission" "java.rmi.server.ignoreSubClasses"
   "write")
    java.security.AccessControlException: access denied ("java.util.PropertyPermissi
    on" "java.rmi.server.ignoreSubClasses" "write")
            at java.security.AccessControlContext.checkPermission(AccessControlConte
    xt.java:372)
            at java.security.AccessController.checkPermission(AccessController.java:
    559)
            at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
            at java.lang.System.setProperty(System.java:783)
            at sun.tools.jstatd.Jstatd.main(Jstatd.java:139)

    The "access denied" error is expected, because "jstatd" requires a security policy file specified with the "java.security.policy" system property, if there is no security manager running on my machine.
    Create the security policy file, jstatd.all.policy, that grants permissions to run "jstatd" and other tools in the tools.jar:

    grant codebase "file:C:/Program Files/Java/jdk1.7.0_45/lib/tools.jar" {
       permission java.security.AllPermission;
    };

    Run "jstatd" with the security policy file, tools.policy specified to the "java.security.policy" system property:

    C:\Program Files\Java\jdk1.7.0_45\bin>jstatd.exe -J-Djava.rmi.server.hostname=127.0.0.1 -J-Djava.security.policy=jstatd.all.policy

    jVisualVM will autodetect all running java processes

    File:Jstatd-local.png

How to setup and use jhat
How to check time taken during full GC

    Setup following JCATALINA_OPTS to enable full GC in setenv.sh

    export CATALINA_OPTS="$CATALINA_OPTS -verbose:gc -XX:+HeapDumpOnOutOfMemoryError-XX:+PrintTenuringDistribution -XX:+PrintGCTimeStamps
    -XX:+PrintGCDetails -XX:+PrintGCApplicationStoppedTime -XX:+PrintGCApplicationConcurrentTime -XX:+PrintHeapAtGC -Xloggc:logs/gc.log"

    Use following tail command tail -f logs/gc.log | grep 'threads were stopped'
页: [1]
查看完整版本: How to setup VisualVM and jstatd to take heap dump and thread dump in linux