Skip to content

Instantly share code, notes, and snippets.

@SamSaffron
Created May 3, 2009 23:03
Show Gist options
  • Save SamSaffron/106190 to your computer and use it in GitHub Desktop.
Save SamSaffron/106190 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;
namespace ConsoleApplication11 {
class Program {
class Foo {
int[] nums = new int[10000];
public void Dispose2() {
var weakRef = GetWeakRef();
GC.Collect();
GC.WaitForPendingFinalizers();
GC.SuppressFinalize(weakRef.Target);
}
private WeakReference GetWeakRef() {
var weakRef = new WeakReference(this);
return weakRef;
}
~Foo() {
Debug.Assert(false);
}
}
static void Main(string[] args) {
Stack<Foo> foos = new Stack<Foo>(Enumerable.Range(1,50000).Select(_ => new Foo()));
Console.WriteLine("got foo");
while(foos.Count > 0) {
var foo = foos.Pop();
foo.Dispose2();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment