Overview
WeakMap is a Dart package that implements a map with weakly referenced keys, allowing garbage collection of key objects when no other references exist. It enables efficient memory management by preventing memory leaks. WeakContainer provides identity checks for objects, ensuring they are the same instance as before. The package supports memoization through cache functions using weak references, ideal for temporary data storage. Designed for performance and safety in long-running applications.
Use cases
- Memoization with automatic cleanup
- Tracking object identity without leaks
- Caching results tied to transient objects
- Avoiding memory leaks in callbacks
- Storing metadata per instance
- Temporary state management
Key features
- Weakly referenced keys
- Garbage-collectible entries
- Identity-based object checking
- Built-in memoization utilities
- Cross-platform support
- Lightweight and fast
Suitable for
- Long-running apps with frequent object creation
- Memory-sensitive applications
- Plugins requiring instance-specific data
- Developers avoiding strong reference cycles
- Use cases needing ephemeral data storage
- High-performance caching
Considerations
- Keys may be collected unexpectedly
- Not suitable for persistent data
- No direct iteration or size access
- Limited to object-keyed storage
- Requires careful handling in async contexts
- Best used with transient or short-lived objects