《深入理解JVM》第三章 垃圾收集器与内存分配策略(内存分配和回收策略)

版权声明:版权为ZZQ所有 https://blog.csdn.net/qq_39148187/article/details/81835490

对象优先在Eden 上分配

大多数情况下对象在新生区 的Eden区中分配,当Eden区没有空间的时候,jvm发起一次GC,这意思gc 幸存的对象会被复制到from 或者to 区,然后把Eden 区和  form 或者to区给清理掉,以后每次gc 幸存的对象age+1 如果年龄模式是大于15 晋级老年代,当然大对象有可能直接进入老年代,

https://blog.csdn.net/Muyundefeng/article/details/72667863

package com.jvm.log;

/**
 * @author ZZQ
 * @date 2018/8/19 9:26
 */
public class MinorGC {

    private  static  final  int _1MB =1024*1024;

    public static void main(String[] args) {
        byte[] allocation1 , allocation2, allocation3,allocation4 ;
        allocation1 =new byte[2*_1MB];
        allocation2 =new byte[2*_1MB];
        allocation3 =new byte[2*_1MB];
        allocation4 =new byte[2*_1MB];

    }
}
[GC (Allocation Failure) [PSYoungGen: 2048K->504K(2560K)] 2048K->767K(9728K), 0.0105860 secs] [Times: user=0.00 sys=0.00, real=0.02 secs] 
Heap
 PSYoungGen      total 2560K, used 612K [0x00000000d5f80000, 0x00000000d6280000, 0x0000000100000000)
  eden space 2048K, 5% used [0x00000000d5f80000,0x00000000d5f9b0b0,0x00000000d6180000)
  from space 512K, 98% used [0x00000000d6180000,0x00000000d61fe030,0x00000000d6200000)
  to   space 512K, 0% used [0x00000000d6200000,0x00000000d6200000,0x00000000d6280000)
 ParOldGen       total 9728K, used 8455K [0x0000000081e00000, 0x0000000082780000, 0x00000000d5f80000)
  object space 9728K, 86% used [0x0000000081e00000,0x0000000082641ca0,0x0000000082780000)
 Metaspace       used 3461K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 378K, capacity 388K, committed 512K, reserved 1048576K

Process finished with exit code 0

从日志可看我的jdk1.8 使用的是parallel scavenge收集器回收新生代, 用parallel old 收集器回收老年代,新生代中大小为2.5M回收之后剩余到504K ,堆内存一共9728K 使用2048k 

PSYoungGen 一共2560 k 使用612 K ,我的JVM回收机制使用的parallel scavenge 模式的eden 和 form /to 的比例是4:1 ,如果使用parnew 的话默认是8:1

通过实验,新生代使用ParNew 的话默认的eden 和 幸村区的比例是8:1 ,使用serial的话比例也是如此,但是使用parallel scavenge 就不然,而是4:1 我的jdk1.8 Client 就是4:1

大对象直接进入老年代

所谓大对象就是指需要连续的内存空间存储的对象, 最典型的就是数组,连续的字符串,这种对象对于JVM来说是一个不好的消息,因为连续的内存不好找呀,所以对这种大的对象的安置不好搞,虚拟机提供一个参数-XX:PretenureSizeThreshold 参数令大于这个值的对象直接晋级老年代,这样做的目的是为了避免Eden 和Surivor区之间的大量复制

package com.jvm.log;

import com.sun.corba.se.impl.orbutil.StackImpl;

/**
 * @author ZZQ
 * @date 2018/8/19 10:53
 *
 * 大的对象直接晋级老年代
 */
public class MinorGc1 {

    private static  final  int _1MB = 1024*1024 ;

    public static void main(String[] args) {
        byte[] arr ;
        arr = new byte[4*_1MB];
    }
}
[GC (Allocation Failure) [PSYoungGen: 2048K->504K(2560K)] 6144K->4850K(9728K), 0.0038272 secs] [Times: user=0.05 sys=0.00, real=0.00 secs] 
Heap
 PSYoungGen      total 2560K, used 545K [0x00000000d5f80000, 0x00000000d6480000, 0x0000000100000000)
  eden space 2048K, 2% used [0x00000000d5f80000,0x00000000d5f8a560,0x00000000d6180000)
  from space 512K, 98% used [0x00000000d6180000,0x00000000d61fe030,0x00000000d6200000)
  to   space 512K, 0% used [0x00000000d6400000,0x00000000d6400000,0x00000000d6480000)
 ParOldGen       total 7168K, used 4346K [0x0000000081e00000, 0x0000000082500000, 0x00000000d5f80000)
  object space 7168K, 60% used [0x0000000081e00000,0x000000008223e8e0,0x0000000082500000)
 Metaspace       used 3376K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 371K, capacity 388K, committed 512K, reserved 1048576K

