JVM-heap memory tuning

6.1 Differences in heap area

JDK 1.7 heap memory

JDK1.8 heap memory

Use IDEA to adjust the heap memory size test

Heap memory tuning

-Xms : Set the initial allocation size, the default is "1/64" of the physical memory.
-Xmx : Maximum allocated memory, the default is "1/4" of physical memory.
-XX:+PrintGCDetails: output detailed GC processing log.

Code example

package cn.guardwhy.jvm_02;

public class Demo01 {
    
    
    public static void main(String[] args) {
    
    
        // 1.返回虚拟机试图使用的最大内存
        long maxMemory = Runtime.getRuntime().maxMemory();  // 字节 1024 * 1024
        // 2.返回JVM的初始化总内存
        long totalMemory = Runtime.getRuntime().totalMemory();

        // 输出结果
        System.out.println("MAX_MEMORY=" + maxMemory + "字节\t" + (maxMemory/(double)1024/1024)+ "MB");
        System.out.println("TOTAL_MEMORY=" + totalMemory + "字节\t" + (totalMemory/(double)1024/1024)+ "MB");

        // 默认情况下: 分配的总内存是PC内存的1/4, 而初始化的内存是:1/64
    }
}

JVM tuning parameter settings in IDEA

By default, the allocated memory is 1/4 of the total memory, and the initialized memory is 1/64

-Xms1024m -Xmx1024m -XX:+PrintGCDetails

Results of the

Metaspace is not in the virtual machine, but uses local memory.

6.2 Dump memory snapshot

When running a java program, you need to use the test tool to check the memory usage during the test run. There is such a plug-in JProfiler in idea, a performance bottleneck analysis tool.
Basic function :

  • Analyze Dump files to quickly locate memory leaks.
  • Obtain the statistical data of the objects in the heap, and obtain the mutual reference relationship between the objects.
  • A tree is used to show the mutual references between objects.

Install JProfiler

1. IDEA plug-in installation

2. Install JProfiler monitoring software

download link

3. Registration code

L-Larry_Lau@163.com#5481-ucjn4a16rvd98#6038
L-Larry_Lau@163.com#99016-hli5ay1ylizjj#27215

4. Configure IDEA operating environment

5. Select the project you want to analyze, click on the JProfiler icon to start, and the JProfiler window will automatically pop up after the start, where you can monitor your code performance.

Code test

package cn.guardwhy.jvm_02;

import java.util.ArrayList;
/*
* -Xms 设置初始化内存分配大小 1/64
* -Xmx 设置最大分配内存, 默认1/4
* -XX:+PrintGCDetails 打印GC垃圾回收信息
* -XX:+HeapDumpOnOutOfMemoryError // OOM DUMP
*/
public class Demo02 {
    
    
    // 创建字节数组对象
    byte[] bytes = new byte[1*1024*1024]; // 1m = 1024k
    public static void main(String[] args) {
    
    
        ArrayList<Demo02> list = new ArrayList<>();
        // 定义计数器
        int count = 0;
        try {
    
    
            while (true){
    
    
                list.add(new Demo02());
                count = count + 1;
            }
        } catch (Error e) {
    
    
            System.out.println("总数:" + count);
            e.printStackTrace();
        }
    }
}

VM parameter debugging

-Xms1m -Xmx8m -XX:+HeapDumpOnOutOfMemoryError

Results of the

Find file

Use Jprofiler tool to analyze and view

Double-click this file to open it with Jprofiler

The biggest target specific errors


Guess you like

Origin blog.csdn.net/hxy1625309592/article/details/114580109
Recommended