Skip to content

Instantly share code, notes, and snippets.

@leachdaniel
Created January 14, 2018 19:29
Show Gist options
  • Save leachdaniel/6ff36b2ea72d23ddcc6c336fbe91c7b1 to your computer and use it in GitHub Desktop.
Save leachdaniel/6ff36b2ea72d23ddcc6c336fbe91c7b1 to your computer and use it in GitHub Desktop.
Example Program to Create a Managed Memory Leak
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ManagedMemoryLeak
{
class Program
{
// if you start this program and then create a memory dump
// you can open the dump up with Visual Studio (not available in Community)
// then you should be able to see the object that is using the most memory
static void Main(string[] args)
{
var e = new AutoResetEvent(false);
var t = new Timer(timerManaged_Tick, e, 0, 250);
e.WaitOne();
}
private static List<object> objs = new List<object>();
private static void timerManaged_Tick(object state)
{
AutoResetEvent autoEvent = (AutoResetEvent)state;
Console.WriteLine("tick");
// add some data to the list
var obj = new { time = DateTime.Now, data = Enumerable.Range(0, 10000).ToList() };
objs.Add(obj);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment