Reputation: 2647
I'm tracing down memory usage and leaks in my Xamarin Android project and decided to start on one of the simple pages.
When starting the dashboard/launch activity I have 21.790 MB of allocated memory and a heap size of 26.016 MB.
When I open the help activity I simply create an activity with the OnCreate method looking like:
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.HelpActivity);
ActionBar.SetDisplayHomeAsUpEnabled(true);
_quickstartGuideLinearLayout = FindViewById<LinearLayout>(Resource.Id.quickstart_guide_layout);
_quickstartGuideLinearLayout.Click += ViewQuickstart;
_usermanualLinearLayout = FindViewById<LinearLayout>(Resource.Id.user_manual_layout);
_usermanualLinearLayout.Click += ViewUserManual;
}
and then in OnDestroy:
protected override void OnDestroy()
{
if (_quickstartGuideLinearLayout != null)
{
_quickstartGuideLinearLayout.Click -= ViewQuickstart;
_quickstartGuideLinearLayout.Dispose();
_quickstartGuideLinearLayout = null;
}
if (_usermanualLinearLayout != null)
{
_usermanualLinearLayout.Click -= ViewUserManual;
_usermanualLinearLayout.Dispose();
_usermanualLinearLayout = null;
}
base.OnDestroy();
}
Memory usage will go up to 22.102 MB when the help activity is shown, and when I click back or the action bar up button it will only go down to 22.078 MB.
How come it doesn't go back down to 21.790 MB if the help activity is destroyed?
Upvotes: 4
Views: 2593
Reputation: 2647
I seem to have fixed it by adding Dispose() to my OnDestroy() as follows:
protected override void OnDestroy()
{
base.OnDestroy();
Dispose();
}
EDIT: Nevermind it seems as if it was purely due to me calling GC.Collect() in conjunction with Dispose(). Removing Dispose() had no negative effect so I'm guessing my original issue was just based upon the fact that the small inconsequential amount of memory increase isn't causing the Mono GC to run which is by nature how things are supposed to be.
EDIT Per Xamarin support: It is not necessary to call Dispose on the activity in OnDestroy(). Objects found via FindViewById should be disposed in OnDestroy via calling .Dispose() on each as well as having event handlers removed via -= MyEventHandler;
Upvotes: 3