2

java | 内存溢出 「堆」

 1 year ago
source link: https://benpaodewoniu.github.io/2022/12/17/java107/
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-17java进阶JVM问题诊断

3

讲解堆的内存溢出。

package com.redisc;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

class A {
};

class B {
};

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

static A a = new A();
static B b = new B();

public static void main(String[] args) throws Exception {
int i = 0;
try {
List<String> list = new ArrayList<>();
String a = "hello";
while (true) {
list.add(a);
a = a + a;
i++;
}
} catch (Throwable e) {
e.printStackTrace();
System.out.println(i);
}

}

}
java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:3332)
at java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:124)
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:448)
at java.lang.StringBuilder.append(StringBuilder.java:136)
at com.redisc.Run.main(Run.java:27)
25

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK