On my app, using Xamarin Profiler I noticed that whenever I push a VC to the stack and navigate back the memory allocation is not free. If I push that same view again it adds more memory.
I created a sample project to test and I found out that is does the same thing.
Sample project:
I have two view controllers, VC1 and VC2. VC1 is the root view controller.
Whenever I push VC2 from VC1, memory is allocated but when I navigate back the memory is not free. If I keep pushing VC2 again it adds more memory. In VC2 I added 3 labels through the designer.
in AppDelegate:
namespace TestSample
{
[Register("AppDelegate")]
public class AppDelegate : UIApplicationDelegate
{
public override UIWindow Window
{
get;
set;
}
public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
Window = new UIWindow(UIScreen.MainScreen.Bounds);
var nav = new UINavigationController(new MyViewController());
Window.RootViewController = nav;
Window.MakeKeyAndVisible();
return true;
}
}
}
VC1:
namespace TestSample
{
public partial class MyViewController : UIViewController
{
public MyViewController() : base("MyViewController", null)
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
// Perform any additional setup after loading the view, typically from a nib.
}
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
btn1.TouchUpInside += Btn1_TouchUpInside;
}
void Btn1_TouchUpInside(object sender, EventArgs e)
{
NavigationController.PushViewController(new MyViewController2(), true);
}
public override void ViewDidDisappear(bool animated)
{
base.ViewDidDisappear(animated);
btn1.TouchUpInside -= Btn1_TouchUpInside;
}
public override void DidReceiveMemoryWarning()
{
base.DidReceiveMemoryWarning();
// Release any cached data, images, etc that aren't in use.
}
}
}
VC2:
namespace TestSample
{
public partial class MyViewController2 : UIViewController
{
public MyViewController2() : base("MyViewController2", null)
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
// Perform any additional setup after loading the view, typically from a nib.
}
public override void DidReceiveMemoryWarning()
{
base.DidReceiveMemoryWarning();
// Release any cached data, images, etc that aren't in use.
}
public override void ViewDidDisappear(bool animated)
{
base.ViewDidDisappear(animated);
/*foreach (UIView view in View.Subviews) {
view.RemoveFromSuperview();
}*/
label1.RemoveFromSuperview();
label2.RemoveFromSuperview();
label3.RemoveFromSuperview();
label1.Dispose();
label2.Dispose();
label3.Dispose();
}
}
}