JVM2
Fri, Feb 18, 2022
2-minute read
JVM note 2
JVM memory
Gabage Collection
Reference Counting (Python)
Tracing Gabage Collection
finalization
MAT && JProfiler GC Root
IDEA: JProfiler
Mark-Sweep
Copying
Mark-Compact
Mark-Sweep vs Copying vs Mark-Compact
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 又不能回收它们,才叫内存泄漏
- 但是不好的实践中导致对象的生命周期变成很长导致 OOM, 也是宽泛意义上的内存泄露
Stop The World
concurrent GC && parallel GC
Concurrent
- single Processor
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
G1 Collector
pros and cons
*G1 working process
G1 Remembered Set
Q1: examples to explain Memory Leak
- singleton
- close