Skip to content

Instantly share code, notes, and snippets.

@rossdargan
Created December 2, 2014 12:54
Show Gist options
  • Save rossdargan/fd231df4ae91e0a85245 to your computer and use it in GitHub Desktop.
Save rossdargan/fd231df4ae91e0a85245 to your computer and use it in GitHub Desktop.
public WeakReference Reference;
public override void ViewDidLoad()
{
base.ViewDidLoad();
float y = 30;
View.BackgroundColor = UIColor.White;
// The image we will work with; we derive our own so we can see
// it get destroyed.
var imageView = new MyImageView(new RectangleF(0, View.Bounds.Height / 2, View.Bounds.Width, View.Bounds.Height / 2));
Add(imageView);
Reference = new WeakReference(imageView);
// Create the button to remove our image
AddButton(ref y, "Remove Image from Screen",
(sender, e) => {
Console.WriteLine ("Removing image from UI");
imageView.RemoveFromSuperview();
imageView.Dispose();
imageView = null;
});
AddButton(ref y, "Output reference", (sender, args) =>
{
Console.WriteLine(Reference.IsAlive);
});
// TODO: Step 1 - Force GC
AddButton(ref y, "Force GC Now!",
(sender, e) => {
// This forces a Garbage Collection
Console.WriteLine ("Forcing GC!");
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment