java的gc日志分析

示例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

此时:发现GC次数减少,年老代占用量较低,但仍然无法满足内容需求,导致溢出

jvm默认垃圾回收策略 -XX:+UseParallelGC

示例采用参数为-Xms10M -Xmx10M -Xloggc:gc.log -XX:+PrintGCDetails ,可调整参数改变gc效果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值