I have an application that creates lot of threads over time. I noticed that memory usage grows as it runs and eventually runs out of memory. But the same code doesn't leak memory on my coworker's environment. We both have same .net version. I was able to reproduce the issue with the following sample code which doesn't leak on my coworker's laptop but does on my laptop.
public static void Main(string[] args)
{
Console.WriteLine("Version " + Environment.Version.ToString());
if (Environment.Is64BitProcess)
Console.WriteLine("64");
else
Console.WriteLine("32");
while(true)
{
Thread t = new Thread(() => { Thread.Sleep(1); });
t.IsBackground = true;
t.Start();
Thread.Sleep(1);
}
}
When I run the above, it prints the following
Version 4.0.30319.18063
32
In Visual Studio 2012 the target framework for the project is .net framework 4.5. The project leaks memory with following configuration
Project Properties -> Build
Platform target: Any CPU
Prefer 32-bit: checked
If I unchecked Prefer 32-bit, it doesn't leak.
Another configuration that leaks memory is
Project Properties -> Build
Platform target: x86
Prefer 32-bit: disabled
The resulting executable that leaks on my laptop doesn't leak on my coworker's laptop.
I used CLR Profiler to find memory leaks but it doesn't show anything that's leaking. But I do see that working set in windows resource monitor increases by about 1 MB/sec.
What is causing the memory usage to increase in 32-bit mode on my environment but not my coworker's?