Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Java process pulls up CPU or load and quickly locates scripts

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Students may often encounter a server running Java in the production environment, and everything is normal when it is first released. After running for a period of time, there will be a phenomenon such as high CPU occupation or soaring load, and the better load or CPU will be higher and higher day by day. If the situation is bad, it will jitter at random and then return to normal, which brings a lot of trouble to the operation and maintenance and development students. Of course, when this problem occurs, how to improve it later, such as: review before code launch, isolation / degradation of relevant strength dependent services, unit testing, regression testing, SQL online audit, basic and business monitoring, related process system, etc.

If the CPU usage or load is soaring and lasts for a long time, there are a large number of troubleshooting steps on the Internet.

Method one

1. Use top to locate the process PID that takes up high CPU

Top

two。 Get thread information

Ps-mp PID-o THREAD,tid,time | sort-rn

3. Convert the required thread ID to hexadecimal format

Printf "% x\ n" tid

4. Print the stack information for the thread

Jstack pid | grep tid # the tid here is the tid in hexadecimal format generated in step 3

Method 2 (recommended)

Can quickly locate the cpu utilization of thread and thread

#! / bin/bash# @ Function# Find out the most cpu consumed threads of java,and print the stack trace of these threads.## @ Usage# $. / javacpu-hype Prog = `basename $0`usage () {cat / dev/null} trap "cleanupWhenExit" EXIT printStackOfThread () {while read threadLine Do pid= `echo ${threadLine} | awk'{print $1} '`print `echo ${threadLine} | awk' {print $2} '`threadId0x= `printf% x ${threadId} `user= `echo ${threadLine} | awk' {print $3} '`pcpu= `echo ${threadLine} | awk' {print $5} '`jstackFile=/tmp/$ {uuid} _ ${pid} [!-f "${jstackFile}"] & & {jstack ${pid} > ${jstackFile} | {redEcho "Fail to jstack java process ${pid}!" Rm ${jstackFile} continue}} redEcho "The stack of busy (${pcpu}%) thread (${threadId} / 0x$ {htreadId0x}) of java process (${pid}) of user (${user}):" sed "/ nid=0x$ {threadId0x} / / ^ $/ p "- n ${jstackFile} done} [- z" ${pid} "] & & {ps-Leo pid,lwp,user,comm,pcpu-- no-headers | awk'$419 =" java "{print $0}'| sort-k5-r-n | head-- lines" ${count} "| printStackOfThread} | | {ps-Leo pid,lwp,user,comm Pcpu-- no-headers | awk-v "pid=$ {pid}"'$1bn "piddline 4 =" java "{print $0}'| sort-K5-r-n | head-- lines" ${count} "| printStackOfThread}

Method 3 (for random jitter of load load of Java server)

#! / usr/bin/env pythonimport osimport time, datetimeimport threading# desc: when system loadavg 1 min load lt 10 Then dump java jstackdef load_stat (): loadavg = {} f = open ("/ proc/loadavg") info = f.read (). Split () f.close () loadavg ['lavg_1'] = info [0] loadavg [' lavg_5'] = info [1] loadavg ['lavg_15'] = info [2] start_time = datetime.datetime.strptime (str (datetime.datetime.now (). Date ()) +' 00Rut00' '% Ymuri% mmi% d% HGV% M') curr_time = datetime.datetime.now () end_time = datetime.datetime.strptime (str (datetime.datetime.now (). Date () + datetime.timedelta (days=2)) +' 23datetime.datetime.strptime 59mm,'% Ymuri% mmi% d% HGV% M') if (start_time)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report