美文网首页
jvm垃圾标记

jvm垃圾标记

作者: 程序男保姆 | 来源:发表于2020-05-26 22:48 被阅读0次
    • 引用计数法

    在对象头处维护一个counter,每增加一次对该对象的引用计数器自加,如果对该对象的引用失联,则计数器自减。
    当counter为0时,表明该对象已经被废弃,不处于存活状态。这种方式一方面无法区分软、虛、弱、强引用类别。
    另一方面,会造成死锁,假设两个对象相互引用始终无法释放counter,永远不能GC。

    • 可达性分析算法 (市面上比较常用)

      • 虚拟机栈中引用的对象
      • 方法区的类属性所引用的对象
      • 方法区中常量所引用的对象
      • 本地方法栈中引用的对象
    • 验证jvm使用的哪种标记法

      -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps
      -verbose:gc == -XX:+PrintGC 打印gc
      -XX:+PrintGCDetails 打印gc详情
      -XX:+PrintGCDateStamps 打印gc时间

    
    public class Biaoji {
    
        private Object object;
        private byte[] a = new byte[1024 * 1024 * 2];
    
        public static void main(String[] args) {
            Biaoji biaoji1 = new Biaoji();
    
            Biaoji biaoji2 = new Biaoji();
    
            biaoji1.object = biaoji2;
            biaoji2.object = biaoji1;
    
            biaoji1 = null;
    
            biaoji2 = null;
    
            System.gc();
        }
    }
    
    [GC (System.gc()) [PSYoungGen: 8102K->688K(38400K)] 8102K->696K(125952K), 0.0017369 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
    [Full GC (System.gc()) [PSYoungGen: 688K->0K(38400K)] [ParOldGen: 8K->606K(87552K)] 696K->606K(125952K), [Metaspace: 3343K->3343K(1056768K)], 0.0067050 secs] [Times: user=0.01 sys=0.00, real=0.00 secs] 
    Heap
     PSYoungGen      total 38400K, used 998K [0x0000000795580000, 0x0000000798000000, 0x00000007c0000000)
      eden space 33280K, 3% used [0x0000000795580000,0x0000000795679b70,0x0000000797600000)
      from space 5120K, 0% used [0x0000000797600000,0x0000000797600000,0x0000000797b00000)
      to   space 5120K, 0% used [0x0000000797b00000,0x0000000797b00000,0x0000000798000000)
     ParOldGen       total 87552K, used 606K [0x0000000740000000, 0x0000000745580000, 0x0000000795580000)
      object space 87552K, 0% used [0x0000000740000000,0x0000000740097990,0x0000000745580000)
     Metaspace       used 3353K, capacity 4496K, committed 4864K, reserved 1056768K
      class space    used 370K, capacity 388K, committed 512K, reserved 1048576K
    
    Process finished with exit code 0
    

    -XX:+PrintHeapAtGC 打印gc前后堆信息

    image.png

    invocations=1 表示共发生一次gc 。full 0 表示full gc发生了0 次
    一次gc后对象由eden空间转移到了sorvivor form空间 与 old space

    {Heap before GC invocations=1 (full 0):
     PSYoungGen      total 38400K, used 8102K [0x0000000795580000, 0x0000000798000000, 0x00000007c0000000)
      eden space 33280K, 24% used [0x0000000795580000,0x0000000795d69b18,0x0000000797600000)
      from space 5120K, 0% used [0x0000000797b00000,0x0000000797b00000,0x0000000798000000)
      to   space 5120K, 0% used [0x0000000797600000,0x0000000797600000,0x0000000797b00000)
     ParOldGen       total 87552K, used 0K [0x0000000740000000, 0x0000000745580000, 0x0000000795580000)
      object space 87552K, 0% used [0x0000000740000000,0x0000000740000000,0x0000000745580000)
     Metaspace       used 3345K, capacity 4496K, committed 4864K, reserved 1056768K
      class space    used 369K, capacity 388K, committed 512K, reserved 1048576K
    [GC (System.gc()) [PSYoungGen: 8102K->688K(38400K)] 8102K->696K(125952K), 0.0020032 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
    Heap after GC invocations=1 (full 0):
     PSYoungGen      total 38400K, used 688K [0x0000000795580000, 0x0000000798000000, 0x00000007c0000000)
      eden space 33280K, 0% used [0x0000000795580000,0x0000000795580000,0x0000000797600000)
      from space 5120K, 13% used [0x0000000797600000,0x00000007976ac010,0x0000000797b00000)
      to   space 5120K, 0% used [0x0000000797b00000,0x0000000797b00000,0x0000000798000000)
     ParOldGen       total 87552K, used 8K [0x0000000740000000, 0x0000000745580000, 0x0000000795580000)
      object space 87552K, 0% used [0x0000000740000000,0x0000000740002000,0x0000000745580000)
     Metaspace       used 3345K, capacity 4496K, committed 4864K, reserved 1056768K
      class space    used 369K, capacity 388K, committed 512K, reserved 1048576K
    }
    {Heap before GC invocations=2 (full 1):
     PSYoungGen      total 38400K, used 688K [0x0000000795580000, 0x0000000798000000, 0x00000007c0000000)
      eden space 33280K, 0% used [0x0000000795580000,0x0000000795580000,0x0000000797600000)
      from space 5120K, 13% used [0x0000000797600000,0x00000007976ac010,0x0000000797b00000)
      to   space 5120K, 0% used [0x0000000797b00000,0x0000000797b00000,0x0000000798000000)
     ParOldGen       total 87552K, used 8K [0x0000000740000000, 0x0000000745580000, 0x0000000795580000)
      object space 87552K, 0% used [0x0000000740000000,0x0000000740002000,0x0000000745580000)
     Metaspace       used 3345K, capacity 4496K, committed 4864K, reserved 1056768K
      class space    used 369K, capacity 388K, committed 512K, reserved 1048576K
    [Full GC (System.gc()) [PSYoungGen: 688K->0K(38400K)] [ParOldGen: 8K->608K(87552K)] 696K->608K(125952K), [Metaspace: 3345K->3345K(1056768K)], 0.0066034 secs] [Times: user=0.01 sys=0.00, real=0.00 secs] 
    Heap after GC invocations=2 (full 1):
     PSYoungGen      total 38400K, used 0K [0x0000000795580000, 0x0000000798000000, 0x00000007c0000000)
      eden space 33280K, 0% used [0x0000000795580000,0x0000000795580000,0x0000000797600000)
      from space 5120K, 0% used [0x0000000797600000,0x0000000797600000,0x0000000797b00000)
      to   space 5120K, 0% used [0x0000000797b00000,0x0000000797b00000,0x0000000798000000)
     ParOldGen       total 87552K, used 608K [0x0000000740000000, 0x0000000745580000, 0x0000000795580000)
      object space 87552K, 0% used [0x0000000740000000,0x0000000740098170,0x0000000745580000)
     Metaspace       used 3345K, capacity 4496K, committed 4864K, reserved 1056768K
      class space    used 369K, capacity 388K, committed 512K, reserved 1048576K
    }
    Heap
     PSYoungGen      total 38400K, used 998K [0x0000000795580000, 0x0000000798000000, 0x00000007c0000000)
      eden space 33280K, 3% used [0x0000000795580000,0x0000000795679b20,0x0000000797600000)
      from space 5120K, 0% used [0x0000000797600000,0x0000000797600000,0x0000000797b00000)
      to   space 5120K, 0% used [0x0000000797b00000,0x0000000797b00000,0x0000000798000000)
     ParOldGen       total 87552K, used 608K [0x0000000740000000, 0x0000000745580000, 0x0000000795580000)
      object space 87552K, 0% used [0x0000000740000000,0x0000000740098170,0x0000000745580000)
     Metaspace       used 3361K, capacity 4496K, committed 4864K, reserved 1056768K
      class space    used 371K, capacity 388K, committed 512K, reserved 1048576K
    
    Process finished with exit code 0
    
    

    PSYoungGen 表示用的是年轻代使用Parallel Scavenge收集器。

    PSOldGen 表示用的是老年代使用的Parallel Old收集器。

    老年代的名称均为:ParOldGen;但是:-XX:+UseParallelGC和-XX:+UseParallelOldGC使用的老年代收集器不同,如下:
    Option 新生代 老年代
    -XX:+UseParallelGC Parallel Scavenge Serial Old
    -XX:+UseParallelOldGC Parallel Scavenge Parallel Old

    相关文章

      网友评论

          本文标题:jvm垃圾标记

          本文链接:https://www.haomeiwen.com/subject/zlybahtx.html