一次服務器CPU占用100%的問題排查


今天寫了一段垃圾代碼,然后上服務器上運行,cpu瞬間飆到了100%,現記錄一下問題排除過程~

1. 問題代碼

 1 package qinfeng.zheng.mockmvcdemo;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.stereotype.Component;
 5 import qinfeng.zheng.mockmvcdemo.service.UserService;
 6 
 7 import javax.annotation.PostConstruct;
 8 
 9 @Component
10 public class TestComponent {
11 
12     @Autowired
13     private UserService userService;
14 
15     @PostConstruct
16     public void init() {
17         new Thread(()->{
18             while (true) {
19                 if ( userService.getUser() !=null) {
20                     // do something
21                 }
22             }
23         }).start();
24     }
25 }

 

2. top 

 

3. 查找問題

3.1  top -Hp  18571, 找出最耗cpu的線程,結果發現18584是就耗了99.9%

 

3.2 將十進制的線程號轉成十六進制

  printf "0x%x\n" 18584

 

3.3  jstack查找問題代碼

  jstack 18571| vim +/0x4898 -

 

 

  跑去看TestComponent.java 的第19行~~~~ 死到這里了。。。。。 

  網上百度一波,while(true) 超級耗cpu, Thread.sleep(1) , 睡一下,cpu直接降到1%

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM