Ignoring unsafe code, .NET cannot have memory leaks. I\'ve read this endlessly from many experts and I believe it. However, I do not understand why this is so.
It is
.NET can have memory leaks.
Mostly, people refer to the Garbage Collector, which decides when an object (or whole object cycle) can be gotten rid of. This avoids the classic c and c++ style memory leaks, by which I mean allocating memory and not freeing it later on.
However, many times programmers do not realize that objects still have dangling references and do not get garbage collected, causing a... memory leak.
This is normally the case when events are registered (with +=
) but not unregistered later on, but also when accessing unmanaged code (using pInvokes or objects that use underlying system resources, such as the filesystem or database connections) and not disposing properly of the resources.
There are already some good answers here, but I want to address one additional point. Let's look very carefully again at your specific question:
It is my understanding that the framework itself is written in C++ and C++ is susceptible to memory leaks.
The key here is to distinguish between your code and their code. The .Net framework (and Java, Go, python, and other garbage-collected languages) promise that if you rely on their code, your code will not leak memory... at least in the traditional sense. You might find yourself in situations where some objects are not freed as you expect, but these cases are subtly different from traditional memory leaks because the objects are still reachable in your program.
You are confused because you correctly understand that this is not the same thing as saying any program you create can't possibly have a traditional memory leak at all. There could still be a bug in their code that leaks memory.
So now you have to ask yourself: would you rather trust your code, or their code? Keep in mind here that their code is not only tested by the original developers (just like yours, right?), it's also battle-hardened from daily use by thousands (perhaps millions) of other programmers like yourself. Any significant memory leak issues would be among the first things identified and corrected. Again, I'm not saying it's not possible. It's just that it's generally a better idea to trust their code than it is your own... at least in this respect.
Therefore the correct answer here is that it's a variant of your first suggestion:
Is the underlying framework so well-written, that it absolutely does not have any possibility of internal memory leaks?
It's not that there's no possibility, but that it's much safer than managing it yourself. I'm certainly not aware of any known leaks in the framework.
This reference shows how leaks can happen in .Net using weak event patterns. http://msdn.microsoft.com/en-us/library/aa970850.aspx
What about if you are using a managed dll but the dll contians unsafe code? I know this is spliting hairs, but if you dont have the source code, then from yourr point of view, you are only using managed code but you can still leak.
The best example I've found was actually from Java, but the same principle applies to C#.
We were reading in text files that consisted of many long lines (each line was a few MB in heap). From each file, we searched for a few key substrings and kept just the substrings. After processing a few hundred text files, we ran out of memory.
It turned out that string.substring(...) would keep a reference to the original long string... even though we kept only 1000 characters or so, those sub-strings would still use several MB of memory each. In effect, we kept the contents of every file in memory.
This is an example of a dangling reference that resulted in leaked memory. The substring method was trying to reuse objects, but ended up wasting memory.
Edit: Not sure if this specific problem plagues .NET. The idea was to illustrate an actual design/optimization performed in a garbage collected language that was, in most cases, smart and useful, but can result in a unwanted memory usage.
One major source of C/C++ memory leaks that effectively doesn't exist in .Net is when to deallocate shared memory
The following is from a Brad Abrams led class on Designing .NET Class Libraries
"Well, the first point is, of course, there are no memory leaks, right? No? There are still memory leaks? Well, there is a different kind of memory leak. How about that? So the kind of memory leak that we don’t have is, in the old world, you used to malloc some memory and then forget to do a free or add ref and forget to do a release, or whatever the pair is. And in the new world, the garbage collector ultimately owns all the memory, and the garbage collector will free that stuff when there are no longer any references. But there can still sort of be leaks, right? What are the sort of leaks? Well, if you keep a reference to that object alive, then the garbage collector can’t free that. So lots of times, what happens is you think you’ve gotten rid of that whole graph of objects, but there’s still one guy holding on to it with a reference, and then you’re stuck. The garbage collector can’t free that until you drop all your references to it.
The other one, I think, is a big issue. No memory ownership issue. If you go read the WIN32 API documentation, you’ll see, okay, I allocate this structure first and pass it in and then you populate it, and then I free it. Or do I tell you the size and you allocate it and then I free it later or you know, there are all these debates going on about who owns that memory and where it’s supposed to be freed. And many times, developers just give up on that and say, “Okay, whatever. Well, it’ll be free when the application shuts down,” and that’s not such a good plan.
In our world, the garbage collector owns all the managed memory, so there’s no memory ownership issue, whether you created it and pass it to the application, the application creates and you start using it. There’s no problem with any of that, because there’s no ambiguity. The garbage collector owns it all. "
Full Transcript