dart-memory
Original:🇺🇸 English
Translated
Professional memory management for mobile applications, focusing on resource disposal, garbage collection pressure, and large asset handling.
3installs
Sourcedhruvanbhalara/skills
Added on
NPX Install
npx skill4agent add dhruvanbhalara/skills dart-memoryTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →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, andFocusNodein theChangeNotifiermethod.dispose() - Late Initialization: Use to delay object creation until it's actually needed, reducing initial memory footprint.
late
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 or high-frequency loops. Reuse data structures where possible.
build() - Large Collections: Clearing a large list () is better than re-assigning it to a new list if the list itself is long-lived.
list.clear()
Mobile Specifics
- Isolates: Use for heavy computations (JSON parsing > 1MB, image processing). This keeps the main thread free and prevents UI freezes.
Isolate.run() - Image Memory: Use and
cacheWidthincacheHeightorImage.networkto avoid loading high-resolution images into memory at full size.Image.asset - 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 to process data in chunks rather than buffering the entire content in memory.
Stream