ResidentModelCache
In-memory cache of loaded models (engines or other AutoCloseable resources) with a RAM budget and OS low-memory eviction.
put / getOrPut register a model with its estimated resident size (for example ModelHeaderInfo.estimatedPeakRam); the least recently used idle models are closed when the budget is exceeded.
attachTo subscribes to a MemoryPressureObserver (Android
AndroidMemoryPressureObserver, iOSIosMemoryPressureObserver, orManualMemoryPressureObserveron desktop): MemoryPressureLevel.MODERATE closes idle models until at most half the budget is resident, MemoryPressureLevel.CRITICAL closes every idle model.Leased (in-use) models are never closed by eviction.
Resources are closed outside the internal lock, on the thread that triggered the eviction. Thread-safe.
Parameters
resident budget in bytes.
Properties
Functions
Leases the model under key, or returns null if it is not resident.
Evicts on every signal from observer. Close the returned handle to unsubscribe.
Closes idle models according to level: NORMAL enforces the budget, MODERATE trims to half the budget (or half of what is resident if no budget is set), CRITICAL closes every idle model.