After finding leak errors
Try harder =)
Managed language memory leaks can be difficult to track. I had a good impression of the Redgate ANTS Memory Profiler . It's not free, but they give you a 14-day full-featured trial. It has a nice interface and shows you where the memory is allocated and why these objects are stored in memory.
According to Alex, event handlers are a very common source of memory leaks in a .NET application. Consider this:
public static class SomeStaticClass { public event EventHandler SomeEvent; } private class Foo { public Foo() { SomeStaticClass.SomeEvent += MyHandler; } private void MyHandler( object sender, EventArgs ) { } }
I used a static class to make the problem as obvious as possible. Let's say that during the life of your application many Foo objects were created. Each Foo subscribes to the SomeEvent event of a static class.
Foo objects can go out of scope at the same time, but the static class maintains a reference to each of them through an event handler delegate. Thus, they survive indefinitely. In this case, the event handler just needs to be βdetachedβ.
... the XNA project seems to have saved a lot of byte arrays that I really used ...
You may encounter fragmentation in LOH. If you select large objects very often, they can cause a problem. The total size of these objects can be much smaller than the total memory allocated for the runtime, but due to fragmentation, a lot of unused memory is allocated for your application.
The profiler associated with the above will tell you if this is a problem. If so, you can probably track it until the object leaks somewhere. I just fixed the problem in my application, showing the same behavior, and this was due to the fact that the MemoryStream did not release its internal byte[] even after calling Dispose() on it. Wrapping a stream in a dummy stream and fixing it fixed the problem.
Also, specifying the obvious, make sure that Dispose() objects that implement IDisposable . Local resources may exist. Again, a good profiler will catch that.
My suggestion; This is not a GC, the problem is in your application. Use the profiler, load the application into a high memory consumption state, take a memory snapshot, and start the analysis.