360 Secure SkillHub
360 安全技能中心

dart-memory

该 Skill 是一份 Dart 内存管理指南,覆盖 StreamController、Timer、FocusNode 等资源释放,降低 GC 压力,移动端 Isolate、图片内存优化,以及分页和流式处理大数据。它提供原则和建议,不包含可执行脚本或具体修复代码。

分类:开发
dhruvanbhalara
热度 0.00
最近更新 2026-10-24 03:08:31

安装

发给 Agent
帮我安装这个技能:npx secure-skills-cli add dhruvanbhalara/skills/dart-memory
兼容性
通用兼容
联网要求
不联网
数据出网
数据不出本机
Metadata
name
dart-memory
description
Use when troubleshooting memory leaks, optimizing garbage collection overhead, or disposing streams and listeners in Dart.
metadata
{"platforms":"cross-platform, mobile","languages":"dart","category":"optimization"}

Memory Management

Mobile devices have limited RAM. Efficient memory management is critical to prevent crashes and ensure a smooth user experience.

Resource Lifecycle

  • Explicit Disposal: Always close StreamController, Timer, FocusNode, and ChangeNotifier in the dispose() method.
  • Late Initialization: Use late to delay object creation until it's actually needed, reducing initial memory footprint.

Garbage Collection (GC) Pressure

  • Generational GC: Dart's GC is optimized for short-lived objects. However, creating thousands of objects in a single frame can still cause jank.
  • Object Re-use: Avoid creating new objects in build() or high-frequency loops. Reuse data structures where possible.
  • Large Collections: Clearing a large list (list.clear()) is better than re-assigning it to a new list if the list itself is long-lived.

Mobile Specifics

  • Isolates: Use Isolate.run() for heavy computations (JSON parsing > 1MB, image processing). This keeps the main thread free and prevents UI freezes.
  • Image Memory: Use cacheWidth and cacheHeight in Image.network or Image.asset to avoid loading high-resolution images into memory at full size.
  • Memory Leaks: Use the DevTools Memory View to identify "leaking" objects that stay in the heap after their context (like a screen) is closed.

Large Data Handling

  • Pagination: Never load entire datasets into memory. Use server-side or local database pagination (Isar, SQLite).
  • Streaming: For large files or real-time data, use Stream to process data in chunks rather than buffering the entire content in memory.