Skip to content

Instantly share code, notes, and snippets.

@rolfbjarne
Created June 3, 2013 21:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rolfbjarne/9a86e8113d3be689589c to your computer and use it in GitHub Desktop.
Save rolfbjarne/9a86e8113d3be689589c to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
namespace perf1
{
[Register("AppDelegate")]
public partial class AppDelegate : UIApplicationDelegate
{
UIWindow window;
UINavigationController navController;
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
window = new UIWindow (UIScreen.MainScreen.Bounds);
navController = new UINavigationController ();
window.Frame = UIScreen.MainScreen.Bounds;
window.RootViewController = navController;
window.MakeKeyAndVisible ();
UIViewController root = new UIViewController ();
root.View.AddSubview (new UIView (){BackgroundColor=UIColor.Green, Frame=root.View.Bounds});
navController.PushViewController (root, false);
root = null;
PushPopViewController ("one", false, true); // MyView & MyViewController instances should get garbage collected afterwards, and will be
PushPopViewController ("two", true, true); // MyView & MyViewController instances should get garbage collected afterwards, and will not be
GC.Collect ();
Console.WriteLine ("GC");
PushPopViewController ("three", false, false); // MyView & MyViewController instances should get garbage collected after navigating back, and will not be
PushPopViewController ("four", true, false); // MyView & MyViewController instances should get garbage collected after navigating back, and will not be
//FixCase3 ();
return true;
}
void FixCase3 ()
{
// This fixes case 3:
ThreadPool.QueueUserWorkItem ((v) =>
{
while (true) {
Thread.Sleep (1000);
InvokeOnMainThread (() =>
{
Console.WriteLine ("ViewControllers: {0}", navController.ViewControllers.Length);
});
}
});
}
void PushPopViewController (string name, bool referenceViewController, bool pop)
{
MyViewController myViewController = new MyViewController (name);
navController.PushViewController (myViewController, false);
UIView view1 = myViewController.View; // cause the view to get instantiated, even if doesn't get displayed
if (pop)
navController.PopViewControllerAnimated (false);
}
class MyView : UIView
{
string name;
public MyView (string name)
{
this.name = name;
Console.WriteLine ("MyView() " + name);
BackgroundColor = UIColor.Yellow;
}
~MyView ()
{
Console.WriteLine ("~MyView() " + name);
}
}
class MyViewController : UIViewController
{
string name;
public override void LoadView ()
{
View = new MyView (name);
}
public MyViewController (string name)
{
this.name = name;
Console.WriteLine ("MyViewController() " + name);
}
~MyViewController ()
{
Console.WriteLine ("~MyViewController() " + name);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment