-Xms18m
-Xmx18m
-Xmn16m
-XX:+UseSerialGC -XX:-UseAdaptiveSizePolicy
-XX:SurvivorRatio=6
-verbose:gc
-XX:+PrintGC
-XX:+PrintGCDetails
使用上面的JVM参数运行下面的代码时,GC日志中旧生代总容量变成4096K(4M),与设置的2M(18-16)不一样
[Tenured: 3072K->3072K(4096K), 0.0143395 secs]
注意这里,括号里面4096K表示旧生代所有空间为4M,而不是设置的2M.
如果设置 -Xms24m -Xmx24m,则由正常了(输出[Tenured: 6144K->6144K(8192K), 0.0129542 secs]).
这个到底嘛原因呢?
输出如下:
Minor GC should happen
[GC [DefNew (promotion failed): 11509K->11659K(14336K), 0.0123390 secs][Tenured: 3072K->3072K(4096K), 0.0143395 secs] 11509K->11413K(18432K), [Perm : 2075K->2075K(12288K)], 0.0333981 secs] [Times: user=0.01 sys=0.00, real=0.03 secs]
[Full GC [Tenured: 3072K->3072K(4096K), 0.0097443 secs] 13461K->13461K(18432K), [Perm : 2081K->2081K(12288K)], 0.0097931 secs] [Times: user=0.02 sys=0.00, real=0.02 secs]
[Full GC [Tenured: 3072K->3072K(4096K), 0.0177553 secs] 13461K->13448K(18432K), [Perm : 2081K->2075K(12288K)], 0.0178095 secs] [Times: user=0.01 sys=0.00, real=0.02 secs]
java.lang.OutOfMemoryError: Java heap space
Dumping heap to D:/D/myeclipse_dump.bin ...
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at jvm.MultiMajorGCLog$MemoryObject.<init>(MultiMajorGCLog.java:48)
at jvm.MultiMajorGCLog.testMain(MultiMajorGCLog.java:34)
at jvm.MultiMajorGCLog.main(MultiMajorGCLog.java:39)
Unable to create D:/D/myeclipse_dump.bin: File exists
Heap
def new generation total 14336K, used 10748K [0x02a00000, 0x03a00000, 0x03a00000)
eden space 12288K, 87% used [0x02a00000, 0x0347f088, 0x03600000)
from space 2048K, 0% used [0x03800000, 0x03800000, 0x03a00000)
to space 2048K, 0% used [0x03600000, 0x03600000, 0x03800000)
tenured generation total 4096K, used 3072K [0x03a00000, 0x03e00000, 0x03e00000)
the space 4096K, 75% used [0x03a00000, 0x03d00010, 0x03d00200, 0x03e00000)
compacting perm gen total 12288K, used 2082K [0x03e00000, 0x04a00000, 0x07e00000)
the space 12288K, 16% used [0x03e00000, 0x04008840, 0x04008a00, 0x04a00000)
No shared spaces configured.
package jvm;
public class MultiMajorGCLog
{
public static void testMain()
{
try
{
Thread.sleep(40);
}
catch (Exception e)
{
e.printStackTrace();
}
MemoryObject object1 = new MemoryObject(1024 * 1024 * 3);
MemoryObject object2 = new MemoryObject(1024 * 1024 * 3);
MemoryObject object3 = new MemoryObject(1024 * 1024 * 3);
MemoryObject object4 = new MemoryObject(1024 * 1024 * 2);
System.out.println("Minor GC should happen");
MemoryObject object5 = new MemoryObject(1024 * 1024 * 2);
MemoryObject object6 = new MemoryObject(1024 * 1024 * 2);
}
public static void main(String[] args)
{
testMain();
}
private static class MemoryObject
{
private byte[] bytes;
public MemoryObject(int objectSize)
{
this.bytes = new byte[objectSize];
}
}
}