I am really confused about GC. My head is a mess when I am trying to explain this to somebody.
Do you guys have a guide or set of articles or a good book that explains GC in .NET?
I did program a little bit in C a long time ago, not much in C++ but mostly in C# all I can explain is using
keyword for opening files and Dispose
which I don't really know how to answer about its internals.
I googled and youtube'd about GC, all I get is empty terminology for interview to memorize which I don't want to memorize. I am looking firstly for a history of it, how it works in C, C++. I don't want random keywords managed heap
, 3 generations
, mutex
, weak references
, like fine I wanna know about it too but I need an order, reading about GC feels like a puzzle for me, I open like 10 tabs then I forgot where it started and its really frustrating.
I really need a dummy guide, from C to IDisposable, even if it takes 500 pages I am gonna go through it.
I know it might be asked a lot of times, it might be a dumb question, but I am never gonna learn if I don't ask for help. I googled myself, I get mostly interview responses which aren't followed by code and its like reading a med school book, I am never gonna memorize anything if it doesn't make sense.
A very high level summary -
Garbage collection is one of the key differentiators between managed languages like C# and unmanaged languages like C and C++.
Managed languages take care of allocating and deallocating memory for your data objects. Garbage collection is just the automatic freeing of memory when you don't need it anymore. C and C++ don't do this for you - you have to do it yourself, or else you will eventually run out of memory. Obviously folks have come up with strategies over the years for dealing with this (reference counters, etc.), but there's really no substitute for the automatic garbage collector of a managed language.
The truth is in C# you rarely have to worry about garbage collection. There are a handful of scenarios where you can accidentally step into pitfalls that prevent it from happening on some objects - we call these memory leaks - but that's a bigger topic.
The Wikipedia article on garbage collection is pretty decent if you want to try to get into the nitty gritty. Otherwise if you're just getting into C# or explaining C# to someone new to it I honestly wouldn't think about it in the beginning. That's sort of the point - it exists so that you don't have to think about it.
Also using
and Dispose
are actually not really related to garbage collection (maybe indirectly). In fact using
and Dispose
are closer to resource management strategies in unmanaged languages. That is to say they represent manual resource deallocation. But Dispose
isn't supposed to be used for memory deallocation either except in rare circumstances. Rather it's supposed to be for cleaning up any other resources that might be in use, such as open files.