Letting something go out of scope is not the same as deliberately disposing it. When an object leaves scope it's undetermined when it's cleaned up by the garbage collector. e.g. you can grow your memory consumption beyond the capacity of the device before the GC ever gets a chance to clear up some room. If the object implements IDisposable, and you're done with it, you should absolutely dispose it. Now that doesn't mean you have to immediately. You can throw objects into a list and manage things intelligently. i.e. you might have a historical nav list where you hold onto the last 10 items but anything older gets disposed.
Also, be careful of event subscriptions. Objects can unknowingly be held in memory with poor event subscription management. http://blogs.msdn.com/b/abhinaba/archive/2009/05/05/memory-leak-via-event-handlers.aspx -Abe 2011/7/4 Jon Hopkins <[email protected]>: > Hi, > > > > I would try a small test app and use a timer to add and remove your UI > object every second. If there is a leak, it will eventually crash. > > My thread shows an example. > > > > Jon > > > > Von: [email protected] > [mailto:[email protected]] Im Auftrag von Anders Kjærgaard > Hansen > Gesendet: 04 July 2011 13:33 > An: [email protected] > Betreff: [MonoTouch] When to use 'using' to avoid memory issues in MonoTouch > > > > Hi > > > > I am soon shipping our first iPad app using MonoTouch, and are still > experiencing some crashes that might be related to memory handling. Some of > it seem to somehow relate to the thread “100 times Adding / removing a View > + backgroundImage will always crash !” > > > > Once in a while my app crashes, typically flipping a lot back and forth > between detailview controllers in the UISplitViewController, sometime when > returning from UIImagePickerController (picture taken with camera), and > sometimes when the iPad has been laying around for long time (e.g. > overnight). > > > > I can’t seem to find anything in crash logs or instruments that make me much > wiser. > > > > So thinking the using and IDisposable pattern could be some of the trouble. > > > > So my question is this – when is it advisable to use “using”, and when > should the garbage collector be able to handle it. > > > > For now I have all UIImages as using. But stuff like UITableViewCell, > UIAlertViews (which I would need a ref to – so “using” is not good here – > but could dispose them manually), UIImagePickerController, UIBarButton etc. > is not disposed by me. > > > > So basically all I am disposing myself is the UIImages. > > > > Should I e.g dispose UITableViewCells once added to a UITableview – even > though they run out of scope a few lines later? > > > > I hope some of you might have some good advice/experience on this > > > > Best regards > > /Anders > > _______________________________________________ > MonoTouch mailing list > [email protected] > http://lists.ximian.com/mailman/listinfo/monotouch > > _______________________________________________ MonoTouch mailing list [email protected] http://lists.ximian.com/mailman/listinfo/monotouch
