--- id: wiki-2026-0508-memory-management title: Memory Management category: 10_Wiki/Topics status: verified canonical_id: self aliases: [memory-mgmt, heap-management] duplicate_of: none source_trust_level: A confidence_score: 0.9 verification_status: applied tags: [memory, gc, performance, runtime, systems] raw_sources: [] last_reinforced: 2026-05-10 github_commit: pending tech_stack: language: C/JS/Rust framework: V8/JVM/glibc --- # Memory Management ## 매 한 줄 > **"매 program 매 lifetime 동안 의 allocation/deallocation 의 전략"**. 매 manual (C/C++) ↔ ARC (Swift/ObjC) ↔ tracing GC (V8/JVM) ↔ ownership (Rust) — 매 spectrum 의 각 trade-off — 매 2026 의 mainstream 4 가지 paradigm 의 공존. ## 매 핵심 ### 매 4 가지 paradigm 1. **Manual**: malloc/free, new/delete — 매 control 최대, 매 leak 위험. 2. **Reference counting**: ARC, shared_ptr — 매 deterministic, 매 cycle 문제. 3. **Tracing GC**: V8, JVM, .NET — 매 productivity, 매 pause. 4. **Ownership**: Rust borrow checker — 매 zero-runtime overhead, 매 learning curve. ### 매 핵심 metric - **RSS** (resident set size): 매 OS 시점. - **Heap used**: 매 runtime 시점. - **External**: 매 native buffer (Node `Buffer`). - **Fragmentation**: 매 allocate 가능하지만 매 contiguous block 부재. ### 매 응용 1. 매 long-running server 의 stability. 2. 매 game engine 의 frame budget. 3. 매 embedded 의 RAM 의 limit. ## 💻 패턴 ### 매 manual (C) ```c char *buf = malloc(1024); if (!buf) { perror("malloc"); exit(1); } // ... use ... free(buf); buf = NULL; // 매 dangling 의 방지 ``` ### 매 RAII (C++) ```cpp { std::unique_ptr p = std::make_unique(); // 매 scope exit 매 자동 delete } std::shared_ptr sp = std::make_shared(); // 매 ref-count 0 매 free ``` ### 매 ownership (Rust) ```rust fn take(s: String) { /* 매 drop on end */ } let owned = String::from("hi"); take(owned); // println!("{}", owned); // 매 compile error: moved ``` ### 매 tracing GC (JS) ```javascript let cache = new Map(); cache.set('a', { big: new Array(1e6) }); cache = null; // 매 GC 의 next cycle 의 reclaim // 매 WeakMap 매 key 의 GC 자동 cleanup const wm = new WeakMap(); ``` ### 매 Node memory inspect ```javascript const v8 = require('v8'); console.log(v8.getHeapStatistics()); // { total_heap_size: ..., used_heap_size: ..., heap_size_limit: ... } process.memoryUsage(); // { rss, heapTotal, heapUsed, external, arrayBuffers } ``` ### 매 leak detection (Node) ```bash node --inspect app.js # Chrome DevTools → Memory → Take snapshot → 매 sample 3 개 → comparison view node --heapsnapshot-signal=SIGUSR2 app.js kill -USR2 $(pgrep node) ``` ### 매 pool allocator ```cpp class Pool { std::vector free_; public: MyObj* acquire() { if (free_.empty()) return new MyObj(); auto* p = free_.back(); free_.pop_back(); return p; } void release(MyObj* p) { free_.push_back(p); } }; // 매 hot path 매 alloc 의 amortize ``` ### 매 arena (Rust bumpalo) ```rust use bumpalo::Bump; let arena = Bump::new(); let s = arena.alloc(String::from("hi")); let v = arena.alloc(vec![1, 2, 3]); // 매 arena drop 매 모두 free — 매 deallocation O(1) ``` ## 매 결정 기준 | 상황 | Approach | |---|---| | 매 systems / kernel | manual + sanitizer | | 매 high-perf game | RAII + pool | | 매 server productive | tracing GC + tune | | 매 safety + perf | Rust ownership | | 매 short-lived bulk | arena | **기본값**: 매 language idiom 따름 — 매 C++ RAII, 매 JS GC, 매 Rust ownership. ## 🔗 Graph - 부모: [[Performance]] - 변형: [[Garbage Collection]] · [[ARC]] · [[Ownership]] - 응용: [[V8 Engine Heap Management]] · [[Major GC]] - Adjacent: [[가비지 컬렉터(Garbage Collector)]] · [[힙 메모리(Heap Memory)]] ## 🤖 LLM 활용 **언제**: 매 memory leak / OOM 의 hunt 매 paradigm 의 선택. **언제 X**: 매 high-level business logic 의 memory 의 의식 안 해도 됨. ## ❌ 안티패턴 - **manual + GC mix 매 over-confidence**: 매 native buffer leak. - **shared_ptr cycle**: 매 weak_ptr 의 break. - **arena 매 long-lived**: 매 effective leak. - **Rust 매 unsafe 의 무분별**: 매 borrow checker 의 우회. ## 🧪 검증 / 중복 - Verified (V8/JVM/Rust/glibc 2026 documentation). - 신뢰도 A. ## 🕓 Changelog | 날짜 | 변경 | |---|---| | 2026-05-08 | Phase 1 | | 2026-05-10 | Manual cleanup — 4가지 메모리 관리 paradigm 비교 + pattern 정리 |