SlideShare a Scribd company logo
1 of 71
Download to read offline
Garbage Collection in JVM
Default VM : Hotspot VM
Default Language : Java
Feat. @명인님, @재영님
김규하
Garbage Collection 이 뭔가요?
Garbage Collection 이 뭔가요?
Heap이란?
1. JVM이 관리하는 프로그램 상에서 데이터를 저장하기 위해 런타임시 동적으로
할당하여 사용되는 공간이다.
2. JAVA에서 new 키워드를 사용하여 만든 인스턴스는 모두 이곳에 저장된다.
3. 이 영역에서 생성된 객체와 배열은 Stack 영역의 인스턴스나 다른 객체의 필드
에서 참조한다.
4. 참조하는 변수나 필드가 없다면 의미 없는 객체가 되어 GC 대상이 된다.
Heap 구조
Permanent Generation, MetaSpace
static List<?>
Permanent Generation, MetaSpace
Heap 구조
Young
Old
Garbage Collector 란?
1. 대부분의 객체는 금방 접근 불가능한 상태가 된다.
2. 오래된 객체에서 새로운 객체에 대한 참조는 아주 적게 존재 한다.
가비지 컬렉터는 아래의 2가지의 가설에 의해 만들어졌습니다.
이러한 가설을 weak generation hypothesis라 합니다.
가비지 컬렉터(Garbage collector)에 대해 알아보기 전에 반드시 알아야 할 용어가
있습니다.
GC에 대해 알아보기전!
바로
GC에 대해 알아보기전!
Stop-the-world 입니다.
GC에 대해 알아보기전!
Stop-the-world 란?
1. 가비지 컬렉터(Garbage collector)를 실행하기 위해 JVM의 어플리케이션이 잠
시 멈추는 것을 말합니다.
2. 대개 가비지 컬렉터(Garbage collector) 튜닝은 Stop-the-world의 시간을 줄이
는 것을 의미합니다.
Java 에서는 개발자가 명시적으로 메모리를 해제 하지 않아 가비지 컬렉터가 더 이
상 사용되지 않는 쓰레기(Object)를 찾아 회수하는 역활을 합니다.
Garbage Collector 란?
Garbage Collector 란?
현재 JVM(Hotspot VM)에서는 이러한 가설의 장점을 살리기 위해 크게 2가지의 물
리적 공간을 나눠서 사용합니다.
YOUNG OLD
Young 영역이란?
Young 영역이란?
Young 영역이란?
Minor GC?
Minor GC
Old 영역이란?
Young
Old
오래된 객체
오래된 객체
새로운 객체
새로운 객체
오래된 객체 오래된 객체
새로운 객체
새로운 객체
새로운 객체
새로운 객체
새로운 객체
오래된 객체 오래된 객체
오래된 객체
오래된 객체
오래된 객체
오래된 객체
오래된 객체
새로운 객체
새로운 객체
새로운 객체
새로운 객체
새로운 객체
새로운 객체
오래된 객체
오래된 객체
오래된 객체
오래된 객체
오래된 객체
오래된 객체
오래된 객체
오래된 객체
오래된 객체
오래된 객체
Old 영역이란?
Old
오래된 객체 오래된 객체
오래된 객체
오래된 객체
오래된 객체
오래된 객체
오래된 객체
오래된 객체
오래된 객체
오래된 객체
Full GC
Old 영역이란?
Old
오래된 객체 오래된 객체
오래된 객체
오래된 객체
오래된 객체
오래된 객체
오래된 객체
오래된 객체
오래된 객체
오래된 객체
Old 영역이란?
Old
오래된 객체
오래된 객체
오래된 객체
오래된 객체
오래된 객체
Young 영역 구성
S0
Edan
S1
Young 영역 구성
S0 S1
Young 영역 구성
EMPTY
EMPTY
S1
Young 영역 구성
S0 S1
Young 영역 구성
S0 EMPTY
EMPTY
Young 영역 구성
S0 S1
Young 영역 구성
EMPTY S1
EMPTY
Young 영역 구성
S0 S1
Old
Old 영역으로
넘어가기 위한
임계값은 15
이다.
Old
Young 영역 구성
S0 EMPTY
EMPTY
왜 Young 영역에서는 한곳으로 데이터를 몰아넣는가?
Young 영역에서 Compaction(정렬)을 하기에는 성능적으로 문제가 발생한다.
그래서 따로 메모리 단편화에 대한 대처를 하지 않기 위해 살아남은 객체들만
Survivor 영역 중 한곳(비어있는곳)으로 몰아넣는 이유이다.
Old 영역 구성
● Old 영역에서는 기본적으로 데이터가 가득차면 GC가 실행된다.
● GC방식에 따라 처리방식이 달라집니다. GC 방식은 아래와 같습니다.
○ Young GC & Old GC
■ Serial GC
■ Parallel GC
■ Parallel Old GC
■ Concurrent Mark & Sweep GC
○ Other GC
■ G1(Garbage first) GC
Serial GC란?
Old 영역의 Serial GC는 Mark - Sweep - compact라는 알고리즘을 사용합니다.
Old
객체
객체
객체
객체
객체
객체
객체
객체
객체
객체
객체
객체
객체
객체
객체
객체
Mark
Sweep
Serial GC란?
Old 영역의 Serial GC는 Mark - Sweep - compact라는 알고리즘을 사용합니다.
Old
객체
객체
객체
객체
객체
객체
객체
객체
Mark
Sweep
Compact
Parallel GC와 Parallel Old GC 란?
기본적으로 Serial GC와 동작은 동일하지만 작업을 처리하는 CPU 프로세서가
다릅니다.
Parallel GC와 Parallel Old GC 란?
Young 영역을 Multi Thread 처리, 즉 Old 영역은 Single Thread로 처리 한다.
Young 영역과 Old 영역 둘다 Multi Thread 처리
Parallel GC
Parallel Old GC
Serial GC와 Parallel GC 차이
1. 적은 메모리와 단일 코어
2. 서버 운용으론 불가
3. 비교적 긴 Stop-the-world의 시간
1. Serial GC 보단 빠름
2. 많은 메모리와 코어 개수가 많을 때 유리
CMS GC란?
Old
CMS GC란? - Initial Mark
Young
GC ROOTS
STW 발생
Old
CMS GC란? - Concurrent Mark
Young
GC ROOTS
Old
CMS GC란? – ReMark
Young
GC ROOTS
STW 발생
Old
CMS GC란? – Concurrent Sweep
Young
GC ROOTS
CMS GC 장단점?
1. Stop-the-world 의 시간이 매우 짧다
1. 다른 GC 방식보다 CPU를 많이 사용한다.
2. Compaction 단계가 기본제공 되지 않는다.
메모리의 단편화?
사용가능 메모리 0.1MB
Full GC
메모리의 단편화?
사용가능 메모리 30MB5MB
8MB List
CMS 메모리 단편화 관련..
If fragmentation becomes a problem, allocate a larger heap.
G1(Garbage First) GC란?
Edan Space
Survivor Space
Old Generation
EMPTY
G1(Garbage First) GC란?
Young Generation
Old Generation
EMPTY
G1(Garbage First) GC란?
GC를 수행 할 때 G1은 CMS 와 유사한 방식으로 동작합니다.
G1 GC란? – Young 한 활동
Edan Space
Survivor Space
Old Generation
EMPTY
EMPTY EMPTY
G1 GC란? – Young 한 활동
GC &
Empty
GC &
Empty
GC &
Empty
GC &
Empty
Edan Space
Survivor Space
Old Generation
STW 발생
EMPTY
EMPTY EMPTY
G1 GC란? – Young 한 활동
Edan Space
Survivor Space
Old Generation
STW 발생
EMPTY
EMPTY EMPTY
G1 GC란? – Young 한 활동
GC &
EMPTY
GC &
EMPTY
GC &
EMPTY
GC &
EMPTY
EMPTY
EMPTYEMPTY
Edan Space
Survivor Space
Old Generation
STW 발생
EMPTY
EMPTY EMPTY
G1 GC란? – Young 한 활동
Edan Space
Survivor Space
Old Generation
STW 발생
EMPTY
EMPTY EMPTY
G1 GC란? – Young 한 활동
GC &
EMPTY
GC &
EMPTY
GC &
EMPTY
GC &
EMPTY
EMPTY
Edan Space
Survivor Space
Old Generation
STW 발생
EMPTY
EMPTY EMPTY
G1 GC란? – Initial Mark
Edan Space
Survivor Space
Old Generation
STW 발생
EMPTY
EMPTY EMPTY
G1 GC란? – Root Region Scanning
Edan Space
Survivor Space
Old Generation
EMPTY
EMPTY EMPTY
G1 GC란? – Concurrent Marking
EMPTY EMPTY
Edan Space
Survivor Space
Old Generation
EMPTY
G1 GC란? – Remark
Edan Space
Survivor Space
Old Generation
STW 발생
EMPTY
G1 GC란? – Copying & Cleanup
Edan Space
Survivor Space
Old Generation
STW 발생
EMPTY
G1 GC란? – After Copying & Cleanup
Edan Space
Survivor Space
Old Generation
STW 발생
EMPTY
G1(Garbage First) GC는?
G1 GC는 장기적으로 문제가 많은 CMS GC를 대체하기 위해 만들어 졌다.
G1 GC의 가장큰 장점은 성능이다. 지금 까지의 어떤 GC보다 가장 빠르다!!
단언컨대 가장 빠릅니다.
G1(Garbage First) GC는 JDK9 부터 Default
Q&A?
참고
1. http://huelet.tistory.com/entry/JVM-
%EB%A9%94%EB%AA%A8%EB%A6%AC%EA%B5%AC%EC%A1%B0
2. http://hoonmaro.tistory.com/19
3. https://stackoverflow.com/questions/20563047/jdk-8-memory-layout-and-
garbage-collection
4. http://d2.naver.com/helloworld/1329
5. https://plumbr.io/handbook/garbage-collection-algorithms-implementations
6. https://www.slipp.net/wiki/pages/viewpage.action?pageId=26641949
7. http://12bme.tistory.com/57
8. http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.ht
ml
9. http://www.oracle.com/technetwork/tutorials/tutorials-1876574.html

