示例1:
public class Test {
public static void main(String[] args) {
testAllocation();
}
private static final int _1MB = 1024 * 1024;
public static void testAllocation() {
byte[] allocation1, allocation2, allocation3, allocation4;
allocation1 = new byte[2 * _1MB];
allocation2 = new byte[2 * _1MB];
allocation3 = new byte[2 * _1MB];
allocation4 = new byte[4 * _1MB];
}
}
gc日志如下:
0.098: [GC (Allocation Failure) [PSYoungGen: 947K->496K(2560K)] 7091K->6776K(9728K), 0.0011675 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
0.099: [GC (Allocation Failure) [PSYoungGen: 496K->496K(2560K)] 6776K->6792K(9728K), 0.0004304 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
0.100: [Full GC (Allocation Failure) [PSYoungGen: 496K->0K(2560K)] [ParOldGen: 6296K->6736K(7168K)] 6792K->6736K(9728K), [Metaspace: 2662K->2662K(1056768K)], 0.0033613 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
0.103: [GC (Allocation Failure) [PSYoungGen: 0K->0K(2560K)] 6736K->6736K(9728K), 0.0001989 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
0.103: [Full GC (Allocation Failure) [PSYoungGen: 0K->0K(2560K)] [ParOldGen: 6736K->6724K(7168K)] 6736K->6724K(9728K), [Metaspace: 2662K->2662K(1056768K)], 0.0036397 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap
PSYoungGen total 2560K, used 82K [0x00000000ffd00000, 0x0000000100000000, 0x0000000100000000)
eden space 2048K, 4% used [0x00000000ffd00000,0x00000000ffd14848,0x00000000fff00000)
from space 512K, 0% used [0x00000000fff00000,0x00000000fff00000,0x00000000fff80000)
to space 512K, 0% used [0x00000000fff80000,0x00000000fff80000,0x0000000100000000)
ParOldGen total 7168K, used 6724K [0x00000000ff600000, 0x00000000ffd00000, 0x00000000ffd00000)
object space 7168K, 93% used [0x00000000ff600000,0x00000000ffc91120,0x00000000ffd00000)
Metaspace used 2694K, capacity 4486K, committed 4864K, reserved 1056768K
class space used 290K, capacity 386K, committed 512K, reserved 1048576K
FULLGC发生是因为年老代或永久代占用内存过高,需通过gc回收内存,当然FullGC后仍然无法满足内存需求,就会报内存溢出
第一次fullGC,年老代gc前占用量为6296K(87%)到gc后占用量6736K(93%),此时堆gc前占用量6792K(69%)到gc后占用量6736K(69%)
,另外第一次年轻代回收了496K,年老代增加440K,所以堆增加496-40=56K
第二次fullGC,年老代gc前占用量为6736K(93%)到gc后占用量6724K(93%),此时堆gc前占用量6736K(69%)到gc后占用量6724K(69%)
,另外第二次年轻代回收了0K,年老代回收12K,所以堆回收12K
示例2: import java.util.ArrayList; import java.util.List; public class Test { public static void main(String[] args) { //testAllocation(); List<String> list = new ArrayList<String>(); while(true){ list.add("hello"); } } }
gc日志如下:
0.099: [GC (Allocation Failure) [PSYoungGen: 1718K->485K(2560K)] 1718K->897K(9728K), 0.0007919 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
0.106: [Full GC (Ergonomics) [PSYoungGen: 2505K->0K(2560K)] [ParOldGen: 7094K->3756K(7168K)] 9600K->3756K(9728K), [Metaspace: 2662K->2662K(1056768K)], 0.0187327 secs] [Times: user=0.02 sys=0.00, real=0.02 secs]
0.125: [GC (Allocation Failure) [PSYoungGen: 0K->0K(2560K)] 3756K->3756K(9728K), 0.0002094 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
0.125: [Full GC (Allocation Failure) [PSYoungGen: 0K->0K(2560K)] [ParOldGen: 3756K->3745K(7168K)] 3756K->3745K(9728K), [Metaspace: 2662K->2662K(1056768K)], 0.0186179 secs] [Times: user=0.02 sys=0.00, real=0.02 secs]
Heap
PSYoungGen total 2560K, used 81K [0x00000000ffd00000, 0x0000000100000000, 0x0000000100000000)
eden space 2048K, 3% used [0x00000000ffd00000,0x00000000ffd146a0,0x00000000fff00000)
from space 512K, 0% used [0x00000000fff80000,0x00000000fff80000,0x0000000100000000)
to space 512K, 0% used [0x00000000fff00000,0x00000000fff00000,0x00000000fff80000)
ParOldGen total 7168K, used 3745K [0x00000000ff600000, 0x00000000ffd00000, 0x00000000ffd00000)
object space 7168K, 52% used [0x00000000ff600000,0x00000000ff9a8458,0x00000000ffd00000)
Metaspace used 2694K, capacity 4486K, committed 4864K, reserved 1056768K
class space used 289K, capacity 386K, committed 512K, reserved 1048576K