Files
2nd/10_Wiki/Topic_Programming/Coding/Native_Memory_Profiling.md
T
Antigravity Agent 9148c358d0 docs(10_Wiki): 위키 전체 재구성 — Topic_* 폴더를 4개 카테고리로 통합 + 대규모 중복 제거
Topic_Agent/Topic_Blog/Topics/Topics_Biz/Topics_Meeting/Topics_Rag의 마크다운 지식 문서를
Topic_General/Topic_Programming/Topic_Graphic/Topic_Business 4개 카테고리로 재분류.

- 중복 제거: frontmatter의 status:duplicate/merged + duplicate_of/redirect_to 필드로
  자기 자신을 중복으로 선언한 리다이렉트 stub 1032개 제거, 완전 동일 내용 파일 472개 제거,
  동일 파일명·다른 내용 충돌 시 더 큰(완전한) 버전만 유지(162개 제거) — 총 1639개 중복 제거.
- 분류: 폴더 단위로 명확한 항목(AI_and_ML/Coding/Architecture 등 → Programming,
  Comfyui/Visual_Effects → Graphic, Topics_Biz/Topics_Meeting/사업 등 → Business,
  Poetic_Blog_Writing/창의성/Game_Design 등 → General)은 폴더 우선순위로,
  나머지 혼재 폴더(Topic_Agent/Topic_Blog/Topics 루트/Thinking & Reasoning/Other/UI_UX_Assets)는
  title/tags 키워드 스코어링으로 파일 단위 분류(불명확한 경우 General로 폴백).
  원본 폴더명은 "From_*" 서브폴더로 보존해 추적 가능성 유지.
- 최종 배치: Programming 2784 / General 1608 / Graphic 285 / Business 249 = 4926개 문서.
- 에이전트 운영 상태(.astra/.agent/.obsidian/sessions/memory/_company/docs/lessons/_shared/src)는
  지식 콘텐츠가 아니므로 재분류 대상에서 제외하고 원위치 유지.
- Topics/Topic_email(상위 보호 폴더 Topic_email과 파일명 100% 중복) 삭제 — 보호 폴더 자체는 미변경.
- 완전히 비게 된 Topic_Agent/Topic_Blog/Topics_Biz/Topics_Rag 폴더 제거.
2026-07-05 00:33:48 +09:00

4.0 KiB

id, title, category, status, source_trust_level, verification_status, created_at, updated_at, tags, tech_stack, applied_in, aliases
id title category status source_trust_level verification_status created_at updated_at tags tech_stack applied_in aliases
native-memory-profiling Native Memory Profiling — iOS / Android leak 추적 Coding draft B conceptual 2026-05-09 2026-05-09
native
memory
profiling
leak
vibe-coding
language applicable_to
Swift / Kotlin
iOS
Android
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

# Xcode → Product → Profile (⌘I) → Leaks 또는 Allocations
# 시나리오 재현 → Instruments 가 cycle / 누수 표시.
# Memory Graph Debugger: Xcode debug → Memory Graph 버튼.

iOS — 일반 leak 패턴

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

// build.gradle
debugImplementation("com.squareup.leakcanary:leakcanary-android:2.13")
// 자동 leak detection.
// Activity / Fragment / View / ViewModel 가 destroy 후에도 referenced 면 알림.

Android — 일반 leak 패턴

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: 사이 늘어난 객체

비트맵 / 큰 자원

// 큰 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.

🔗 관련 문서