Goal: Automatically clear memoization caches when the operating system signals low memory pressure to potentially free up resources.
Proposed Implementation (Initial focus on Apple Platforms):
- System Notification: Use
NotificationCenter to observe system memory warnings (e.g., UIApplication.didReceiveMemoryWarningNotification on iOS/tvOS, investigate equivalents or related mechanisms like memory pressure dispatch sources on macOS).
- Central Observer: Implement a central entity (e.g., a singleton or shared manager within the Memoization library) responsible for listening to these system notifications.
- Storage Registry: Create a mechanism (e.g., a
WeakSet or similar weak collection) for active MemoizeStorage instances to register themselves with the central observer upon initialization and de-register upon deinitialization.
- Clear Action: When the central observer receives a low memory notification, it iterates through its registry of active storage instances and calls their
clear() method.
Considerations:
- Platform Abstraction: How to handle this for non-Apple platforms if broader support is desired? May require
#if os(...) checks.
- Granularity: Initial implementation can clear all registered caches. Future enhancements could consider more granular clearing based on priority or least-recently-used, but this adds complexity.
Reference: Conversation with Copilot on 2025-04-27.
Goal: Automatically clear memoization caches when the operating system signals low memory pressure to potentially free up resources.
Proposed Implementation (Initial focus on Apple Platforms):
NotificationCenterto observe system memory warnings (e.g.,UIApplication.didReceiveMemoryWarningNotificationon iOS/tvOS, investigate equivalents or related mechanisms like memory pressure dispatch sources on macOS).WeakSetor similar weak collection) for activeMemoizeStorageinstances to register themselves with the central observer upon initialization and de-register upon deinitialization.clear()method.Considerations:
#if os(...)checks.Reference: Conversation with Copilot on 2025-04-27.