Java Hotspot VM의 자동 힙 메모리 관리 프로세스.
객체가 힙 영역에 할당되고, 접근불가 상태가 되면 할당해제를 한다.
프로세스 내에서 참조되지 않는 객체의 상태를 의미한다.
Employee emp=new Employee();
emp=null;
Employee emp1=new Employee();
Employee emp2=new Employee();
emp1=emp2;//now the first object referred by emp1 is available for garbage collectio
new Employee();
Copying Algorithm을 기반.
1. Heap을 Active 영역과 InActive 영역으로 나누어
Active 영역에만 Object를 할당할 수 있게 하고 Active 영역이 꽉 차게 되면 GC를 수행.
2. GC를 수행하면 Suspend 상태가 되고 살아남은 Live Object를 Inactive 영역으로
Copy 하는 작업을 수행.
약한 세대 가설 (Weak Generational Hypothesis)을 기반.
1. 대부분의 객체는 금방 접근 불가능 상태(unreachable)가 된다.
2. 오래된 객체에서 젊은 객체의 참조는 아주 적게 존재한다.
Java 8부터는 MetaSpace가 Permanent Space를 대체한다.
객체를 수명으로 분류한다.
1. Young Generation
- 새롭게 생성된 객체.
- Eden Space에서 생성된다.
- Minor GC로 수집 된다.
1. Old Generation
- 생성된 후, 접근 가능 상태(reachable)를 유지한 객체.
- Major GC로 수집 된다.
Eden Space
에 넣는다.Eden Space
가 가득차면 Minor GC가 실행된다.Survivor Space0
으로 이동하고, 접근불가 객체는 수집된다.Eden Space
가 가득차면 Minor GC가 실행된다.Survivor Space1
으로 이동하고, 접근불가 객체는 수집된다.Survivor Space0
에 있던 Young Generation 객체는 나이를 먹는다.promotion
).이렇게 되면 Young Generation은 작은 크기를 가진 메모리,
Old Generation은 큰 크기의 메모리를 가지는 메모리로 분류되는 특성이 있으며
Minor GC와 Major GC 방식에 차이를 만든다.
Stop the World
이벤트 시간.Stop the World
이벤트 실행.Stop the World
이벤트를 여러번 실행.Stop the World
.live data
: Root로부터 참조되는 객체Stop the World
이벤트 소요시간 줄임.The Z Garbage Collector (ZGC) is a scalable low latency garbage collector.
ZGC performs all expensive work concurrently,
without stopping the execution of application threads for more than a millisecond.
It is suitable for applications which require low latency.
Pause times are independent of the heap size that is being used. ZGC works well with heap sizes from a few hundred megabytes to 16TB.
출처 : https://wiki.openjdk.org/display/zgc/Main
Runtime 중 제어 불가능한 메모리를 자동으로 해제 해준다.
객체가 가비지 컬렉션으로 수집되기 전에 실행되는 메서드.
Major GC
를 짧게 실행해야 한다. java -Xmx12m -Xms3m -Xmn1m -XX:+UseSerialGC -XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:/opt/app/gc.log -jar c:\javademos\demo\jfc\Java2D\Java2demo.jar
Universal JVM GC analyzer - Java Garbage collection log analysis made easy (gceasy.io)
https://github.com/scouter-project/scouter
Heap Dump – JVM의 힙 영역에서 사용 중인 모든 객체의 스냅샷.
NetBeans Profiler – supports Java SE, Java FX, EJB, mobile applications, and Web applications and could be used to monitor memory, threads and CPU resources.
JProfiler – a thread, memory and CPU profiling tool that can also be used to analyze memory leaks and other performance bottlenecks.
GC Viewer – an open-source tool that allows you to easily visualize information produced by JVM. You can use GC Viewer to see performance metrics related to garbage collection, including accumulated pauses, longest pauses and throughput. Aside from enabling you to run garbage collection, you can also use this tool to set up the preliminary heap size.
VisualVM – based on the NetBeans platform, VisualVM is an easily extensible tool using various plugins to give you detailed data on your applications for monitoring both remote and local apps. You can get memory profiling and manually run the garbage collector using this tool.
Patty in action – another open-source tool that you can use as a profiling tool to give you target and drilled down profiling. You can use this tool to analyze heaps.
JRockit – a proprietary solution from Oracle, JRockit is for Java SE applications that may be used to predict latency, visualize garbage collection and sort through memory-related issues.
GCeasy – GCeasy is a tool that analyzes logs related to garbage collection and is an easy way to detect memory leak problems when analyzing garbage collection logs. Another reason to use GCeasy is that it is available online; there is no need to install it on your machine to use it.
Java Garbage Collection Basics (oracle.com)
Java Memory Leaks: Solutions, Tools, Tutorials & More (stackify.com)
Java Garbage Collection (naver.com)