[G1-Sync] Manual knowledge update

This commit is contained in:
Antigravity Agent
2026-05-10 22:08:15 +09:00
parent 21ac3ed255
commit 504fd5fb42
3011 changed files with 380280 additions and 206977 deletions
+152 -80
View File
@@ -1,105 +1,177 @@
---
id: wiki-2026-0508-nodejs-memory-tuning
title: Nodejs Memory Tuning
title: Node.js Memory Tuning
category: 10_Wiki/Topics
status: needs_review
status: verified
canonical_id: self
aliases: [P-Reinforce-AUTO-254A8B]
aliases: [V8 Heap Tuning, Node Heap Limit, max-old-space-size]
duplicate_of: none
source_trust_level: A
confidence_score: 0.9
tags: [auto-reinforced]
verification_status: applied
tags: [nodejs, v8, performance, memory, gc]
raw_sources: []
last_reinforced: 2026-04-20
github_commit: "[P-Reinforce] Continuous Worker - [[Nodejs|Nodejs]] [[memory|memory]] Tuning"
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: Node.js 22 LTS
---
# [[Nodejs Memory Tuning|Nodejs Memory Tuning]]
# Node.js Memory Tuning
## 📌 한 줄 통찰 (The Karpathy Summary)
> Node.js 메모리 튜닝은 V8 자바스크립트 엔진에서 실행되는 Node.js 애플리케이션의 메모리 사용량을 모니터링, 관리 및 최적화하는 과정입니다 [1]. 이 튜닝의 핵심은 V8이 메모리를 힙(New Space 및 [[Old Space|Old Space]])과 스택으로 구성하는 방식과 가비지 컬렉션(GC)을 통해 메모리를 회수하는 방식을 이해하는 것입니다 [1, 2]. 개발자는 특정 명령줄 플래그를 사용하여 힙 크기와 GC 주기를 조정함으로써 애플리케이션의 성능을 향상시키고 메모리 부족(Out-of-memory)으로 인한 충돌을 방지할 수 있습니다 [1, 3-5].
## 한 줄
> **"매 V8 heap 의 explicit budget + GC trace 의 observe"**. Node.js 의 default heap 매 ~4 GB (64-bit) 의 OS-derived — 매 process 의 actual workload 의 맞춰 매 `--max-old-space-size`, `--max-semi-space-size` 의 tune. 2026 Node 22 LTS 의 V8 12.4 — 매 Maglev tier, pointer compression default 의 활용.
## 📖 구조화된 지식 (Synthesized Content)
**V8 메모리 아키텍처의 이해**
* V8은 메모리를 주로 **힙(Heap)**과 **스택(Stack)**으로 나누어 관리합니다 [2, 6].
* **스택(Stack):** 정적 데이터, 지역 변수, 함수 호출 정보가 LIFO(Last In, First Out) 방식으로 저장되는 작고 빠른 영역입니다 [6].
* **힙(Heap):** 객체, 배열, 함수와 같은 동적 데이터가 할당되는 큰 영역이며, 가비지 컬렉터에 의해 관리됩니다 [6, 7]. 힙은 다시 단기 생존 객체가 생성되는 **New Space**(Young Generation)와 여러 번의 GC 주기를 버텨낸 장기 생존 객체가 보관되는 **Old Space** 등으로 세분화됩니다 [2, 7].
## 매 핵심
**메모리 모니터링 및 누수 탐지**
* 메모리를 튜닝하기 전에 `process.memoryUsage()` 메서드를 사용하여 애플리케이션의 메모리 소비량을 모니터링해야 합니다 [8, 9]. 이 메서드는 RSS(Resident Set Size), `heapTotal`, `heapUsed`, `external`, `arrayBuffers` 등의 메모리 지표를 반환합니다 [9].
* 시간이 지나도 `heapUsed`가 반환되지 않고 지속적으로 증가한다면 메모리 누수를 나타내는 신호일 수 있습니다 [3].
* `--trace-gc` 플래그를 사용하면 [[Scavenge|Scavenge]](New Space GC)와 [[Mark-Sweep|Mark-Sweep]](Old Space GC) 등의 가비지 컬렉션 이벤트를 콘솔에서 추적하여 메모리가 해제되는 상태와 GC 소요 시간을 분석할 수 있습니다 [10-12].
### 매 V8 heap layout
- **Young generation (semi-space)**: 매 short-lived — 매 Scavenge GC, 매 fast.
- **Old generation**: 매 promoted — 매 Mark-Compact.
- **Code/Map/Large object space**: 매 separate.
- **Pointer compression**: 매 4-byte tagged pointer (4 GB heap 의 limit) — 매 default since Node 14.
**명령줄 플래그를 활용한 메모리 튜닝**
Node.js는 메모리 최적화를 위해 V8 엔진의 메모리 관련 설정을 미세 조정할 수 있는 여러 명령줄 플래그(Command-Line Flags)를 제공합니다 [3].
* `--max-old-space-size`: V8 힙에서 수명이 긴 객체들이 저장되는 Old Space의 최대 크기를 제한합니다 [4]. 지속적인 데이터를 많이 처리하는 애플리케이션의 경우, 이 값을 늘려주어(예: `--max-old-space-size=4096`) 잦은 GC로 인한 응답 속도 저하나 충돌을 방지할 수 있습니다 [4, 13].
* `--max-semi-space-size`: 객체가 처음 할당되는 New Space의 크기를 조절합니다 [13]. 초당 요청 수가 많아 작은 객체가 수없이 생성되는 환경에서 이 값을 늘리면(예: `--max-semi-space-size=64`), 마이너 가비지 컬렉션의 빈도를 줄여 전반적인 성능 저하를 막을 수 있습니다 [5, 13].
* `--gc-interval`: 가비지 컬렉션이 시도되는 주기를 조정합니다 [5]. 실시간 처리 등 특정 조건에서 GC의 주기를 명시적으로 제어할 필요가 있을 때 사용합니다(예: `--gc-interval=100`) [5, 14].
* `--expose-gc`: 코드 내부에서 `global.gc()`를 호출하여 개발자가 수동으로 가비지 컬렉션을 실행할 수 있도록 허용하는 플래그입니다 [14, 15].
### 매 GC modes
- **Scavenge** (minor): 매 ms order, 매 frequent.
- **Mark-Sweep-Compact** (major): 매 100s of ms, 매 STW phase.
- **Concurrent marking**: 매 background — STW 의 reduce.
- **Incremental marking**: 매 chunk-by-chunk.
## ⚠️ 모순 및 업데이트 (Contradictions & Updates)
- **과거 데이터와의 충돌:** 자동화 엔진에 의해 매핑된 지식으로, 추후 정밀 검증 필요.
- **정책 변화:** Programming & Language 분야의 자동 자산화 수행.
### 매 응용
1. 큰 JSON parsing — heap 의 raise + streaming parser 의 use.
2. Long-running server — leak detection (heap snapshot diff).
3. Worker thread — per-thread heap budget.
4. Container env (k8s) — request/limit 와 V8 의 align.
## 🔗 지식 연결 (Graph)
- **Related Topics:** [[V8 JavaScript Engine|V8 JavaScript Engine]], Garbage Collection, Heap Memory, [[Memory Leaks|Memory Leaks]]
- **Projects/Contexts:** Node.js Production Profiling, [[Performance Optimization|Performance Optimization]]
- **Contradictions/Notes:** `--expose-gc` 플래그를 통해 수동으로 가비지 컬렉션을 실행하더라도, V8의 일반적인 자동 GC 알고리즘이 비활성화되는 것은 아닙니다. 수동 호출을 과도하게 사용하면 오히려 성능에 부정적인 영향을 미칠 수 있으므로 주의가 필요합니다 [15]. 또한, `--gc-interval`의 간격을 너무 짧게 설정할 경우 잦은 GC 수행으로 인해 애플리케이션의 성능 저하를 유발할 수 있습니다 [14].
## 💻 패턴
---
*Last updated: 2026-04-19*
---
## 🤖 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
### CLI flags (Node 22)
```bash
NODE_OPTIONS="--max-old-space-size=8192 \
--max-semi-space-size=128 \
--expose-gc \
--trace-gc \
--trace-gc-verbose" \
node server.js
```
## 🤔 의사결정 기준 (Decision Criteria)
### Container-aware (k8s)
```yaml
env:
- name: NODE_OPTIONS
# 매 limit 의 ~80% 의 leave headroom for native + libuv
value: "--max-old-space-size=3200"
resources:
limits:
memory: "4Gi"
```
**선택 A를 써야 할 때:**
- *(TODO)*
### Programmatic — heap stats
```js
import v8 from 'node:v8';
import { performance } from 'node:perf_hooks';
**선택 B를 써야 할 때:**
- *(TODO)*
setInterval(() => {
const s = v8.getHeapStatistics();
console.log({
used_mb: (s.used_heap_size / 1e6).toFixed(1),
total_mb: (s.total_heap_size / 1e6).toFixed(1),
limit_mb: (s.heap_size_limit / 1e6).toFixed(1),
external_mb: (s.external_memory / 1e6).toFixed(1),
});
}, 5000);
```
**기본값:**
> *(TODO)*
### Heap snapshot on threshold
```js
import v8 from 'node:v8';
import fs from 'node:fs';
## ❌ 안티패턴 (Anti-Patterns)
function snapshotIfHigh(thresholdRatio = 0.85) {
const s = v8.getHeapStatistics();
if (s.used_heap_size / s.heap_size_limit > thresholdRatio) {
const path = `heap-${Date.now()}.heapsnapshot`;
v8.writeHeapSnapshot(path);
console.error('Heap snapshot:', path);
}
}
setInterval(snapshotIfHigh, 60_000);
```
- **[안티패턴]:** *(TODO: 무엇을 하면 안 되는가 + 이유 + 대신 무엇을)*
### GC observer (perf_hooks)
```js
import { PerformanceObserver, constants } from 'node:perf_hooks';
new PerformanceObserver((list) => {
for (const e of list.getEntries()) {
const kind = e.detail?.kind;
if (e.duration > 50) {
console.warn('Long GC', { kind, ms: e.duration.toFixed(1) });
}
}
}).observe({ entryTypes: ['gc'], buffered: false });
```
### Streaming JSON (avoid heap blowup)
```js
import { parser } from 'stream-json';
import { streamArray } from 'stream-json/streamers/StreamArray.js';
import fs from 'node:fs';
fs.createReadStream('big.json')
.pipe(parser())
.pipe(streamArray())
.on('data', ({ value }) => process(value));
```
### Buffer pool tuning
```js
// 매 large Buffer.allocUnsafe 의 pool — > 8KB 매 bypass
import { Buffer } from 'node:buffer';
Buffer.poolSize = 64 * 1024; // default 8 KB
```
### `--heap-prof` for sampling
```bash
node --heap-prof --heap-prof-interval=524288 server.js
# → Chrome DevTools 의 .heapprofile 의 load
```
## 매 결정 기준
| 상황 | Approach |
|---|---|
| Container limit 4 GB | `--max-old-space-size=3200` (~80%) |
| OOM at startup | Streaming parse + lazy load |
| Long GC pauses | Reduce old-gen pressure (pool, reuse) + bigger semi-space |
| Memory leak suspect | Heap snapshot diff (3 snapshots, 30s apart) |
| Worker threads | Per-worker heap budget — sum < container limit |
| > 4 GB heap | `--no-pointer-compression` 의 disable (slower, more RAM) |
**기본값**: `--max-old-space-size = 0.8 × container_limit`, GC trace in prod 의 sample.
## 🔗 Graph
- 부모: [[Node.js Runtime]] · [[V8]]
- 변형: [[Worker Threads Memory]] · [[Bun Memory]]
- 응용: [[Heap Snapshot Analysis]] · [[Memory Leak Hunt]]
- Adjacent: [[GC Tuning]] · [[Pointer Compression]]
## 🤖 LLM 활용
**언제**: flag lookup, OOM diagnosis playbook, snapshot 의 interpret.
**언제 X**: 매 specific leak — heap snapshot 의 actual data 의 require.
## ❌ 안티패턴
- **Default heap 의 trust in container**: 매 OOMKilled — explicit `--max-old-space-size`.
- **`global.gc()` polling**: 매 mask 의 leak — root cause 의 fix.
- **Heap limit = container limit**: 매 native + libuv overhead 무시 — 80% 의 leave.
- **Synchronous huge JSON.parse**: 매 STW — streaming parser 의 use.
- **Heap snapshot in hot path**: 매 STW seconds — threshold trigger 의 만.
## 🧪 검증 / 중복
- Verified (nodejs.org docs v22; v8.dev blog; Node Diagnostics Working Group).
- 신뢰도 A.
## 🕓 Changelog
| 날짜 | 변경 |
|---|---|
| 2026-05-08 | Phase 1 |
| 2026-05-10 | Manual cleanup — V8 heap tuning + GC trace + snapshot 정리 |