{"id":3608,"date":"2018-12-21T18:43:22","date_gmt":"2018-12-21T10:43:22","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=3608"},"modified":"2018-12-21T18:43:22","modified_gmt":"2018-12-21T10:43:22","slug":"%e8%af%a6%e8%a7%a3-jstack-%e5%91%bd%e4%bb%a4%e7%9a%84%e7%94%a8%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=3608","title":{"rendered":"\u8be6\u89e3 jstack \u547d\u4ee4\u7684\u7528\u6cd5"},"content":{"rendered":"<p>\n\t\u5927\u5bb6\u6700\u8fd1\u53ef\u80fd\u4f1a\u53d1\u73b0\u6211\u5199\u7684\u6587\u7ae0\u90fd\u5f88\u5957\u8def\uff0c\u4e00\u4e2a\u7cfb\u5217\u4e00\u4e2a\u7cfb\u5217\u7684\u3002\u524d\u9762\u5199\u4e86\u4e00\u7cfb\u5217\u7684\u6570\u636e\u7ed3\u6784\uff0c\u6700\u8fd1\u6211\u4f1a\u5f00\u59cb\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u4e00\u4e9b Java \u4e2d\u7684\u4e00\u4e9b\u81ea\u5e26\u547d\u4ee4\u3001\u5de5\u5177\u7684\u4f7f\u7528\u3002\u5982\u679c\u4f60\u957f\u671f\u7684\u5173\u6ce8\u548c\u9605\u8bfb\uff0c\u6211\u76f8\u4fe1\u4e0d\u5230\u4e00\u5e74\u4f60\u5c31\u662f\u4e00\u4e2a\u9ad8\u624b\u3002\u81f3\u5c11\u4f60\u548c\u9ad8\u624b\u5728\u4e00\u8d77\u540c\u884c\uff0c\u9762\u8bd5\u7edd\u5bf9\u4e0d\u4f1a\u6709\u95ee\u9898\u3002\n<\/p>\n<p>\n\t\u4eca\u5929\u6211\u4eec\u7ee7\u7eed\u6765\u5b66\u4e60\u4e00\u4e2a java \u81ea\u5e26\u7684\u547d\u4ee4 jstack\u3002\n<\/p>\n<p>\n\tjstack \u547d\u4ee4\u975e\u5e38\u7684\u7b80\u5355\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 jstack -h \u6216\u8005 jstack -help \u547d\u4ee4\u67e5\u770b\u5b83\u7684\u7528\u6cd5\u8be6\u60c5\u3002\n<\/p>\n<pre class=\"brush:bash;\">\r\njstack -help\r\nUsage:\r\n    jstack [-l] &lt;pid&gt;\r\n        (to connect to running process)\r\n    jstack -F [-m] [-l] &lt;pid&gt;\r\n        (to connect to a hung process)\r\n    jstack [-m] [-l] &lt;executable&gt; &lt;core&gt;\r\n        (to connect to a core file)\r\n    jstack [-m] [-l] [server_id@]&lt;remote server IP or hostname&gt;\r\n        (to connect to a remote debug server)\r\n\r\nOptions:\r\n    -F  to force a thread dump. Use when jstack &lt;pid&gt; does not respond (process is hung)\r\n    -m  to print both java and native frames (mixed mode)\r\n    -l  long listing. Prints additional information about locks\r\n    -h or -help to print this help message<\/pre>\n<p>\n\t\u4ece\u4e0a\u9762\u53ef\u4ee5\u770b\u51fa jstack \u547d\u4ee4\u4e3b\u8981\u6709 4 \u4e2a\u53c2\u6570\uff1a\n<\/p>\n<ul>\n<li>\n\t\t-F \u5f53\u8fdb\u7a0b\u6302\u8d77\uff0c\u6267\u884cjstack &lt;pid&gt; \u547d\u4ee4\u6ca1\u6709\u4efb\u4f55\u8f93\u51fa\u540e\uff0c\u5c06\u5f3a\u5236\u8f6c\u50a8\u5806\u5185\u7684\u7ebf\u7a0b\u4fe1\u606f\n\t<\/li>\n<li>\n\t\t-m \u5728\u6df7\u5408\u6a21\u5f0f\u4e0b\uff0c\u6253\u5370 java \u548c native c\/c++ \u6846\u67b6\u7684\u6240\u6709\u6808\u4fe1\u606f\n\t<\/li>\n<li>\n\t\t-l \u957f\u5217\u8868\u3002\u6253\u5370\u5173\u4e8e\u9501\u7684\u9644\u52a0\u4fe1\u606f\uff0c\u4f8b\u5982\u5c5e\u4e8e java.util.concurrent \u7684 ownable synchronizers \u5217\u8868\n\t<\/li>\n<li>\n\t\t-h | -help \u6253\u5370\u5e2e\u52a9\u4fe1\u606f\n\t<\/li>\n<\/ul>\n<p>\n\t\u8bf4\u4e00\u4e0b\uff0c\u6709\u4e9b\u4eba\u53ef\u80fd\u8fd8\u4e0d\u77e5\u9053 pid \u662f\u4ec0\u4e48\uff1fpid \u5c31\u662f Java \u7684\u8fdb\u7a0b id\uff0c\u53ef\u4ee5\u4f7f\u7528\u6211\u4eec\u4e0a\u4e00\u7bc7\u4e2d\u7684 jps \u67e5\u770b\uff08<a href=\"https:\/\/www.xttblog.com\/?p=3604\" title=\"Java \u9ad8\u5927\u4e0a\u7684\u865a\u62df\u673a\u8fdb\u7a0b\u72b6\u6001\u7ba1\u7406\u5de5\u5177 jps \u4f7f\u7528\u6559\u7a0b\uff01 - \u4e1a\u4f59\u8349\">Java \u9ad8\u5927\u4e0a\u7684\u865a\u62df\u673a\u8fdb\u7a0b\u72b6\u6001\u7ba1\u7406\u5de5\u5177 jps \u4f7f\u7528\u6559\u7a0b\uff01<\/a>\uff09\u3002\n<\/p>\n<p>\n\t\u5f53\u6211\u4eec\u8981\u67e5\u770b\u5806\u6808\u4fe1\u606f\u53ef\u4ee5\u6267\u884c jstack 88888\uff08pid\uff09\u300288888 \u662f\u6211\u7684\u4e00\u4e2a java \u8fdb\u7a0b id\uff0c\u6bcf\u4e2a\u8fdb\u7a0b\u7684 id \u90fd\u4e0d\u4e00\u6837\u3002\n<\/p>\n<p>\n\t\u76f4\u63a5\u5728\u63a7\u5236\u53f0\u67e5\u770b\uff0c\u592a\u9ebb\u70e6\u4e86\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u7684\u547d\u4ee4\uff0c\u5c06 jstack \u7ed3\u679c\u8f93\u51fa\u5230\u6587\u4ef6\u4e2d\u3002\n<\/p>\n<pre class=\"brush:bash;\">\r\njstack -l 88888 &gt;1.txt<\/pre>\n<p>\n\t\u8f93\u51fa\u7684\u5185\u5bb9\u5927\u81f4\u5982\u4e0b\uff0c\u53ea\u5217\u4e00\u5c0f\u90e8\u5206\uff1a\n<\/p>\n<pre class=\"brush:plain;\">\r\nFull thread dump Java HotSpot(TM) Client VM (20.45-b01 mixed mode, sharing):\r\n\r\n&quot;Attach Listener&quot; daemon prio=10 tid=0x08251400 nid=0x11bd runnable [0x00000000]\r\n   java.lang.Thread.State: RUNNABLE\r\n\r\n   Locked ownable synchronizers:\r\n        - None\r\n\r\n&quot;DestroyJavaVM&quot; prio=10 tid=0xb3a0a800 nid=0xffa waiting on condition [0x00000000]\r\n   java.lang.Thread.State: RUNNABLE<\/pre>\n<p>\n\t\u5982\u679c\u4e0d\u77e5\u9053\u600e\u4e48\u5206\u6790\uff0c\u53ef\u4ee5\u67e5\u770b\u6211\u7684\u8fd9\u7bc7\u6587\u7ae0\u300a<a href=\"https:\/\/www.xttblog.com\/?p=3128\" title=\"jvm hs_err_pid.log \u6587\u4ef6\u5206\u6790\u5de5\u5177 CrashAnalysis \u4f7f\u7528\u6559\u7a0b - \u4e1a\u4f59\u8349\">jvm hs_err_pid.log \u6587\u4ef6\u5206\u6790\u5de5\u5177 CrashAnalysis \u4f7f\u7528\u6559\u7a0b<\/a>\u300b\u3002\n<\/p>\n<p>\n\t\u6216\u8005\u4f7f\u7528\u6211\u4e0b\u9762\u7684\u8fd9\u4e2a\u811a\u672c\u3002\n<\/p>\n<pre class=\"brush:bash;\">\r\n#!\/bin\/sh\r\nJAVA_HOME=\/home\/q\/java\/default\r\nWEB_HOME=\/home\/q\/www\r\njstack=&quot;$JAVA_HOME\/bin\/jstack&quot;\r\nprocessId=$1\r\n#\u5982\u679c\u6ca1\u6709\u8f93\u5165\u8fdb\u7a0bID\uff0c\u63d0\u793a\u7528\u6237\u8f93\u5165\r\nif [ ! -n \"$processId\" ] ;then\r\n    echo &quot;please input a java processId &quot;\r\n    read processId\r\nfi\r\necho $processId\r\n#\u5982\u679c\u662f\u6807\u51c6\u8f93\u51fa\u5219\u53d8\u6210\u7ea2\u8272\uff0c\u5426\u5219\u6b63\u5e38\u8f93\u51fa\r\nredEcho(){\r\n   if [ -c \/dev\/stdout ] ; then\r\n       echo -e &quot;\\033[1;31m$@\\033[0m&quot;\r\n   else\r\n       echo &quot;$@&quot;\r\n   fi\r\n}\r\nshowProcessAllthreads(){\r\n   while read eachLine ; do\r\n      threadId=`echo $eachLine | awk &#039;{print $1}&#039;`\r\n      threadId0x=`printf %x ${threadId}`\r\n      pcpu=`echo ${eachLine} | awk &#039;{print $2}&#039;`\r\n      totalTime=`echo ${eachLine} | awk &#039;{print $3}&#039;`\r\n      #\u5b58\u50a8\u6587\u4ef6\r\n      jstackFile=\/tmp\/${uniqueId}_${processId}\r\n      sudo -u tomcat $jstack ${processId} &gt; ${jstackFile} || {\r\n          redEcho &quot;failed to use jstack commond on java process ${processId}&quot;\r\n          rm -rf ${jstackFile}\r\n          continue\r\n      }\r\n      redEcho &quot;java processId is [${processId}] : current thread is(${threadId}\/${threadId0x}) : cost total process time is [${totalTime}] : use total cup precent is [${pcpu}]&quot;\r\n      sed &quot;\/nid=0x${threadId0x}\/,\/^$\/p&quot; -n ${jstackFile}\r\n   done\r\n}\r\n#\u4e3a\u5f53\u524d\u7684\u7a0b\u5e8f\u7684\u6587\u4ef6\u751f\u6210\u4e00\u4e2a\u552f\u4e00\u7684\u5730\u5740(\u65f6\u95f4+\u968f\u673a\u6570+\u8fdb\u7a0b\u53f7(\u6ce8\u610f\u8fd9\u4e2a\u8fdb\u7a0b\u53f7\u662f\u5f53\u524d\u7a0b\u5e8f\u7684\u8fdb\u7a0b\u53f7\uff0c\u4e0d\u662fjava\u57ce\u5e02\u7684\u8fdb\u7a0b\u53f7))\r\nuniqueId=`date +%s`_${RANDOM}_$$\r\necho $uniqueId\r\ntop -p $processId -H -n 1 -b | sed -n &quot;8,1000p&quot; | awk &#039;{print $1,$9,$11}&#039; | showProcessAllthreads\r\nrm -rf \/tmp\/${uniqueId}<\/pre>\n<p>\n\t\u5bf9\u7ebf\u7a0b\u72b6\u6001\u4e0d\u4e86\u89e3\u7684\uff0c\u53ef\u4ee5\u770b\u6211\u7684\u8fd9\u7bc7\u6587\u7ae0\u300a<a href=\"https:\/\/www.xttblog.com\/?p=3544\" title=\"\u4e86\u89e3\u591a\u7ebf\u7a0b\uff0c\u5148\u4ece\u201c\u56fe\u201d\u4e86\u89e3\u7ebf\u7a0b\u7684\u57fa\u672c\u72b6\u6001 - \u4e1a\u4f59\u8349\">\u4e86\u89e3\u591a\u7ebf\u7a0b\uff0c\u5148\u4ece&ldquo;\u56fe&rdquo;\u4e86\u89e3\u7ebf\u7a0b\u7684\u57fa\u672c\u72b6\u6001<\/a>\u300b\u3002<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=3608\">\u8be6\u89e3 jstack \u547d\u4ee4\u7684\u7528\u6cd5<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u5927\u5bb6\u6700\u8fd1\u53ef\u80fd\u4f1a\u53d1\u73b0\u6211\u5199\u7684\u6587\u7ae0\u90fd\u5f88\u5957\u8def\uff0c\u4e00\u4e2a\u7cfb\u5217\u4e00\u4e2a\u7cfb\u5217\u7684\u3002\u524d\u9762\u5199\u4e86\u4e00\u7cfb\u5217\u7684\u6570\u636e\u7ed3\u6784\uff0c\u6700\u8fd1\u6211\u4f1a\u5f00\u59cb\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u4e00\u4e9b [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[1844,945,1699,41,946],"class_list":["post-3608","post","type-post","status-publish","format-standard","hentry","category-java","tag-jps","tag-jstack","tag-jvisualvm","tag-jvisualvm-exe","tag-946"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3608","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3608"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3608\/revisions"}],"predecessor-version":[{"id":3609,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3608\/revisions\/3609"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3608"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3608"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3608"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}