--- id: native-memory-profiling title: Native Memory Profiling — iOS / Android leak 추적 category: Coding status: draft source_trust_level: B verification_status: conceptual created_at: 2026-05-09 updated_at: 2026-05-09 tags: [native, memory, profiling, leak, vibe-coding] tech_stack: { language: "Swift / Kotlin", applicable_to: ["iOS", "Android"] } applied_in: [] aliases: [Instruments Leaks, Android Profiler, LeakCanary, heap dump] --- # Native Memory Profiling > Mobile = 메모리 한정. Leak 한 번 = OOM crash. **iOS Instruments (Leaks/Allocations), Android Studio Profiler/LeakCanary** 가 표준. Closure capture 와 Listener 등록이 leak 단골. ## 📖 핵심 개념 - Heap: 객체 보관. - Leak: 참조가 안 끊겨 GC/ARC 가 회수 못 함. - Retain cycle: 두 객체가 서로 strong reference. ## 💻 코드 패턴 ### iOS — Instruments ```bash # Xcode → Product → Profile (⌘I) → Leaks 또는 Allocations # 시나리오 재현 → Instruments 가 cycle / 누수 표시. # Memory Graph Debugger: Xcode debug → Memory Graph 버튼. ``` ### iOS — 일반 leak 패턴 ```swift class VC: UIViewController { var data: DataSource! override func viewDidLoad() { // ❌ self 강한 참조 → cycle data.onUpdate = { items in self.tableView.reloadData() } // ✅ data.onUpdate = { [weak self] items in self?.tableView.reloadData() } // ❌ Timer 가 self 보유 Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { _ in self.tick() } // ✅ Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { [weak self] _ in self?.tick() } } } ``` ### Android — LeakCanary ```kotlin // build.gradle debugImplementation("com.squareup.leakcanary:leakcanary-android:2.13") ``` ```kotlin // 자동 leak detection. // Activity / Fragment / View / ViewModel 가 destroy 후에도 referenced 면 알림. ``` ### Android — 일반 leak 패턴 ```kotlin class MainActivity : AppCompatActivity() { // ❌ static — Application 살아있는 한 leak companion object { var instance: MainActivity? = null } // ❌ 익명 inner class 가 outer 보유 private val handler = Handler(Looper.getMainLooper()) override fun onCreate(s: Bundle?) { super.onCreate(s) handler.postDelayed({ doStuff() }, 60_000) // 1분 후, Activity 가 그 사이 종료되어도 leak } // ✅ removeCallbacks in onDestroy override fun onDestroy() { handler.removeCallbacksAndMessages(null) super.onDestroy() } } ``` ### Android Studio Profiler ``` View → Tool Windows → Profiler → Memory - Heap dump: 현재 객체 list - Allocation tracking: 시점별 allocation - Compare 2 heaps: 사이 늘어난 객체 ``` ### 비트맵 / 큰 자원 ```kotlin // 큰 image inSampleSize 로 축소 val opts = BitmapFactory.Options().apply { inSampleSize = 4 } val bmp = BitmapFactory.decodeFile(path, opts) // 사용 후 명시 recycle (안 해도 GC 처리하지만 빠름) bmp.recycle() ``` ## 🤔 의사결정 기준 | 증상 | 도구 | |---|---| | OOM crash | Heap dump → 가장 큰 객체 | | 점진적 메모리 증가 | Allocation tracking + 시점 비교 | | 화면 이동 시 leak | LeakCanary (Android) / Memory Graph (iOS) | | 큰 이미지 / 비디오 | inSampleSize / Glide / FastImage | | Background 메모리 누적 | LiveData/Flow stop, Coroutine cancel | ## ❌ 안티패턴 - **measure 없이 추측**: 시간 낭비. - **profiler off 상태로 release**: dev 만 보고 prod 다름. - **strong reference 묵시 가정**: closure / inner class / listener 모두 강한 참조 default. - **Singleton 에 Activity / Fragment / View 보관**: leak 보장. - **bitmap 그대로 메모리**: 1080p 이미지 = 8MB. 다수면 OOM. - **Job / Subscription cancel 안 함**: 백그라운드에서도 살아있음. ## 🤖 LLM 활용 힌트 - iOS = Instruments + [weak self]. Android = LeakCanary + lifecycle. - 큰 자원 = sampling / pooling. ## 🔗 관련 문서 - [[iOS_Swift_Memory_ARC_Cycles]] - [[Android_Lifecycle_Aware_Components]] - [[Native_Crash_Reporting]]