JAVA memory analysis: use JDK's own tools for memory and CPU analysis and garbage collection

JAVA memory analysis: use JDK's own tools for memory and CPU analysis and garbage collection

JAVA memory analysis 1: Based on dump memory overflow snapshot analysis
JAVA memory analysis 2: Idea integrated jprofiler to view JVM memory usage
JAVA memory analysis 3: Use JDK's own tools for memory and CPU analysis and garbage collection

introduction

Follow my last two articles, use jprofiler tool for memory and cpu analysis
JAVA memory analysis: based on dump memory overflow snapshot analysis
JAVA memory analysis: idea integrated jprofiler to view JVM memory usage
are based on, need to download jprofiler for analysis, then we Think simple, whether you can use the tools that come with the JDK for analysis. This article is based on using the tools that come with the JDK, jconsole.exe to analyze the memory and CPU, and use the tools that come with it for GC recovery.

text

Steps

1. Run the local program for analysis example
2. Run jconsole.exe
3. View the memory and CPU usage of JVM
4. Send data to the running program and simulate data processing
5. Use the built-in tool to perform GC recovery

1. Run the program

The code examples in the previous two articles will run the server program. The program running here is still the consumer program monitored by rabbitMQ. You can also build a SpringBoot, SpringMVC program by yourself, and provide a Controller interface to request data around , The entrance to observe the memory usage.

package com.cdzg.rabbit.rabbitmqprovider;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class RabbitmqProviderApplication {
    
    

    public static void main(String[] args) {
    
    
        SpringApplication.run(RabbitmqProviderApplication.class, args);
    }

}

2. Open the jconsole.exe tool

My JDK is installed under the java directory of Disk D,
so we first enter the directory D:\java\jdk1.8.0_211\bin
Where to run the program
. From the figure, we can find the jconsole.exe program we are about to run, and its size It is only 17KB, which is actually unscientific. A 17KB running program is how He De can monitor the UI interface.
Behind it, the actual support is: the jar package of tool.jar as shown in the figure.
Actually dependent jar

3. Run jconsole.exe

Now we run the exe
Interface after running

From the picture, we can see that the program I am running on the local idea, now we select it and click connect.
The following interface appears.
After running the interface
This interface is a screenshot of my program after running for 5 minutes. From this tool, we can analyze our memory, CPU, thread and other usage.

4. Here we still send 1000 messages to run the program

package com.cdzg.rabbit.rabbitmqconsumer;

import com.cdzg.rabbit.rabbitmqconsumer.mq.RabbitConfig;
import com.cdzg.rabbit.rabbitmqconsumer.service.TestSendDlxProducer;
import com.cdzg.rabbit.rabbitmqconsumer.service.TestSendProducer;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class RabbitmqConsumerApplicationTests {
    
    

    @Autowired
    private TestSendProducer testSendProducer;
    @Autowired
    private TestSendDlxProducer testSendDlxProducer;

    @Test
    void contextLoads() {
    
    
    }

    @Test
    public void testSend() throws InterruptedException {
    
    
        for (int i = 0; i < 1000; i++) {
    
    
            testSendProducer.sendMsg("你好,打工人!" + i);
        }
        Thread.sleep(2000);
    }

    @Test
    public void testSendMore() throws InterruptedException {
    
    
        testSendProducer.sendMsg(RabbitConfig.CDZG_MOONL_MORE_EXCHANGE, RabbitConfig.CDZG_MOONL_MORE_ROUTINGKEY, "多队列,你好,打工人!");
        Thread.sleep(2000);
    }

    @Test
    public void testSendDlx() throws InterruptedException {
    
    
        for (int i = 0; i < 3; i++) {
    
    
            testSendDlxProducer.sendDlxMsg("延时你好,打工人!" + i, "3000");

        }
        Thread.sleep(2000);
    }
}

The result after running
From the above figure, we can see that the CPU usage has soared from 0.2% to 1%, and other indicators are improving.

5. Use the GC recovery function that comes with the tool

Before GC recovery
Before GC recovery

Insert picture description here
After the GC recovery is executed, it can be clearly seen that the memory has dropped from more than 100 Mb to more than 20 Mb

postscript

Here we are done, using the tools that come with the JDK, jConsole to query and recover the usage of JVM memory, CPU, threads, etc.
In our actual development process, if you want to perform JVM tuning, you can refer to this method to analyze memory, CPU, threads, etc.

I hope everyone will pay attention

Insert picture description here
Follow my personal official account to get more information

In the public account, I shared a series of articles on how my colleague slapped the interviewer, full of dry goods

Guess you like

Origin blog.csdn.net/liaoyue11/article/details/110875466