safwanc
safwanc

Reputation: 3391

Xamarin.iOS: Object Disposed but Memory Not Reclaiming

I have a straightforward UIPageViewController with 30 pages where each page has a UIImageView (and accompanying UIImage which is loaded with UIImage.FromFile(..)). I ensured that there are no references to any of the pages being held in memory (or by the UIPageViewDataSource) once the user navigates forward through the pages.

What I'm seeing is that when the user navigates forward, the destructor and Dispose(bool) methods are being called for the UIViewController of each page and for the UIImageView within the page (as expected). However, when I run this test on hardware attached to Instruments, even though the IDisposable methods are being called, the memory is not being reclaimed. After a certain threshold of "live bytes" is reached, a lot of the memory consumed by the pages which have been garbage collected is then reclaimed:

Instruments screenshot illustrating the problem

Why am I not seeing the memory being reclaimed immediately (i.e. when the IDisposable methods are called)?

Upvotes: 1

Views: 805

Answers (1)

poupou
poupou

Reputation: 43553

There's a few reasons for this (see New Dispose Semantics documentation).

They are mostly related to the fact that you call Dispose when you (the application) is done with the managed instance of the an object.

However it does not mean that iOS itself is done with the native object and it could very well try to callback into managed code (e.g. subclasses, events). This is not easy to predict unless you have a very good knowledge of the lifecycle of the objects involved (and any error would result in crashes).

Note that even before this change (in 6.0.8) the only thing that Dispose could release was the managed instance memory - which is, for most objects, very small compared to it's native size counterpart.

Upvotes: 1

Related Questions