On Aug 21, 2012, at 1:38 PM, bjarke <[email protected]> wrote:
> But what happens if a bitmap is used in multiple activities(eg. Background
> image) and then disposed because the list is too large? Wont the activities
> suffer or similar??
Yes, if the Activities share the same Bitmap instance. The question is, can
they? Rephrased, how are you using the Bitmaps?
If each activity independently creates and disposes of the instance without
storing the Bitmap instance in a field, then things should work sanely:
[Activity]
partial class ExampleActivity : Activity {
protected override void OnCreate (Bundle b)
{
using (var background = BitmapFactory.DecodeResource
(Resources, Resource.Id.Background))
SetWallpaper (background);
}
}
When an Android API returns the same Java instance, then Mono for Android will
attempt to return the same wrapper instance (assuming that there's a
pre-existing wrapper to return). The above code block works by shortening the
lifetime of the wrapper, thus ensuring that each Activity creates (and disposes
of) its own wrapper.
However, add multi-threading and all bets are off; depending on timing, two
different callers could get the same instance, and if one disposes of the
instance then the other caller gets a disposed (i.e. useless) wrapper back.
The above Activity code is safe only because all Activities need to run on the
Main (UI) thread, so there's no possibility of multi-threading issues.
If the above code instead held a Bitmap instance member, then we hit the
"instance sharing" scenario; disposing of one impacts them all (because there's
only one instance!), so things will fail quickly.
So what should you do if you need to share a Bitmap instance? Share normally,
but don't dispose of it. If you're running out of Java memory (easy to do with
lots of Bitmap instances...), you'll need to get involved with Android's memory
notification mechanism so that you can free up the bitmaps when you run low on
memory.
- Jon
_______________________________________________
Monodroid mailing list
[email protected]
UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid