深入理解JVM内存分配策略

理解JVM内存分配策略 三大原则+担保机制

JVM分配内存机制有三大原则和担保机制
具体如下所示:

优先分配到eden区

大对象,直接进入到老年代

长期存活的对象分配到老年代

空间分配担保

对象优先在Eden上分配

如何验证对象优先在Eden上分配呢,我们进行如下实验。

打印内存分配信息

首先代码如下所示:

public class A { public static void main(String[] args) { byte[] b1 = new byte[4*1024*1024]; } }

代码很简单,就是创建一个Byte数组,大小为4mb。
然后我们在运行的时候加上虚拟机参数来打印垃圾回收的信息。

-verbose:gc -XX:+PrintGCDetails

在我们运行后,结果如下所示。

Heap
PSYoungGen total 37888K, used 6718K [0x00000000d6000000, 0x00000000d8a00000, 0x0000000100000000)
eden space 32768K, 20% used [0x00000000d6000000,0x00000000d668f810,0x00000000d8000000)
from space 5120K, 0% used [0x00000000d8500000,0x00000000d8500000,0x00000000d8a00000)
to space 5120K, 0% used [0x00000000d8000000,0x00000000d8000000,0x00000000d8500000)
ParOldGen total 86016K, used 0K [0x0000000082000000, 0x0000000087400000, 0x00000000d6000000)
object space 86016K, 0% used [0x0000000082000000,0x0000000082000000,0x0000000087400000)
Metaspace used 2638K, capacity 4486K, committed 4864K, reserved 1056768K
class space used 281K, capacity 386K, committed 512K, reserved 1048576K

手动指定收集器

我们可以看在新生代采用的是Parallel Scavenge收集器
其实我们可以指定虚拟机参数来选择垃圾收集器。
比方说如下参数:

-verbose:gc -XX:+PrintGCDetails -XX:+UseSerialGC

运行结果如下:

Heap
def new generation total 38720K, used 6850K [0x0000000082000000, 0x0000000084a00000, 0x00000000ac000000)
eden space 34432K, 19% used [0x0000000082000000, 0x00000000826b0be8, 0x00000000841a0000)
from space 4288K, 0% used [0x00000000841a0000, 0x00000000841a0000, 0x00000000845d0000)
to space 4288K, 0% used [0x00000000845d0000, 0x00000000845d0000, 0x0000000084a00000)
tenured generation total 86016K, used 0K [0x00000000ac000000, 0x00000000b1400000, 0x0000000100000000)
the space 86016K, 0% used [0x00000000ac000000, 0x00000000ac000000, 0x00000000ac000200, 0x00000000b1400000)
Metaspace used 2637K, capacity 4486K, committed 4864K, reserved 1056768K
class space used 281K, capacity 386K, committed 512K, reserved 1048576K

其实JDK默认的不是Parallel收集器,但是JDK会依照各种环境来调整采用的垃圾收集器。

查看环境的代码如下:

java -version

深入理解JVM内存分配策略


因此JDK根据server的环境,采用了Paralled收集器。

而Serial收集器主要用在客户端的。

eden分配的验证

我们看到现在eden区域为34432K,使用了19%,那我们来扩大10倍是否eden就放不下了呢?
我们来验证一下。

public class A { public static void main(String[] args) { byte[] b1 = new byte[40*1024*1024]; } }

运行结果如下:

Heap
def new generation total 38720K, used 2754K [0x0000000082000000, 0x0000000084a00000, 0x00000000ac000000)
eden space 34432K, 8% used [0x0000000082000000, 0x00000000822b0bd8, 0x00000000841a0000)
from space 4288K, 0% used [0x00000000841a0000, 0x00000000841a0000, 0x00000000845d0000)
to space 4288K, 0% used [0x00000000845d0000, 0x00000000845d0000, 0x0000000084a00000)
tenured generation total 86016K, used 40960K [0x00000000ac000000, 0x00000000b1400000, 0x0000000100000000)
the space 86016K, 47% used [0x00000000ac000000, 0x00000000ae800010, 0x00000000ae800200, 0x00000000b1400000)
Metaspace used 2637K, capacity 4486K, committed 4864K, reserved 1056768K
class space used 281K, capacity 386K, committed 512K, reserved 1048576K

显然,我们还是正常运行了,但是eden区域没有增加,老年代区域却增加了,符合大对象直接分配到老年代的特征。。

所以我们适当的缩小每次分配的大小。
我们在此限制下eden区域的大小
参数如下:

-verbose:gc -XX:+PrintGCDetails -XX:+UseSerialGC -Xms20M -Xmx20M -Xmn10M -XX:SurvivorRatio=8

这里我们限制内存大小为20M
Eden大小为8M

然后我们运行我们的代码:

代码如下所示:

public class A { public static void main(String[] args) { byte[] b1 = new byte[2*1024*1024]; byte[] b2 = new byte[2*1024*1024]; byte[] b3 = new byte[2*1024*1024]; byte[] b4 = new byte[4*1024*1024]; System.gc(); } }

运行结果如下:

[GC (Allocation Failure) [DefNew: 7129K->520K(9216K), 0.0053010 secs] 7129K->6664K(19456K), 0.0053739 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[Full GC (System.gc()) [Tenured: 6144K->6144K(10240K), 0.0459449 secs] 10920K->10759K(19456K), [Metaspace: 2632K->2632K(1056768K)], 0.0496885 secs] [Times: user=0.00 sys=0.00, real=0.04 secs]
Heap
def new generation total 9216K, used 4779K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
eden space 8192K, 58% used [0x00000000fec00000, 0x00000000ff0aad38, 0x00000000ff400000)
from space 1024K, 0% used [0x00000000ff500000, 0x00000000ff500000, 0x00000000ff600000)
to space 1024K, 0% used [0x00000000ff400000, 0x00000000ff400000, 0x00000000ff500000)
tenured generation total 10240K, used 6144K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
the space 10240K, 60% used [0x00000000ff600000, 0x00000000ffc00030, 0x00000000ffc00200, 0x0000000100000000)
Metaspace used 2638K, capacity 4486K, committed 4864K, reserved 1056768K
class space used 281K, capacity 386K, committed 512K, reserved 1048576K

我们可以发现在eden区域为8192K 约为8M
也就是我们的b4的大小

而原先的b1,b2,b3为6M,被分配到了tenured generation。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zgjgjx.html