Skip to content

Conversation

@copybara-service
Copy link

@copybara-service copybara-service bot commented Jun 24, 2025

Allow limiting the size of LruBitmapPool/MemoryCache when in background

When an app is moved to background, onTrimMemory() will be called, and LruBitmapPool/MemoryCache will be trimmed. However, the app could keep running and will new bitmaps will be retired to MemoryCache and LruBitmapPool. This change limits the sizes of LruBitmapPool/MemoryCache to zero when the app is in background, and restore the sizes to normal when it's moved out of background.

We introduce an experimental API setMemoryCategoryInBackground(MemoryCategory) for this purpose, as well as a new MemoryCategory.ZERO for no pooling/caching. App can use this API to set the MemoryCategory to limit the pool/cache size when the app is in background.

@sjudd
Copy link
Collaborator

sjudd commented Jun 24, 2025

This is probably not a good idea. It'll cause a regression in performance for apps that would like to retain cached images in the background. The existing logic already decreases memory usage when the app is backgrounded, just not all the way to empty. That happens in onLowMemory

I don't think the atomic boolean here is useful these methods are all executed on the UI thread.

@copybara-service copybara-service bot changed the title When an app is moved to background, onTrimMemory() will be called, and LruBitmapPool/MemoryCache will be trimmed. However, the app could keep running and will new bitmaps will be retired to MemoryCache and LruBitmapPool. This change limits the sizes of LruBitmapPool/MemoryCache to zero when the app is in background, and restore the sizes to normal when it's moved out of background. Allow limiting the size of LruBitmapPool/MemoryCache when in background Dec 12, 2025
@copybara-service copybara-service bot force-pushed the test_774983423 branch 2 times, most recently from 9d7bda3 to 65596f0 Compare December 12, 2025 19:06
@copybara-service copybara-service bot force-pushed the test_774983423 branch 3 times, most recently from 0cad726 to 0e0a308 Compare December 18, 2025 19:11
When an app is moved to background, onTrimMemory() will be called, and LruBitmapPool/MemoryCache will be trimmed. However, the app could keep running and will new bitmaps will be retired to MemoryCache and LruBitmapPool. This change limits the sizes of LruBitmapPool/MemoryCache to zero when the app is in background, and restore the sizes to normal when it's moved out of background.

We introduce an experimental API `setMemoryCategoryInBackground(MemoryCategory)` for this purpose, as well as a new `MemoryCategory.ZERO` for no pooling/caching. App can use this API to set the MemoryCategory to limit the pool/cache size when the app is in background.

PiperOrigin-RevId: 774983423
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants