6

java | 垃圾回收后,内存占用依然很高「堆」

 1 year ago
source link: https://benpaodewoniu.github.io/2022/12/18/java114/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

java | 垃圾回收后,内存占用依然很高「堆」

2022-12-18java进阶JVM问题诊断

2

有的时候 GC 根本没有用处,导致内存占有率非常高,所以,这里借助 visualvm

打开 visualvm

114_0.png

发现堆内存非常高,然后点击红框的 「堆 Dump」。

114_1.png

然后点击「查找」,看看堆中是哪些对象占用的。发现时第一个对象,对这个对象双击。

114_2.png

然后发现,是这个对象存储了 Student 对象。然后看看这段代码。

package com.redisc;

import lombok.extern.slf4j.Slf4j;

import java.util.*;


@Slf4j(topic = "c.Run")
public class Run {

public static void main(String[] args) throws Exception {
List<Student> students = new ArrayList<>();
for (int i = 0; i < 200; i++) {
students.add(new Student());
}
Thread.sleep(10000000000L);
}

}

class Student {
private byte[] big = new byte[1024 * 1024];
}

由于 students 一直存储着 student 对象,导致,并且一直被使用,导致,GC 没办法回收,内存占有率,居高不下。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK