-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathOutOfMemory.java
More file actions
46 lines (37 loc) · 1.02 KB
/
OutOfMemory.java
File metadata and controls
46 lines (37 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* User: cpucode
* Date: 2021/1/5
* Time: 15:54
* Github: https://github.com/CPU-Code
* CSDN: https://blog.csdn.net/qq_44226094
*/
package com.cpucode.java.heap;
import java.util.ArrayList;
import java.util.Random;
//设置VM 选项: -Xms600m -Xmx600m
//通过visualVM 查看GC 变化
// 查看取样器的存储
public class OutOfMemory {
public static void main(String[] args) {
ArrayList<Picture> list = new ArrayList<>();
while (true){
try{
Thread.sleep(20);
} catch(InterruptedException e){
e.printStackTrace();
}
list.add(new Picture(new Random().nextInt(1024 * 1024)));
}
}
}
class Picture{
private final byte[] pixels;
public Picture(int length){
this.pixels = new byte[length];
}
}
/*
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at com.cpucode.java.heap.Picture.<init>(OutOfMemory.java:35)
at com.cpucode.java.heap.OutOfMemory.main(OutOfMemory.java:26)
* */