On May 11, 2012, at 6:22 AM, michelw wrote:
> I've a problem (too malloc) when i read pics from sdcard in my gridview.

The problem is circular (cross-VM) object references:

        http://docs.xamarin.com/android/advanced_topics/garbage_collection

In particular:

>                    //i print the good pic
>                    
> imageView.SetImageDrawable(Drawable.CreateFromPath(Config.CheminCache + 
> nameofpic));

The Drawable.CreateFromPath() call here will create a Peer instance (C# 
Android.Graphics.Drawables.Drawable instance + Java 
android.graphics.drawable.Drawable instance), which form a circular reference: 
the C# managed peer won't be collectable until the Java framework peer is 
collectible, and vice versa. The result is that the Java-side Drawable instance 
will have an extended lifetime; it is still collectible, but ONLY if it ceases 
to be referenced from Java code AND the C# object is no longer referenced from 
C# code.

Normally this would be fine. However, with large Java-side objects (such as 
Bitmaps) having ANY extended lifetime semantics is very probably Badâ„¢, as 
Mono's GC won't be running frequently enough to allow the Java instance to be 
collected.

The fix is to nuke the Managed peer:

        using (var d = Drawable.CreateFromPath(Config.CheminCache + nameofpic))
                imageView.SetImageDrawable(d);

This will allow the Java-side Drawable object to be collected as soon as Dalvik 
is able to, instead of whenever both Dalvik & Mono agree that an object can be 
collected.

 - Jon

_______________________________________________
Monodroid mailing list
[email protected]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid

Reply via email to