Skip to content

Commit

Permalink
feat: garbage collection
Browse files Browse the repository at this point in the history
  • Loading branch information
eunbae0 committed Feb 15, 2024
1 parent db9345f commit dac4158
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions docs/basics/javascript/garbage_collection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# 가비지 컬렉터

> 더이상 사용되지 않는 메모리를 해제하는 기능
- Javascript는 가비지 컬렉터를 내장하는 **매니지드 언어**로, **메모리 누수를 방지**한다.

- 자바스크립트는 *도달 가능성(reachability)* 이라는 개념을 사용해 메모리 관리를 수행한다.

## 가비지 컬렉터 알고리즘?

> **마크 앤 스위프 알고리즘**
- 가비지 컬렉터는 루트(root) 정보를 수집하고 이를 **mark(기억)** 한다.
- 루트가 참조하고 있는 모든 객체를 방문하고 이것들을 **mark** 한다.
- mark 된 모든 객체에 방문하고 그 객체들이 참조하는 객체도 mark 합니다. 한번 방문한 객체는 전부 mark 하기 때문에 같은 객체를 다시 방문하는 일은 없다.
- 루트에서 도달 가능한 모든 객체를 방문할 때까지 위 과정을 반복한다.
- **mark 되지 않은 모든 객체를 메모리에서 삭제한다.**

:::tip
루트(root)에 페인트를 붓는다고 생각하면 쉽다.
페인트가 부어지지 않는 객체를 삭제하는 것.
:::

## 더 빠르게 동작하도록하는 다양한 최적화 기법

- **generational collection(세대별 수집)**, **incremental collection(점진적 수집)**, **idle-time collection(유휴 시간 수집)** 등이 있다. 자세한 내용은 참고문서 참조.

## 참고문서

https://ko.javascript.info/garbage-collection

0 comments on commit dac4158

Please sign in to comment.