Troubleshooting of high CPU and memory usage in Java

Come and go 2021-02-23 17:01:43
troubleshooting high cpu memory usage


Next, analyze and check through simulation examples Java Applications CPU And the process of high memory consumption . If it is Java interview , this 2 There's a high probability that a question will appear in the interview process , So I'm going to sum it up here .

1、Java CPU Too high a problem

for instance , as follows : 

package com.classloading;
public class Test {
static class MyThread extends Thread {
public void run() { // Dead cycle , Consume CPU
int i = 0;
while (true) {
i++;
}
}
}
public static void main(String args[]) throws InterruptedException {
new MyThread().start();
Thread.sleep(10000000);
}
}

Use top Command view occupancy CPU Too much progress . As shown in the figure below .

  

Check the process 6102 The occupation of the next thread , As shown in the figure below .

 

Use the following command to 6122 Convert to 16 Hexadecimal said , as follows :

 

export CPU Occupy the thread stack of high process . The order is as follows :

jstack pid >> java.txt

The contents are as follows :

mazhi@mazhi:~$ cat java.txt
Attaching to remote server pid, please wait...
2021-02-23 15:38:18
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.192-b12 mixed mode):
"Attach Listener" #10 daemon prio=9 os_prio=0 tid=0x00007f4ee0001000 nid=0x1956 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
// This is a 0x17ea Threads , It's also occupation CPU The highest thread
"Thread-0" #9 prio=5 os_prio=0 tid=0x00007f4f180d6000 nid=0x17ea runnable [0x00007f4f044da000]
java.lang.Thread.State: RUNNABLE
at com.cpuhigh.Test$MyThread.run(Test.java:8) // This indicates the second 8 That's ok , That's the beginning of the loop
...

The exported stack information has the state of the thread ( I usually look for RUNNABLE state ) And call stack to find problems . Threads dump analysis : Threads dump The main purpose of analysis is to locate the cause of thread long pause  

2、Java Check the problem of high memory  

Here's an example :

package com.classloading;
import java.util.ArrayList;
import java.util.List;
public class Test {
private static final int UNIT_MB = 1024 * 1024;
public static void main(String args[]) throws InterruptedException{
List<Object> x = new ArrayList<Object>();
int i = 0;
while(i<1000){
x.add(new byte[UNIT_MB]);
i++;
}
Thread.sleep(1000000000);
}
}

adopt jmap dump memory dump . If it's an online environment , Be careful dump You have to cut off the flow before , Otherwise, large memory dump It's a direct stuck service .

Command line input :

jmap -histo <pid> | head -20

You can see a pid Of java Top service memory usage 20 Class , As shown in the figure below .

 

You can see , The one that takes up the most memory is byte Byte array , share 1008 An example .

jmap There is also an instruction that can convert the whole memory into a file and save it , as follows :

jmap -dump:format=b,file=filename.bin <pid>

Execute the command as shown in the figure below .

 

Can be in JVM Set on startup , In case of OOM, be dump File out . The order is as follows :

-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/tmp/heapdump.hprof

If the snapshot file is small , It can be downloaded to local , And then through MAT analysis , It can also be analyzed online (https://fastthread.io/); If the snapshot file is large , It can be analyzed directly on the server , The command used is :

jhat dump.hprof

jhat It's also jdk One of the built-in tools . It's mainly used to analyze java The order of the heap , You can change the objects in the heap to html In the form of , Including the number of objects , Size and so on , And support Object query language . After the command is executed, it is shown in the figure below .

 

The access is shown in the figure below .

Among them Show heap histogram It shows the size of the object . As shown in the figure below . 

 

 

 

 

版权声明
本文为[Come and go]所创,转载请带上原文链接,感谢
https://javamana.com/2021/02/20210223165821664G.html

  1. k8s-prometheus
  2. Linux Disk Command
  3. Linux FS
  4. 使用docker-compose &WordPress建站
  5. Linux Command
  6. This time, thoroughly grasp the depth of JavaScript copy
  7. Linux Disk Command
  8. Linux FS
  9. Using docker compose & WordPress to build a website
  10. Linux Command
  11. 摊牌了,我 HTTP 功底贼好!
  12. shiro 报 Submitted credentials for token
  13. It's a showdown. I'm good at it!
  14. Shiro submitted credentials for token
  15. Linux Stress test
  16. Linux Root Disk Extension
  17. Linux Stress test
  18. Linux Root Disk Extension
  19. Redis高级客户端Lettuce详解
  20. springboot学习-综合运用(一)
  21. 忘记云服务器上MySQL数据库的root密码时如何重置密码?
  22. Detailed explanation of lettuce, an advanced client of redis
  23. Springboot learning integrated application (1)
  24. Linux File Recover
  25. Linux-Security
  26. How to reset the password when you forget the root password of MySQL database on the cloud server?
  27. Linux File Recover
  28. Linux-Security
  29. LiteOS:盘点那些重要的数据结构
  30. Linux Memory
  31. Liteos: inventory those important data structures
  32. Linux Memory
  33. 手把手教你使用IDEA2020创建SpringBoot项目
  34. Hand in hand to teach you how to create a springboot project with idea2020
  35. spring boot 整合swagger2生成API文档
  36. Spring boot integrates swagger2 to generate API documents
  37. linux操作系统重启后 解决nginx的pid消失问题
  38. Solve the problem of nginx PID disappearing after Linux operating system restart
  39. JAVA版本号含义
  40. The meaning of java version number
  41. 开源办公开发平台丨Mysql5.7两套四节点主从结构环境搭建教程(二)
  42. 开源办公开发平台丨Mysql5.7两套四节点主从结构环境搭建教程(一)
  43. Open source office development platform mysql5.7 two sets of four node master-slave structure environment building tutorial (2)
  44. HTTP的“无状态”和REST的“状态转换”
  45. Open source office development platform mysql5.7 two sets of four node master-slave structure environment building tutorial (1)
  46. 【大数据哔哔集20210128】使用Hive计算环比和同比
  47. 【大数据哔哔集20210125】Kafka将逐步弃用对zookeeper的依赖
  48. 【大数据哔哔集20210124】有人问我Kafka Leader选举?我真没慌
  49. 【大数据哔哔集20210123】别问,问就是Kafka高可靠
  50. Spring 事务、异步和循环依赖有什么关系?
  51. Spring 动态代理时是如何解决循环依赖的?为什么要使用三级缓存?
  52. "Stateless" of HTTP and "state transition" of rest
  53. [big data bibiji 20210128] use hive to calculate month on month and year on year
  54. [big data bibiji 20210125] Kafka will gradually abandon its dependence on zookeeper
  55. [big data beeps 20210124] someone asked me about Kafka leader election? I'm not in a panic
  56. [big data bibiji 20210123] don't ask, ask is Kafka highly reliable
  57. jQuery Gantt Package 在Visual Studio中创建一个新的ASP.NET项目
  58. What is the relationship between spring transactions, asynchrony, and circular dependencies?
  59. How to solve circular dependency in spring dynamic proxy? Why use level 3 caching?
  60. Unicode码的二进制转换(Java)