/**
* 演示内存的分配策略
*/
public class Demo2_1 {
private static final int _512KB = 512 * 1024;
private static final int _1MB = 1024 * 1024;
private static final int _6MB = 6 * 1024 * 1024;
private static final int _7MB = 7 * 1024 * 1024;
private static final int _8MB = 8 * 1024 * 1024;
// -Xms20M -Xmx20M -Xmn10M -XX:+UseSerialGC -XX:+PrintGCDetails -verbose:gc -XX:-ScavengeBeforeFullGC
public static void main(String[] args) throws InterruptedException {
// new Thread(() -> {
// ArrayList<byte[]> list = new ArrayList<>();
// list.add(new byte[_8MB]);
// list.add(new byte[_8MB]);
// }).start();
//
// System.out.println("sleep....");
// Thread.sleep(1000L);
}
}
Heap
def new generation total 9216K, used 1679K [0x00000007bec00000, 0x00000007bf600000, 0x00000007bf600000)
eden space 8192K, 20% used [0x00000007bec00000, 0x00000007beda3ec0, 0x00000007bf400000)
from space 1024K, 0% used [0x00000007bf400000, 0x00000007bf400000, 0x00000007bf500000)
to space 1024K, 0% used [0x00000007bf500000, 0x00000007bf500000, 0x00000007bf600000)
tenured generation total 10240K, used 0K [0x00000007bf600000, 0x00000007c0000000, 0x00000007c0000000)
the space 10240K, 0% used [0x00000007bf600000, 0x00000007bf600000, 0x00000007bf600200, 0x00000007c0000000)
Metaspace used 2939K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 319K, capacity 388K, committed 512K, reserved 1048576K
/**
* 演示内存的分配策略
*/
public class Demo2_1 {
private static final int _512KB = 512 * 1024;
private static final int _1MB = 1024 * 1024;
private static final int _6MB = 6 * 1024 * 1024;
private static final int _7MB = 7 * 1024 * 1024;
private static final int _8MB = 8 * 1024 * 1024;
// -Xms20M -Xmx20M -Xmn10M -XX:+UseSerialGC -XX:+PrintGCDetails -verbose:gc -XX:-ScavengeBeforeFullGC
public static void main(String[] args) throws InterruptedException {
new Thread(() -> {
ArrayList<byte[]> list = new ArrayList<>();
list.add(new byte[_8MB]);
list.add(new byte[_8MB]);
}).start();
System.out.println("sleep....");
Thread.sleep(1000L);
}
}
sleep....
[GC (Allocation Failure) [DefNew: 3821K->624K(9216K), 0.0019739 secs][Tenured: 8192K->8814K(10240K), 0.0028774 secs] 12013K->8814K(19456K), [Metaspace: 3993K->3993K(1056768K)], 0.0048869 secs] [Times: user=0.01 sys=0.00, real=0.01 secs]
[Full GC (Allocation Failure) [Tenured: 8814K->8758K(10240K), 0.0022079 secs] 8814K->8758K(19456K), [Metaspace: 3993K->3993K(1056768K)], 0.0022375 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Exception in thread "Thread-0" java.lang.OutOfMemoryError: Java heap space
at cn.itcast.jvm.t2.Demo2_1.lambda$main$0(Demo2_1.java:20)
at cn.itcast.jvm.t2.Demo2_1$$Lambda$1/363771819.run(Unknown Source)
at java.lang.Thread.run(Thread.java:748)
Heap
def new generation total 9216K, used 1504K [0x00000007bec00000, 0x00000007bf600000, 0x00000007bf600000)
eden space 8192K, 18% used [0x00000007bec00000, 0x00000007bed78058, 0x00000007bf400000)
from space 1024K, 0% used [0x00000007bf500000, 0x00000007bf500000, 0x00000007bf600000)
to space 1024K, 0% used [0x00000007bf400000, 0x00000007bf400000, 0x00000007bf500000)
tenured generation total 10240K, used 8758K [0x00000007bf600000, 0x00000007c0000000, 0x00000007c0000000)
the space 10240K, 85% used [0x00000007bf600000, 0x00000007bfe8d8c8, 0x00000007bfe8da00, 0x00000007c0000000)
Metaspace used 4507K, capacity 4740K, committed 4992K, reserved 1056768K
class space used 506K, capacity 560K, committed 640K, reserved 1048576K
大对象:当老年代空间足够,新生代空间不够时,会直接放进老年代中。
当老年代和新生代都不足以放进对象时,会出现
Exception in thread “Thread-0” java.lang.OutOfMemoryError: Java heap space异常。