Process finished with exit code 0

长期存活的对象进入老年代

在eden 区经过一次Minor GC 之后对象如果仍然存活,并且能被Survivor 容纳,呢么将对象复制到Survivor 中,年龄设定为1,然后每次GC一次年龄+1 ,默认是15次晋级老年代,

扫描二维码关注公众号,回复: 2970305 查看本文章
package com.jvm.log;

/**
 * @author ZZQ
 * @date 2018/8/19 11:00
 * 使用 -XX:MaxTenuringThreshold  参数来制定 age 多大晋级老年代
 */
public class MinorGc2 {
    private  static  final  int _1MB =1024*1024;

    public static void main(String[] args) {
        byte[] allocation1 , allocation2, allocation3,allocation4 ;
        allocation1 =new byte[4*_1MB];
        allocation2 =new byte[4*_1MB];
        allocation3 = null;
        allocation4 =new byte[4*_1MB];
    }
}
Heap
 PSYoungGen      total 9216K, used 6613K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
  eden space 8192K, 80% used [0x00000000ff600000,0x00000000ffc756b0,0x00000000ffe00000)
  from space 1024K, 0% used [0x00000000fff00000,0x00000000fff00000,0x0000000100000000)
  to   space 1024K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000fff00000)
 ParOldGen       total 10240K, used 8192K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
  object space 10240K, 80% used [0x00000000fec00000,0x00000000ff400020,0x00000000ff600000)
 Metaspace       used 3459K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 378K, capacity 388K, committed 512K, reserved 1048576K

可以看出from 和to 都是空的,gc 回收一轮,对象直接晋级了

动态年龄判断

如果Survivor 空间中相同年龄所有对象的大小的总和大于总Survivor 空间的一般,年龄大于或者等于改年龄的对象可以直接进入老年代,

package com.jvm.log;

/**
 * @author ZZQ
 * @date 2018/8/19 11:18
 * 动态年龄判断
 * -verbose:gc  -XX:+PrintGCDetails -XX:+UseParNewGC -Xms20M -Xmx20M -Xmn10M -XX:MaxTenuringThreshold=15  -XX:+PrintTenuringDistribution
 */
public class MinorGc3 {

    private  static  final  int _1MB =1024*1024;


    public static void main(String[] args) {
        byte[] allocation1 , allocation2, allocation3,allocation4 ;
        allocation1 =new byte[_1MB/4];
        allocation2 =new byte[_1MB/4];
        allocation3 = new byte[4* _1MB];
        allocation3 = new byte[4* _1MB];
        allocation3 = null;
        allocation4 =new byte[4*_1MB];
    }
}
[GC (Allocation Failure) [ParNew
Desired survivor size 524288 bytes, new threshold 1 (max 15)
- age   1:    1037008 bytes,    1037008 total
: 6793K->1022K(9216K), 0.0024469 secs] 6793K->5318K(19456K), 0.0024926 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[GC (Allocation Failure) [ParNew
Desired survivor size 524288 bytes, new threshold 15 (max 15)
- age   1:        240 bytes,        240 total
: 5200K->175K(9216K), 0.0008162 secs] 9496K->5500K(19456K), 0.0008422 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
Heap
 par new generation   total 9216K, used 4407K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
  eden space 8192K,  51% used [0x00000000fec00000, 0x00000000ff022188, 0x00000000ff400000)
  from space 1024K,  17% used [0x00000000ff400000, 0x00000000ff42bcb0, 0x00000000ff500000)
  to   space 1024K,   0% used [0x00000000ff500000, 0x00000000ff500000, 0x00000000ff600000)
 tenured generation   total 10240K, used 5324K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
   the space 10240K,  52% used [0x00000000ff600000, 0x00000000ffb333e8, 0x00000000ffb33400, 0x0000000100000000)
 Metaspace       used 3219K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 354K, capacity 388K, committed 512K, reserved 1048576K
Java HotSpot(TM) 64-Bit Server VM warning: Using the ParNew young collector with the Serial old collector is deprecated and will likely be removed in a future release

内存分配担保

https://blog.csdn.net/yhyr_ycy/article/details/52566105

https://www.cnblogs.com/yang-hao/p/5948207.html

在发生Minor GC之前,虚拟机会先检查老年代的最大可的连续内存是否大于新生代的所有兑现的空间,如果这个条件成立,Minor GC是安全的如果不成立虚拟机会查看HanlerPromotionFailure 设置值是否允许担当失败,如果允许,呢么会继续检查老年代最大可用的连续内的村是否大于历次晋级到老年代对象的平均大小,如果大于就尝试一次Minor GC, 如果小于,或者HanlerPromotionFailure 不愿承担风险就要进行一次Full GC 

猜你喜欢

转载自blog.csdn.net/qq_39148187/article/details/81835490
今日推荐