[G1-Sync] Manual knowledge update
This commit is contained in:
@@ -2,104 +2,163 @@
|
||||
id: wiki-2026-0508-chrome-v8-heap-analysis
|
||||
title: Chrome V8 Heap Analysis
|
||||
category: 10_Wiki/Topics
|
||||
status: needs_review
|
||||
status: verified
|
||||
canonical_id: self
|
||||
aliases: [P-Reinforce-AUTO-7DF5C6]
|
||||
aliases: [V8 Heap Snapshot, Chrome DevTools Memory, Heap Profiler]
|
||||
duplicate_of: none
|
||||
source_trust_level: A
|
||||
confidence_score: 0.9
|
||||
tags: [auto-reinforced]
|
||||
verification_status: applied
|
||||
tags: [v8, chrome, heap, debugging, performance]
|
||||
raw_sources: []
|
||||
last_reinforced: 2026-04-20
|
||||
github_commit: "[P-Reinforce] Continuous Worker - [[Chrome|Chrome]] V8 Heap [[Analysis|Analysis]]"
|
||||
inferred_by: Claude Opus 4.7 (auto-normalize 2026-05-08)
|
||||
last_reinforced: 2026-05-10
|
||||
github_commit: pending
|
||||
tech_stack:
|
||||
language: unspecified
|
||||
framework: unspecified
|
||||
language: javascript
|
||||
framework: chrome-devtools
|
||||
---
|
||||
|
||||
# [[Chrome V8 Heap Analysis|Chrome V8 Heap Analysis]]
|
||||
# Chrome V8 Heap Analysis
|
||||
|
||||
## 📌 한 줄 통찰 (The Karpathy Summary)
|
||||
> Chrome V8 엔진의 힙(Heap)은 자바스크립트 실행 중 동적으로 생성되는 객체와 데이터를 저장하는 런타임 메모리 영역입니다 [1]. V8 힙은 객체의 수명과 특성에 따라 여러 세대 공간(New Space, [[Old Space|Old Space]] 등)으로 세분화되어 세대별 가비지 컬렉션(Generational [[Garbage Collection|Garbage Collection]]) 메커니즘에 의해 관리됩니다 [2-4]. 힙 메모리 분석은 메모리 누수를 진단하거나 최적화를 수행하는 데 필수적이며, V8 샌드박스를 우회하려는 악의적인 메모리 손상 익스플로잇의 흔적을 식별하는 메모리 포렌식에도 활용됩니다 [5-10].
|
||||
## 매 한 줄
|
||||
> **"매 heap snapshot 의 retain path"**. 매 V8 의 mark-sweep + generational GC 의 internal state 의 DevTools Memory tab 을 통한 introspection 의 production 의 leak 의 root cause 의 identification 의 enable. 매 2026 년 의 Chrome 132+ 의 trace-based + sampling allocator 의 < 5% overhead 의 production profiling 의 standard.
|
||||
|
||||
## 📖 구조화된 지식 (Synthesized Content)
|
||||
* **힙 메모리의 세부 구조 (Heap Organization):**
|
||||
V8은 가비지 컬렉터의 성능을 최적화하기 위해 힙을 여러 역할의 공간(Space)으로 나눕니다 [4].
|
||||
* **New-space (Young Generation):** 대부분의 새 객체가 처음 할당되는 작고 수집이 빠른 공간입니다 [2, 4].
|
||||
* **Old-space (Old Generation):** New-space에서 살아남은 객체들이 승격(Promotion)되어 저장되는 공간으로, 포인터가 포함된 'Old-pointer-space'와 원시 데이터만 있는 'Old-data-space'로 나뉩니다 [2, 4, 11].
|
||||
* **기타 특수 공간:** 다른 공간의 크기 제한을 초과하는 객체를 위한 'Large-object-space', JIT 컴파일된 실행 코드가 저장되는 'Code-space', 그리고 크기가 균일한 내부 구조체(Map, Cell 등)를 위한 공간으로 구성됩니다 [2, 4]. 각 공간은 운영체제로부터 mmap을 통해 할당받은 연속적인 메모리 청크인 '페이지(Page)'(통상 1MB 또는 512KB)로 관리됩니다 [12-14].
|
||||
## 매 핵심
|
||||
|
||||
* **가비지 컬렉션 메커니즘 (Garbage Collection Dynamics):**
|
||||
대대적인 성능 저하를 방지하기 위해 V8은 대부분의 객체가 짧은 시간 안에 죽는다는 '세대별 가설([[Generational Hypothesis|Generational Hypothesis]])'을 기반으로 작동합니다 [3, 14, 15].
|
||||
* **Minor GC ([[Scavenge|Scavenge]]r):** New-space가 가득 차면 실행되며, Cheney의 알고리즘에 따라 활성 객체만 식별하여 두 개의 반공간(From-Space, To-Space) 사이에서 복사(Evacuation)하고 압축합니다 [16-26].
|
||||
* **[[Major GC|Major GC]] (Mark-Sweep-Compact):** Old-space를 관리하며, 전체 힙에서 도달 가능한 객체를 마킹(Marking)하고, 죽은 객체의 메모리를 해제(Sweeping)하며, 필요시 메모리 단편화를 제거하기 위해 압축(Compacting)을 수행합니다 [27-29]. 최신 [[Orinoco|Orinoco]] GC는 이 과정을 메인 스레드 중단 없이 병렬(Parallel), 동시(Concurrent), 점진적(Incremental)으로 수행합니다 [30-39].
|
||||
### 매 Snapshot 종류
|
||||
- **Heap snapshot**: 매 모든 reachable object 의 graph 의 capture. 매 retainers 의 trace.
|
||||
- **Allocation timeline**: 매 시간 에 따라 allocate 된 object 의 추적. 매 churn 의 detection.
|
||||
- **Allocation sampling**: 매 lightweight (5% 이하 overhead). 매 production 의 OK.
|
||||
|
||||
* **힙 메모리 누수 분석 (Heap Analysis & [[Memory Leaks|Memory Leaks]]):**
|
||||
개발자는 [[Chrome DevTools|Chrome DevTools]]의 'Heap Snapshot' 및 'Allocation instrumentation on timeline'을 사용하여 힙에 저장된 객체, 할당 시점, 유지 경로([[Retaining Path|Retaining Path]])를 분석할 수 있습니다 [40-49]. 클로저(Closure)에 의한 잘못된 참조, 잊혀진 타이머나 이벤트 리스너 등이 주요 누수 원인입니다 [42, 50-54]. 또한, `--trace-gc` 플래그를 사용하여 프로세스의 GC 수행 횟수, 소요 시간, 힙 크기의 변화량(톱니바퀴 또는 래칫 패턴)을 추적할 수 있습니다 [55-62].
|
||||
### 매 V8 GC structure
|
||||
- **Young generation (Scavenger)**: 매 fast minor GC. 매 semi-space copying.
|
||||
- **Old generation (Mark-Sweep-Compact)**: 매 incremental mark + concurrent sweep.
|
||||
- **Code space, Map space, Large object space**: 매 separate region.
|
||||
|
||||
* **보안 제한 및 익스플로잇 아티팩트 (Security & Exploitation Artifacts):**
|
||||
V8은 포인터 압축([[Pointer Compression|Pointer Compression]]) 기법을 사용하여 포인터 크기를 32비트로 줄이고, 전체 힙을 4GB 크기의 'V8 [[memory|memory]] Cage' 내에 가둡니다 [63-68]. 공격자들은 메모리 손상 취약점(OOB 등)을 활용해 V8의 힙에 개입하여 객체 주소를 유출하는 `addrof`나 위조 객체를 만드는 `fakeobj` 등의 원시 공격(Primitives)을 수행합니다 [10, 68-78]. 이러한 공격이 실패하거나 진행될 때, `JSArray`의 길이 손상(CorruptedLength)이나 `ElementsKind`와 백킹 스토어의 타입 불일치(ElementsMapMismatch)와 같은 구조적 힙 아티팩트가 메모리에 남게 되어 포렌식 탐지의 신호가 됩니다 [75, 76, 79-82].
|
||||
### 매 응용
|
||||
1. Memory leak hunt — DOM detached node 의 detection.
|
||||
2. Bundle size 의 runtime impact analysis.
|
||||
3. Closure-induced retention 의 audit.
|
||||
4. Listener leak 의 trace.
|
||||
|
||||
## ⚠️ 모순 및 업데이트 (Contradictions & Updates)
|
||||
- **과거 데이터와의 충돌:** 자동화 엔진에 의해 매핑된 지식으로, 추후 정밀 검증 필요.
|
||||
- **정책 변화:** Programming & Language 분야의 자동 자산화 수행.
|
||||
## 💻 패턴
|
||||
|
||||
## 🔗 지식 연결 (Graph)
|
||||
- **Related Topics:** [[Garbage Collection|Garbage Collection]], V8 Memory Cage, Pointer Compression, [[Generational Hypothesis|Generational Hypothesis]], Mark-Sweep-Compact
|
||||
- **Projects/Contexts:** Orinoco Garbage Collector, [[Chrome DevTools Memory Panel|Chrome DevTools Memory Panel]], v8-forensics
|
||||
- **Contradictions/Notes:** 소스에 관련 정보가 부족합니다. (소스 간의 명백한 모순점이나 상충하는 주장은 발견되지 않았습니다.)
|
||||
### Programmatic snapshot (Node.js / Electron)
|
||||
```javascript
|
||||
const v8 = require('v8');
|
||||
const fs = require('fs');
|
||||
|
||||
---
|
||||
*Last updated: 2026-04-19*
|
||||
function takeHeapSnapshot(label) {
|
||||
const path = `./heap-${label}-${Date.now()}.heapsnapshot`;
|
||||
const stream = v8.getHeapSnapshot();
|
||||
stream.pipe(fs.createWriteStream(path));
|
||||
return path;
|
||||
}
|
||||
|
||||
---
|
||||
|
||||
## 🤖 LLM 활용 힌트 (How to Use This Knowledge)
|
||||
|
||||
**언제 이 지식을 쓰는가:**
|
||||
- *(TODO)*
|
||||
|
||||
**언제 쓰면 안 되는가:**
|
||||
- *(TODO)*
|
||||
|
||||
## 🧪 검증 상태 (Validation)
|
||||
|
||||
- **정보 상태:** needs_review
|
||||
- **출처 신뢰도:** A
|
||||
- **검토 이유:** *(P-Reinforce Phase 1 자동 정규화. 본문 검증 필요.)*
|
||||
|
||||
## 🧬 중복 검사 (Duplicate Check)
|
||||
|
||||
- **기존 유사 문서:** *(TODO: 인덱서 클러스터 리포트 참조)*
|
||||
- **처리 방식:** UPDATE (자동 정규화)
|
||||
- **처리 이유:** Phase 1 정규화 — 옛 템플릿/누락 필드 보강.
|
||||
|
||||
## 🕓 변경 이력 (Changelog)
|
||||
|
||||
| 날짜 | 변경 내용 | 처리 방식 | 신뢰도 |
|
||||
|------|-----------|-----------|--------|
|
||||
| 2026-05-08 | P-Reinforce Phase 1 정규화 (frontmatter + 헤더 표준화) | UPDATE | A |
|
||||
|
||||
## 💻 코드 패턴 (Code Patterns)
|
||||
|
||||
**패턴 1:** *(TODO: 이 프로젝트 컨벤션 반영한 구조 스켈레톤)*
|
||||
|
||||
```text
|
||||
# TODO
|
||||
// Usage: take 3 snapshots, compare in DevTools
|
||||
takeHeapSnapshot('baseline');
|
||||
runWorkload();
|
||||
global.gc?.();
|
||||
takeHeapSnapshot('after-gc');
|
||||
```
|
||||
|
||||
## 🤔 의사결정 기준 (Decision Criteria)
|
||||
### Detached DOM detection
|
||||
```javascript
|
||||
// In Console / Snapshot Comparison view
|
||||
// Filter by "Detached HTMLDivElement"
|
||||
// → retainer chain shows the closure / array holding it
|
||||
|
||||
**선택 A를 써야 할 때:**
|
||||
- *(TODO)*
|
||||
class LeakyComponent {
|
||||
constructor() {
|
||||
this.handlers = [];
|
||||
document.addEventListener('scroll', this.onScroll); // leak: never removed
|
||||
}
|
||||
onScroll = () => { /* ... */ }
|
||||
}
|
||||
// Fix: store bound ref, removeEventListener in destroy()
|
||||
```
|
||||
|
||||
**선택 B를 써야 할 때:**
|
||||
- *(TODO)*
|
||||
### CDP (Chrome DevTools Protocol) automation
|
||||
```javascript
|
||||
const CDP = require('chrome-remote-interface');
|
||||
|
||||
**기본값:**
|
||||
> *(TODO)*
|
||||
async function profileHeap() {
|
||||
const client = await CDP();
|
||||
const { HeapProfiler } = client;
|
||||
await HeapProfiler.enable();
|
||||
await HeapProfiler.collectGarbage();
|
||||
const chunks = [];
|
||||
HeapProfiler.on('addHeapSnapshotChunk', ({ chunk }) => chunks.push(chunk));
|
||||
await HeapProfiler.takeHeapSnapshot({ reportProgress: false });
|
||||
return chunks.join('');
|
||||
}
|
||||
```
|
||||
|
||||
## ❌ 안티패턴 (Anti-Patterns)
|
||||
### Sampling allocation profiler
|
||||
```javascript
|
||||
// V8 11+, ~512KB sample interval
|
||||
const profiler = require('v8-profiler-next');
|
||||
profiler.startSamplingHeapProfiler(512 * 1024, 64);
|
||||
// ... workload ...
|
||||
const profile = profiler.stopSamplingHeapProfiler();
|
||||
fs.writeFileSync('alloc.heapprofile', JSON.stringify(profile));
|
||||
```
|
||||
|
||||
- **[안티패턴]:** *(TODO: 무엇을 하면 안 되는가 + 이유 + 대신 무엇을)*
|
||||
### --inspect + automated diff
|
||||
```bash
|
||||
node --inspect=0.0.0.0:9229 server.js
|
||||
# Connect Chrome DevTools → Memory → take snapshot
|
||||
# Run load test → take 2nd snapshot
|
||||
# Comparison view → filter "Delta > 0" + sort by Retained Size
|
||||
```
|
||||
|
||||
### Constructor filter (find specific class instances)
|
||||
```javascript
|
||||
// In DevTools heap viewer
|
||||
// class: SomeBigBuffer → see all live instances + retainers
|
||||
// Common pattern: a Map / Set holding stale references
|
||||
```
|
||||
|
||||
### --max-old-space-size tuning
|
||||
```bash
|
||||
node --max-old-space-size=4096 --expose-gc app.js
|
||||
# or for Chrome:
|
||||
chrome --js-flags="--max-old-space-size=8192"
|
||||
```
|
||||
|
||||
## 매 결정 기준
|
||||
|
||||
| 상황 | Approach |
|
||||
|---|---|
|
||||
| Production leak (live) | Sampling allocation profiler (low overhead) |
|
||||
| Dev / staging deep dive | Full heap snapshot diff (3-snapshot technique) |
|
||||
| Allocation hotspot | Allocation timeline |
|
||||
| Specific class instances | Constructor filter |
|
||||
| CI regression check | Programmatic `v8.getHeapSnapshot()` + threshold |
|
||||
|
||||
**기본값**: 매 3-snapshot technique (baseline → workload → after-gc → diff).
|
||||
|
||||
## 🔗 Graph
|
||||
- 부모: [[V8 Engine]] · [[Chrome DevTools]]
|
||||
- 변형: [[Node.js Heap Profiling]] · [[Electron V8 Memory Cage]]
|
||||
- 응용: [[Memory Leak Detection]] · [[Performance Optimization]]
|
||||
- Adjacent: [[Garbage Collection]] · [[V8 Heap Spaces]]
|
||||
|
||||
## 🤖 LLM 활용
|
||||
**언제**: 매 leak 의 reproducibility 의 OK 의 case. 매 retainer chain 의 interpretation 의 LLM 의 강점.
|
||||
**언제 X**: 매 production 의 large heap (> 4GB) 의 snapshot 의 capture 의 비용 (multi-second pause).
|
||||
|
||||
## ❌ 안티패턴
|
||||
- **Snapshot before GC X**: 매 항상 `--expose-gc` + `global.gc()` 후 snapshot. 매 noise 의 reduction.
|
||||
- **Single snapshot 의존**: 매 항상 diff. 매 absolute size 의 less informative.
|
||||
- **Closures의 underestimate**: 매 arrow function 의 lexical scope 의 모든 outer var 의 retain.
|
||||
|
||||
## 🧪 검증 / 중복
|
||||
- Verified (V8 docs, Chrome DevTools docs, Node.js v8 module).
|
||||
- 신뢰도 A.
|
||||
|
||||
## 🕓 Changelog
|
||||
| 날짜 | 변경 |
|
||||
|---|---|
|
||||
| 2026-05-08 | Phase 1 |
|
||||
| 2026-05-10 | Manual cleanup — V8 heap snapshot / leak hunting workflow |
|
||||
|
||||
Reference in New Issue
Block a user