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 .
Java Medium CPU More related articles about high occupancy and high memory occupancy
- Java Member initialization sequence and memory allocation process in
Java Member initialization sequence and memory allocation process in The original post is like this : http://java.dzone.com/articles/java-object-initialization?utm_source= ...
- Java in this、static Memory diagram of keywords
Java There are many keywords in ,abstract default goto* null switch boolean do if package nchronzed break dou ...
- java Medium hashcode Method function and memory leak problem
This article is loaded with :http://hi.baidu.com/iduany/item/6d66dfc9d5f2da1650505870 hashCode() The role of methods & Use analysis I always want to write an article to explain ...
- JAVA Heap in 、 Stack and other memory analysis
stay JAVA in , There are six different places to store data 1. register ( register ) This is the fastest storage area , Because it is located in a different place from other storage areas —— Processor internal . But the number of registers is extremely limited , So registers are written by the compiler based on ...
- stay Golang Get the disk space and memory occupation of the system in
Get disk occupancy (Linux/Mac Effective under ) import ( "syscall" ) type DiskStatus struct { All uint64 `json:&quo ...
- Java Of 8 The number of bytes occupied by memory and the value range of the two basic data types
This is a 8 The number of bytes occupied in the memory of the basic type in ( The value range is 2 Of ( Number of bytes X8-1) Power ) 1. integer type Storage requirements bit Count Value range byte 1 byte 1*8 -128-127 short 2 byte 2*8 -32 ...
- C++ In the virtual function inheritance class memory size calculation
The law of calculating the size of a class object : 1. Empty class . Empty classes of single inheritance . The space occupied by multiple inherited empty classes is :1( byte , The same below ): 2. In a class , Virtual function itself . Member functions ( Including static and non static ) And static data members do not occupy the storage space of class objects ...
- C# obtain CPU Occupancy rate 、 Memory footprint 、 Disk usage 、 Process information
Code : using System; using System.Collections.Generic; using System.Diagnostics; using System.Threading ...
- A wave Linux View in cpu、 disk 、 Memory 、 Network command
Reprint please indicate the source . If you want to manage the server remotely, you have a remote management card , such as Dell idRAC,HP ILO,IBM IMM Check the temperature of the hardware / Fan speed , The computer has Master Lu , The server has ipmitool. Use ipmitool real ...
- java The allocation of member variables and local variables in memory
For member variables and local variables : Member variables are external to the method , Class's internally defined variables : A local variable is a variable defined inside a method or statement block . Local variables must be initialized . Formal parameters are local variables , References and values of the underlying data types in local variables are stored on the stack , Object citation ...
Random recommendation
- GDUT——1169: Krito The crusade of ( Priority queue BFS)
1169: Krito The crusade of Time Limit: 1 Sec Memory Limit: 128 MB Submit: 619 Solved: 102 Description Krito Finally ...
- About .net Some basic knowledge of ( Two )
What is indexer ? What's the role ? Indexers allow instances of classes to access properties in objects in the form of access arrays . For example, we can often see something like dr[“name”]=”test”, Or with config[“connectString”] Lai Huo ...
- ArcGis ToolBar It's gray
Problem description : Add the navigation button , The button is always gray during the test , Unavailable status . Possible causes : It depends on the order in which the controls are added , Lead to toolbar Follow map Control cannot be correctly associated . Solution : Delete toolBar, To add .
- java Next tcp Of socket Connect
serverDemo package cn.stat.p4.ipdemo; import java.io.IOException; import java.io.InputStream; import ...
- WebApi Achieve cross domain functionality
In the configuration file system.webServer Add the following configuration information to the node <httpProtocol> <customHeaders> <add name="Acce ...
- js Delete array elements
One . Empty array var ary = [1,2,3,4]; ary.splice(0,ary.length);// Empty array console.log(ary); // Output [], An empty array , It's emptied Two . Delete ...
- idea Set the background color of the selected code
- CDN Service domain name resolution configuration
Please select record type as CNAME: Host record is the prefix of domain name , for example : If your domain name is ... The prefix for ... testcdn.aliyun.com testcdn www.aliyun.com www ...
- hdu 1394 Minimum Inversion Number - Tree array
The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that ...
- synchronized( Two )
package com.bjsxt.base.sync002;/** * keyword synchronized All locks obtained are object locks , Instead of putting a piece of code ( Method ) As a lock , * So which thread in the code executes first synchro ...