[mono-android] Possible GC Bug?
Michael Kestner
mkestner at novell.com
Fri Mar 18 17:47:07 EDT 2011
On Mar 18, 2011, at 4:24 PM, Aaron Knabb wrote:
> Thanks for your input Jon. So if I understand correctly, I should call GC.Collect regularly? I don't see how to know what objects (assuming this isn't exclusive to Bitmaps) are allocated in Dalvik and which ones aren't. If that's the case, then I should call collect just in case there's something that needs to be cleaned up.
Just to add a little to the discussion, calling Dispose on the Bitmap doesn't necessarily cause a java collection of the underlying object. All that can really happen is that we release our explicit ref to it. If you have done something with the Bitmap like:
Canvas canvas = new Canvas();
canvas.Bitmap = bitmap;
and you don't also dispose the Canvas, then you have to wait for the canvas to be finalized by the mono GC before the java collector is going to be able to release the bitmap. If the Canvas is being held alive by mono pending a collection, the bitmap is effectively held alive on the java side.
I mention this scenario because we have seen it explicitly reported, but I am sure there are many ways to get into a situation where you Dispose an object, but java/dalvik can't really get rid of it right away because of outstanding refs. It's possible you are seeing something similar if an explicit GC.Collect helps. You probably are missing an additional Dispose.
Mike
> -Aaron
>
> On Fri, Mar 18, 2011 at 10:56 AM, Jonathan Pryor <jpryor at novell.com> wrote:
> On Mar 18, 2011, at 12:43 PM, Aaron Knabb wrote:
> > However, if I am creating and disposing of a large bitmap every time my activity is recreated (say in an ImageView), I run out of memory before long.
>
> The problem -- which is present on every GC system -- is that Mono's GC doesn't know how big the Dalvik-allocated bitmap is (just as .NET's GC doesn't know that you just called Marshal.AllocHGlobal() to allocate 4GB of memory...). Furthermore, Mono's GC _can't_ know how big the Dalvik-allocated bitmap is, and even if you knew how much memory it took GC.AddMemoryPressure() isn't supported.
>
> Thus, in this (and similar) cases you need to help the GC out, as you know things the GC doesn't, and thus the GC.Collect() call is appropriate.
>
> - Jon
>
> _______________________________________________
> Monodroid mailing list
> Monodroid at lists.ximian.com
>
> UNSUBSCRIBE INFORMATION:
> http://lists.ximian.com/mailman/listinfo/monodroid
>
> _______________________________________________
> Monodroid mailing list
> Monodroid at lists.ximian.com
>
> UNSUBSCRIBE INFORMATION:
> http://lists.ximian.com/mailman/listinfo/monodroid
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/mailman/private/monodroid/attachments/20110318/29c2d355/attachment-0001.html
More information about the Monodroid
mailing list