More Related Content

Recently uploaded

Console API (Kitworks Team Study 백혜인 발표자료)
Console API (Kitworks Team Study 백혜인 발표자료)Console API (Kitworks Team Study 백혜인 발표자료)
Console API (Kitworks Team Study 백혜인 발표자료)
Wonjun Hwang
 
Merge (Kitworks Team Study 이성수 발표자료 240426)
Merge (Kitworks Team Study 이성수 발표자료 240426)Merge (Kitworks Team Study 이성수 발표자료 240426)
Merge (Kitworks Team Study 이성수 발표자료 240426)
Wonjun Hwang
 

Recently uploaded (6)

캐드앤그래픽스 2024년 5월호 목차
캐드앤그래픽스 2024년 5월호 목차캐드앤그래픽스 2024년 5월호 목차
캐드앤그래픽스 2024년 5월호 목차
 
MOODv2 : Masked Image Modeling for Out-of-Distribution Detection
MOODv2 : Masked Image Modeling for Out-of-Distribution DetectionMOODv2 : Masked Image Modeling for Out-of-Distribution Detection
MOODv2 : Masked Image Modeling for Out-of-Distribution Detection
 
Console API (Kitworks Team Study 백혜인 발표자료)
Console API (Kitworks Team Study 백혜인 발표자료)Console API (Kitworks Team Study 백혜인 발표자료)
Console API (Kitworks Team Study 백혜인 발표자료)
 
Continual Active Learning for Efficient Adaptation of Machine LearningModels ...
Continual Active Learning for Efficient Adaptation of Machine LearningModels ...Continual Active Learning for Efficient Adaptation of Machine LearningModels ...
Continual Active Learning for Efficient Adaptation of Machine LearningModels ...
 
A future that integrates LLMs and LAMs (Symposium)
A future that integrates LLMs and LAMs (Symposium)A future that integrates LLMs and LAMs (Symposium)
A future that integrates LLMs and LAMs (Symposium)
 
Merge (Kitworks Team Study 이성수 발표자료 240426)
Merge (Kitworks Team Study 이성수 발표자료 240426)Merge (Kitworks Team Study 이성수 발표자료 240426)
Merge (Kitworks Team Study 이성수 발표자료 240426)
 

Featured

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

GarbageCollection in JVM