JAVA 从GC日志分析堆内存 第七节

时间:2021-10-28 09:02:31

JAVA 从GC日志分析堆内存 第七节

在上一章中,我们只设置了整个堆的内存大小。但是我们知道,堆又分为了新生代,年老代。他们之间的内存怎么分配呢?新生代又分为Eden和Survivor,他们的比例大小能改变吗?其实这些都是可控的,以前没有讲到是因为就算讲了也只是讲讲而已,看不到实质性的东西。因此这章我们通过分析GC日志来一步步讲解如何细化设置堆内存。

首先我们来了解几个相关的参数:

-XX:+PrintGCDetails:用于告诉虚拟机回收垃圾的时候顺便打印日志。

-Xloggc:路径 :将打印出来的日志信息保存至指定的路径。

-Xmn10M:设置新生代的内存大小。

-XX:SurvivorRatio=8:调整Eden和Survivor的比例为8:1

我们还是用昨天的代码例子来讲:

import java.util.ArrayList;
import java.util.List; public class Test {
private static List<Test> list = new ArrayList<Test>(); public static void main(String[] args) {
while(true){
Test test = new Test();
list.add(test);
}
} }
import java.util.ArrayList;
import java.util.List; public class Test {
private static List<Test> list = new ArrayList<Test>(); public static void main(String[] args) {
while(true){
Test test = new Test();
list.add(test);
}
} }

然后用参数-Xms20m -Xmx20m -Xmn10m -XX:+PrintGCDetails -Xloggc:d:\gc1.log启动。表示给堆分配20M,给新生代分配10M,打印GC日志,并将其输出至D盘的gc1.log文件中。运行后得到以下日志,这是第一部分:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyBpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMC1jMDYwIDYxLjEzNDc3NywgMjAxMC8wMi8xMi0xNzozMjowMCAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJlZiMiIHhtcDpDcmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIENTNSBXaW5kb3dzIiB4bXBNTTpJbnN0YW5jZUlEPSJ4bXAuaWlkOkJDQzA1MTVGNkE2MjExRTRBRjEzODVCM0Q0NEVFMjFBIiB4bXBNTTpEb2N1bWVudElEPSJ4bXAuZGlkOkJDQzA1MTYwNkE2MjExRTRBRjEzODVCM0Q0NEVFMjFBIj4gPHhtcE1NOkRlcml2ZWRGcm9tIHN0UmVmOmluc3RhbmNlSUQ9InhtcC5paWQ6QkNDMDUxNUQ2QTYyMTFFNEFGMTM4NUIzRDQ0RUUyMUEiIHN0UmVmOmRvY3VtZW50SUQ9InhtcC5kaWQ6QkNDMDUxNUU2QTYyMTFFNEFGMTM4NUIzRDQ0RUUyMUEiLz4gPC9yZGY6RGVzY3JpcHRpb24+IDwvcmRmOlJERj4gPC94OnhtcG1ldGE+IDw/eHBhY2tldCBlbmQ9InIiPz6p+a6fAAAAD0lEQVR42mJ89/Y1QIABAAWXAsgVS/hWAAAAAElFTkSuQmCC" alt="" data-s="300,640" data-type="png" data-ratio="0.10625" data-w="640" data-src="http://mmbiz.qpic.cn/mmbiz/NQanUz3wF4rUcalqIHibdznGQ1tgJc9J4w2XrUgz79iaPIS8ich6G3fibrPnc9rrNY415mp3bnJXcNqczRzXMTvngg/640?wx_fmt=png" />

现在我们来分析下每个部分代表的含义:

1)0.090:就是虚拟机从启动到现在经历的时间。

2)GC:指的是停顿类型(留着下一章讲)

3)PSYoungGen:发生GC的区域,这里指的是年轻代。根据收集器的种类而定。

4)7284K->1016K(9216K):该区域GC前当前区域所使用的容量-->该区域GC后已使用的容量(该区域的总容量),也就是新生代的容量。

5)7284K->6139K(19456K):整个堆GC前当前区域所使用的容量-->整个堆GC后已使用的容量(整个堆的总容量)。

6) 0.0078481:这次GC所占用的时间。

我们再来看看第二部分:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyBpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMC1jMDYwIDYxLjEzNDc3NywgMjAxMC8wMi8xMi0xNzozMjowMCAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJlZiMiIHhtcDpDcmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIENTNSBXaW5kb3dzIiB4bXBNTTpJbnN0YW5jZUlEPSJ4bXAuaWlkOkJDQzA1MTVGNkE2MjExRTRBRjEzODVCM0Q0NEVFMjFBIiB4bXBNTTpEb2N1bWVudElEPSJ4bXAuZGlkOkJDQzA1MTYwNkE2MjExRTRBRjEzODVCM0Q0NEVFMjFBIj4gPHhtcE1NOkRlcml2ZWRGcm9tIHN0UmVmOmluc3RhbmNlSUQ9InhtcC5paWQ6QkNDMDUxNUQ2QTYyMTFFNEFGMTM4NUIzRDQ0RUUyMUEiIHN0UmVmOmRvY3VtZW50SUQ9InhtcC5kaWQ6QkNDMDUxNUU2QTYyMTFFNEFGMTM4NUIzRDQ0RUUyMUEiLz4gPC9yZGY6RGVzY3JpcHRpb24+IDwvcmRmOlJERj4gPC94OnhtcG1ldGE+IDw/eHBhY2tldCBlbmQ9InIiPz6p+a6fAAAAD0lEQVR42mJ89/Y1QIABAAWXAsgVS/hWAAAAAElFTkSuQmCC" alt="" data-s="300,640" data-type="png" data-ratio="0.1828125" data-w="640" data-src="http://mmbiz.qpic.cn/mmbiz/NQanUz3wF4rUcalqIHibdznGQ1tgJc9J4gSQJ0vKFjJicD1Ysuexz3fIorWDjsPVQjl6O11ESmY7tmJVibfOXicS2g/640?wx_fmt=png" />
        看图画红线部分,表示当前的堆中新生代可用内存的大小(一个edenh和一个Survivor视为可用内存),红色框下面则是年老区的大小,加上一共是20m,符合我们所设置的。

红色框的部分则是新生代中eden区和两个Survivor区的大小,可以看出他们的比例是8:1,如果设置为-XX:SurvivorRatio=3的话,结果如下

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyBpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuM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bNw9rTMibwl75kLZHfgspp23j00hAF2O8OuVBW5A/640?wx_fmt=png" />

到这里以上几个参数的作用以及分析就讲完啦,小伙伴们可以打开自己的工具试一试,感受一下。以后碰到了内存泄漏或者内存不足的话就可以直接查看日志来进行分析调优了!