종류
Minor GC
- 새로 생성된 객체는 Eden 영역에 위치하게 된다
- Eden 영역에서 GC가 발생한 후 살아남은 객체는 Survivor 영역 중 하나로 이동된다
- 이 과정을 반복한 후 살아남은 객체는 일정시간 참조되고 있다고 보고, Old 영역으로 이동된다
Major GC
- Old 영역에 있는 객체들을 검사하여 참조되지 않은 객체는 삭제한다
- 시간도 오래 걸리고 실행 프로세스가 중지되는데 이를 Stop-The-World라 한다
- GC 작업이 완료되고 나서야 이후 중단된 작업을 다시 시작하게 된다
동작
GC 대상 찾기
- 알고리즘에 따라 다양한 방식이 있겠지만 공통된 원리가 있는데
- GC는 힙 내 객체 중에서 Garbage를 찾아내고 처리하여 힙의 메모리를 회수한다
- 참조되지 않은 객체를 Garbage라 하고 이를 판단하기 위해 Reachability라는 개념을 사용한다
- 힙 영역에 할당된 객체가 유효한 참조가 있다면 Reachability, 없으면 Unreachability