Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does garbage collection and scoping work in C#? [duplicate]

I'm learning C# coming from python and wish to know how the C# garbage collector works - I found that I understood a lot more about python once I figured out what it was doing behind the scenes, and wish to avoid making the sort of noob errors I made at first when learning python.

I've not been able to find any good clear explanations of when an item is garbage collected and am left with questions such as

  1. "What happens to an object when its last reference passes out of scope?" Does that object get garbage collected or is it still there when you pass back into the scope in which it was defined?
  2. "At what point is the number of refernces decremented?" Leading me to wonder whether it even uses reference counting or some other technique...

Answers to these, or even better a clear consise overview of what's actually going on will win cookies (or upvotes), and even better if your answer compares it to the python way of doing things. I'm not interested in which is better, just the details. Also answers on my original post on programmers.stackexchange would be much appreciated...

like image 976
theheadofabroom Avatar asked Mar 24 '11 17:03

theheadofabroom


People also ask

How does C garbage collection work?

Garbage collection (GC) is a memory recovery feature built into programming languages such as C# and Java. A GC-enabled programming language includes one or more garbage collectors (GC engines) that automatically free up memory space that has been allocated to objects no longer needed by the program.

What is garbage collection how it works in net?

NET's garbage collector manages the allocation and release of memory for your application. Each time you create a new object, the common language runtime allocates memory for the object from the managed heap.

Which algorithm is used for garbage collection in C?

The mark-and-sweep algorithm is called a tracing garbage collector because it traces out the entire collection of objects that are directly or indirectly accessible by the program.

What is garbage collection in C with example?

What Is Garbage Collection? Garbage Collection (GC) is a mechanism that provides automatic memory reclamation for unused memory blocks. Programmers dynamically allocate memory, but when a block is no longer needed, they do not have to return it to the system explicitly with a free() call.


2 Answers

The dotnet GC engine is a mark-and-sweep engine rather than a reference-counter engine like you're used to in python. The system doesn't maintain a count of references to a variable, but rather runs a "collection" when it needs to reclaim RAM, marking all of the currently-reachable pointers, and removing all the pointers that aren't reachable (and therefore are out of scope).

You can find out more about how it works here:
http://msdn.microsoft.com/en-us/library/ee787088.aspx

The system finds "reachable" objects by starting at specific "root" locations, like global objects and objects on the stack, and traces all objects referenced by those, and all the objects referenced by those, etc., until it's built a complete tree. This is faster than it sounds.

like image 185
tylerl Avatar answered Oct 04 '22 18:10

tylerl


At some indeterminate point in time after the last reference to an object disappears, the object will be collected.

The second part of your first question doesn't make sense.
If you can get back into the scope in which an object was defined (eg, a lambda expression), there is obviously still a reference.

The GC does not use reference counting at all.
Rather, it uses a mark-and-sweep algorithm.

like image 22
SLaks Avatar answered Oct 04 '22 18:10

SLaks