JVM2

Fri, Feb 18, 2022 2-minute read

JVM note 2

JVM memory

Gabage Collection

Reference Counting (Python)

image

Reference Counting

image

Reference Counting

Tracing Gabage Collection

image

Tracing Gabage Collection

finalization

image

finalization

MAT && JProfiler GC Root

IDEA: JProfiler

JProfiler

Mark-Sweep

image

Mark-Sweep

image

Mark-Sweep

Copying

image

Copying

image

Copying

Mark-Compact

image

Mark-Compact

Mark-Sweep vs Copying vs Mark-Compact

image

gc

Generational Collecting

image

Generational Collecting

image

Generational Collecting

System.gc()

Full GC

public class SystemGCTest {
    public static void main (String[] args) {
        new SystemGCTest();
        System.gc(); // remind jvc excute gc

        System.runfinalization();
    }
    @Override
    protected void finalize() thrwows Throwable {
        super.finalize();
        System.put.println("SystemGCTest override finalize()");
    }
}

Memory leak && OOM

  • OOM :没有空闲内存,并且垃圾收集器也无法提供更多内容
  • OutOfMemoryError: JVM GC a soft reference object

Memory leak: A memory leak occurs when a process allocates memory from the paged or nonpaged pools, but does not free the memory

  • 只有对象不会再被程序用到了,但是 GC 又不能回收它们,才叫内存泄漏
image

Memory leak

  • 但是不好的实践中导致对象的生命周期变成很长导致 OOM, 也是宽泛意义上的内存泄露

Stop The World

concurrent GC && parallel GC

Concurrent

  • single Processor
image

Memory leak

GC Safe Points && Safe Region

Reference Counting

  • Strong Reference

Object obj = new Object() 强引用存在不回收

  • Soft Reference

内存不足即回收 (Cache)不会导致 OOM

  • Weak Reference

发现就回收 (Cache)

  • Phantom Reference

不会对生存影响,无法获得实例,唯一目的是 gc 收到通知

GC Performance

different GC

Serial Collector

默认被应用在客户端的 Client 模式下的 JVM

ParNew Collector

Parallel Collector

image

different GC

image

different GC

G1 Collector

image

G1

pros and cons

image

G1

image

G1

image

G1

image

G1

*G1 working process

image

G1

G1 Remembered Set

image

G1

image

G1


Q1: examples to explain Memory Leak

  • singleton
  • close
image

Memory leak


JVM