This is a theoretical question more than anything else. In the case of C# and .NET, if an .aspx or .ascx script dies due to an unhandled exception, would you normally expect its memory to be released, or is that not a sure thing? In other words, can an unhandled exception itself leak memory?
Share
What is a “memory leak”? In manually managed environments we say that unreferenced allocations are a leak. By this definition, there are no leaks in a GC environment.
However, there are a few scenarios that have the same effect:
Stale References
If Process() throws, the item remains referenced in
itemMap. Unless you regulary throw out the entire map (or you made the wise decision to use weak pointers for the map), the item map will accumulate and hold references to temporaries, blocking memory.Unmanaged resources
Imagine you have a small managed object that allocates large unmanaged resources, and does not implement IDisposable, or IDisposable isn’t used.
These objects might live long enough to get pushed to Gen 2. If there’s no other pressure on the managed heap, a Gen 2 collection might never run. You may end up pushing tiny objects into Gen 2 until the unmanaged resources exhaust your memory.
This scenario might sound silly, but that’s exactly how COM Interop works (and I have a case ID to prove it).