Skip to content

Commit

Permalink
Update 2023-11-06-javaGC.md
Browse files Browse the repository at this point in the history
  • Loading branch information
stoneHee99 authored Nov 21, 2023
1 parent d7f6c3a commit 936b169
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions _posts/2023-11-06-javaGC.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ title: 자바의 가비지 컬렉션
author: 박석희
categories: GC
banner:
image: https://raw.githubusercontent.com/Kernel360/blog-image/main/1106/thumb_gc.png
image: https://raw.githubusercontent.com/Kernel360/blog-image/main/20231106/thumb_gc.png
background: "#000"
height: "100vh"
min_height: "38vh"
Expand Down Expand Up @@ -77,13 +77,13 @@ member.setName("Park SeokHee");
이를 해결하기 위해 C나 C++ 같은 언어에선 개발자가 직접 메모리에 할당 해제 합니다.

이는 효율적으로 메모리를 관리하도록 프로그램을 개발하게 도와주지만 반대로 이것이 잘되지 않았을 경우 프로그램 크래시를 일으키기도 합니다.
![Untitled](https://raw.githubusercontent.com/Kernel360/blog-image/main/1106/1.png)
![Untitled](https://raw.githubusercontent.com/Kernel360/blog-image/main/20231106/1.png)

자바에서는 JVM의 가비지 콜렉터가 이 역할을 자동으로 수행합니다.

그래서 개발자는 객체의 할당을 신경쓰지 않고 비즈니스 로직 그 자체에 집중해서 개발을 할 수 있도록 도와줍니다.

![Untitled](https://raw.githubusercontent.com/Kernel360/blog-image/main/1106/2.png)
![Untitled](https://raw.githubusercontent.com/Kernel360/blog-image/main/20231106/2.png)

JVM은 객체나 동적 데이터를 JVM 메모리 내의 힙 영역에 저장합니다.

Expand All @@ -101,7 +101,7 @@ JVM 메모리 영역 중 가장 큰 블록이고 가비지 콜렉션이 이루

`Mark` 는 살아있는 객체를 찾는 과정입니다.

![Untitled](https://raw.githubusercontent.com/Kernel360/blog-image/main/1106/3.png)
![Untitled](https://raw.githubusercontent.com/Kernel360/blog-image/main/20231106/3.png)

파란색으로 표시된 객체는 닿을 수 있는 객체(reachable), 붉은색으로 표시된 객체는 닿을 수 없는 객체(unreachable) 입니다. 한때는 참조되었지만 지금은 범위를 벗어난 객체죠.

Expand All @@ -113,7 +113,7 @@ GC root 는 Stack 영역의 로컬 변수 / 메서드 영역의 static 변수 /

그래서 GC의 동작 방식을 `Mark And Sweep` 이라고 합니다.

![Untitled](https://raw.githubusercontent.com/Kernel360/blog-image/main/1106/4.png)
![Untitled](https://raw.githubusercontent.com/Kernel360/blog-image/main/20231106/4.png)

JVM 의 Heap 영역은 2가지 전제를 두고 설계 되었습니다.

Expand All @@ -134,29 +134,29 @@ Young generation 영역에서 일어나는 가비지 콜렉션을 `Minor GC` , O

Young generation 영역은 `Eden` 영역과 `Survivor` 영역으로 나누어지는데, 에덴 영역은 새로 생성된 객체가 저장되는 영역이고 서바이버 영역은 에덴 영역에서 살아남은 객체가 저장되는 곳입니다.

![Untitled](https://raw.githubusercontent.com/Kernel360/blog-image/main/1106/5.png)
![Untitled](https://raw.githubusercontent.com/Kernel360/blog-image/main/20231106/5.png)

우선 객체가 새로 생성될 때마다 에덴 영역에 계속해서 생성되고, 어느 순간 에덴 영역이 꽉 차게 되면 그 때 Minor GC가 발생하게 됩니다.

![Untitled](https://raw.githubusercontent.com/Kernel360/blog-image/main/1106/6.png)
![Untitled](https://raw.githubusercontent.com/Kernel360/blog-image/main/20231106/6.png)

이때 마크 앤 스윕이 일어나고 살아남은 객체는 Survivor 영역으로 옮겨집니다.

그리고 Survivor 에 할당된 객체에는 age가 1 증가하게 됩니다.

![Untitled](https://raw.githubusercontent.com/Kernel360/blog-image/main/1106/7.png)
![Untitled](https://raw.githubusercontent.com/Kernel360/blog-image/main/20231106/7.png)

다시한번 같은 과정을 통해 에덴이 꽉 차게되고 마크앤 스윕이 일어나면 서바이버영역에 있던 객체와 gc로부터 살아남은 객체가 또 다른 서바이버 영역으로 이동합니다.

![Untitled](https://raw.githubusercontent.com/Kernel360/blog-image/main/1106/8.png)
![Untitled](https://raw.githubusercontent.com/Kernel360/blog-image/main/20231106/8.png)

이는 heap fragment(힙 메모리 단편화) 문제를 막기 위함인데요.

Sweep된 객체들의 메모리 주소가 연속되지 않기 때문에, 충분한 자리가 있음에도 객체가 할당되지 못하는 현상을 말합니다. 이를 방지하기 위해, 모든 살아남은 객체를 한 쪽으로 모아서 빈 공간을 모으는 동작을 하게 됩니다.

이를 `Mark, Sweep and Compact` 라고 하기도 합니다.

![Untitled](https://raw.githubusercontent.com/Kernel360/blog-image/main/1106/9.png)
![Untitled](https://raw.githubusercontent.com/Kernel360/blog-image/main/20231106/9.png)

이후 age 값이 임계값에 달하면 (default: 31) old generation 영역으로 이동합니다. 이후 이 영역도 꽉 차면 Old generation을 대상으로 발생하는 Major GC가 발생합니다.

Expand Down

0 comments on commit 936b169

Please sign in to comment.