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

How to implement memory leak, memory overflow and CPU100%

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article focuses on "how to achieve memory leaks, memory spills and CPU100%", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to achieve memory leaks, memory spills and CPU100%.

Cpu 100%

In the following example, the share of cpu is less than 100%, which is only relatively high, but the method of troubleshooting is the same. I hope everyone will not play a corner.

Windows

1. Find the java process number with the highest cpu share

PID: 20260

2. Find the thread number with the highest share of cpu according to the process number

Double-click the java process you just found

Thread number: 15900, converted to hexadecimal: 3e1c

3. Use jstack to generate snapshots of all threads in the virtual machine

Command: jstack-l {pid} > {path}

File path: d:\ 20260.stack

4. Thread snapshot analysis

Let's browse the contents of the snapshot first.

The content is quite concise, and the thread snapshot format is uniform. Let's briefly explain it with a thread snapshot.

"main" # 1 prio=5 os_prio=0 tid=0x0000000002792800 nid=0x3e1c runnable 0x00000000025cf000

We found the highest thread number in cpu: 15900, hexadecimal: 3e1c. Use 3e1c to search the snapshot file.

Since then, the problem has been found.

Linux

The method of troubleshooting is the same as the Windows version, except that the commands are somewhat different.

1. Find the java process number with the highest cpu share

Use the command: top-c to display the list of running processes, and shift + p to sort the list by cpu usage

Processes with PID = 2227, with the highest cpu utilization

2. Find the thread number with the highest share of cpu according to the process number

Use the command: top-Hp {pid}, and shift + p can sort the list of threads by cpu utilization

Threads with PID = 2228 consume the highest cpu, with decimal 2228 converted to hexadecimal 8b4

3. Use jstack to generate snapshots of all threads in the virtual machine

4. Thread snapshot analysis

The analysis method is the same as the Windows version. We can download 2227.stack locally for analysis, or we can analyze it directly on Linux.

Analyze on Linux, command: cat 2227.stack | grep '8b4'-C 5

Locate the problem at this point

Whether under Windows or Linux, the troubleshooting routine is the same.

Memory leak

Similarly, Windows and Linux each show an example

Windows

1. Find the process number PID with the highest memory share

At first glance, idea has the highest memory share because I started the java process with idea; we don't need to pay attention to the idea process. We found PID: 10824 of java with the highest memory share.

2. Use jmap to generate heap dump snapshot

Command: jmap-dump:format=b,file= {path} {pid}

Dump file path: d:\ heapdump_108244.hprof

3. Use MAT to analyze dump files.

MAT:Memory Analyzer Tool is a memory analysis tool for java. Download address:

Select the corresponding version and decompress it directly after downloading. By default, the maximum memory of mat is 1024m, while our dump files are often larger than 1024m, so we need to adjust, find MemoryAnalyzer.ini in the home directory of mat, modify-Xmx1024m to a space larger than dump, and I changed it to-Xmx4096m.

Then we can import the dump file into mat and start parsing the dump file

Parsing is a long process. We need to wait patiently.

After the parsing is complete, we can see the following overview interface

The details of each window will not be described in detail, and those who are interested can consult the data on their own; let's take a look at several charts: pie chart, histogram, dominant tree, suspicious memory leak report.

Pie chart

As you can see, the com.lee.schedule.Schedule object holds 1 gigabyte of memory. There must be a problem.

Histogram

Let's look at the definition of Person.

It is conceivable that several of the items marked above are related to Person.

Dominant tree

This is very intuitive. ArrayList in Schedule accounts for 99.04% of the size.

Suspicious memory leak report

Through these data, I believe you can also find the problem.

Linux

The troubleshooting method is the same as that of Windows, except that there is a slight command difference.

1. Find the process number with the highest memory footprint

Use the command: top-c to display the list of running processes, and shift + m to sort by memory usage

Process number: 2527

2. Use jmap to generate heap dump snapshot

Command: jmap-dump:format=b,file= {path} {pid}

Heap dump snapshot file path: / opt/heapdump_2527.hprof

3. Analyze the heap dump snapshot with MAT

Download heapdump_2448.phrof locally and analyze it using MAT; the analysis process is exactly the same as that of Windows version

Since then, the problem has been located.

Under Windows and Linux, the troubleshooting process is the same.

Summarize common JVM commands

Jps: lists the running virtual machine processes

Jstat: monitors various running status information of the virtual machine, and can display class loading, memory, garbage collection, JIT compilation and other running data in the process of the virtual machine.

Jinfo: view and adjust the parameters of the virtual machine in real time

Jmap: generate a snapshot of the heap dump, or query the details of the finalize execution queue, Java heap and permanent generation

Jstack: generates a thread snapshot of the current moment of the virtual machine

Jhat: virtual machine heap dump snapshot analysis tool

Used in conjunction with jmap to analyze the heap dump snapshot generated by jmap, which is similar to MAT

Troubleshooting steps

1. Find the corresponding process first: PID

2. Generate thread snapshot stack (or heap dump snapshot: hprof)

3. Analyze the snapshot (or heap dump snapshot) and locate the problem

Memory leak, memory overflow and CPU 100% relationship

Commonly used JVM performance testing tools so far, I believe you have a deeper understanding of "how to achieve memory leaks, memory spills and CPU100%". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Development

Wechat

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

12
Report