Java线程监控

第一步,找出Java进程PID:

ps -ef | grep java | grep -v “grep” | awk ‘{print $2}’

 第二步,找出最耗CPU的线程:

ps -mp pid -o THREAD,tid,time | sort -rn -k9

第三步,将tid转换为16进制:

printf “%x\n” tid

第四步,dumpJVM的堆栈信息:

jstack pid

jstack中的nid对应于前面的16进制tid,由此找到最耗cpu的线程代码。

使用top命令获取线程

top -Hp pid

 

附上一段完整脚本:http://blog.91gaoqing.com/archives/253.html

#!/bin/sh

export LANG="zh_CN.UTF-8";
export LC_ALL="zh_CN.UTF-8";

LOG_FILE="/tmp/jmonitor.log";
JSTACK_FILE="/tmp/jstack.log";

PID="$1";
shift;
i=0;
j="$1";
if [ -z "${j}" ]; then
    j=5;
fi

ps -mp ${PID} -o THREAD,tid,time | sort -rn > ${LOG_FILE};
jstack ${PID} > ${JSTACK_FILE};

for LINE in `cat ${LOG_FILE}|gawk -F '-' '{print $4}'|gawk -F ' ' '{print $1}'`
do
    i=$(($i+1));
    if (($i>$j)); then
        break;
    fi;
    XPID=`printf "%x\n" ${LINE}`;
    echo -ne "\033[32m";
    echo ${XPID};
    echo -e "\033[34m";
    grep -A 10 "0x${XPID}" ${JSTACK_FILE};
    echo -e "\e[0m";
done;

 

posted on 2014-09-26 11:51  万木春  阅读(602)  评论(0编辑  收藏  举报

导航