dart-memory
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, andChangeNotifierin thedispose()method. - Late Initialization: Use
lateto 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
cacheWidthandcacheHeightinImage.networkorImage.assetto 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.
More from dhruvanbhalara/skills
flutter-ui
Build performant, accessible UIs with strict design tokens and reusable widget patterns. Use when implementing layouts, responsive breakpoints, theming, widget extraction, or fixing common rendering issues like overflow errors.
180flutter-firebase
Integrate Firebase services including Authentication, Firestore, Cloud Messaging, Crashlytics, and Analytics. Use when adding backend capabilities, push notifications, crash reporting, or remote configuration to a Flutter app.
146flutter-dio
Implement HTTP networking with Dio including interceptors, retry logic, and response caching. Use when building API clients, configuring authentication headers, or handling network errors gracefully.
143flutter-security
Enforce architect-level security standards including AES-256-GCM encryption, secure storage, biometric gates, and memory safety. Use when handling sensitive data, credentials, clipboard content, or API communication security.
140flutter-architecture
Enforce Clean Architecture with BLoC pattern for Flutter applications. Use when scaffolding features, structuring data/domain/presentation layers, defining data models, or integrating native platform channels.
138flutter-debugging
Debug and profile Flutter applications using DevTools, structured logging, and memory analysis. Use when diagnosing layout issues, tracking performance bottlenecks, or setting up centralized error reporting with Crashlytics.
133