Reputation: 63
I'm designing an Android app which loads lots of small (say 128x128px@24-bit) PNG images as Bitmaps to be drawn on a View. There's a lot of them - combined they form a 3000x3000px map!
The map only loads the Bitmaps from the .png files when you first look at their section of the map, and they need to be drawn. Until then, they are not loaded.
I want to be able to unload (Bitmap.recycle()) the images when they've been off-screen for a certain period of time, otherwise the user scrolling around the map is going to quickly fill up the application heap.
Are there any ways of catching when the GC wants to run in the class which encapsulates the Bitmap images, so I can manually unload the Bitmaps which haven't been onscreen over a threshold time? Obviously the Dalvik GC won't unload the images itself as they always have live references.
(PS: I'm not using the Google Maps APIs as a challenge to myself - but it would be interesting to see how they handle the drawing of, say, satellite imagery... do they do what I do and dynamically load/unload image sections from file?)
Upvotes: 1
Views: 106
Reputation: 34563
Rather than unloading the images based on time, why not do it based on distance? The farther a tile is outside the visible boundaries, the less likely it is to be needed soon, assuming that all movement is by scrolling. You can choose a threshold — say, four tiles beyond the visible area — and unload any images that are outside that boundary.
As you scroll around, you'll be able to unload one row or column of tiles at the same time you need to load another, so your total memory usage remains constant.
Upvotes: 2
Reputation: 22656
I wouldn't advise relying on the garbage collector since this could be called erratically. It's probably better to have code that runs in your app which occasionally checks images and unloads if necessary.
Upvotes: 0