Skip to content

Instantly share code, notes, and snippets.

@NVentimiglia
Created April 3, 2016 19:23
Show Gist options
  • Save NVentimiglia/b08d6c92be0d2015871ce9fd23aeed09 to your computer and use it in GitHub Desktop.
Save NVentimiglia/b08d6c92be0d2015871ce9fd23aeed09 to your computer and use it in GitHub Desktop.
public static class LeakTracker
{
static List<WeakReference> _tracker = new List<WeakReference>();
/// <summary>
/// should be called only at object construction/instantiation
/// </summary>
public static void Add(object objToTrack)
{
Prune();
_tracker.Add(new WeakReference(objToTrack));
}
public static void Prune()
{
_tracker = _tracker.Where(o => o.IsAlive).ToList();
}
public static string Dump()
{
Prune();
return string.Join("\r\n", _tracker.Select(o => o.Target).Where(o => o != null).Select(o => o.GetType().ToString()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment