Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Garbage collection in C# not carried out. Why?

I have tried a simple experiment to verify the functionality of the garbage collector. Referencing 3.9 Automatic memory management (MSDN) about automatic memory management in .NET. To me, it sounded like a shared pointer equivalent in C++. If the reference counter of an object becomes zero, it will be deallocated by the garbage collector.

So I tried creating a function inside my main form. The function was called inside the Shown event function of my main form which is executed after the constructor. Here is the experimental code.

    public void experiment()     {         int[] a = new int[100000];         int[] b = new int[100000];         int[] c = new int[100000];         int[] d = new int[100000];          a = null;         b = null;         c = null;         d = null;     } 

And here are the results:

Before memory allocation

http://i.stack.imgur.com/ZhUKc.png

After memory allocation

http ://i.stack.imgur.com/NyXJ6.png

Before leaving the function scope

http ://i.stack.imgur.com/MnTrm.png

After leaving the function scope

http ://i.stack.imgur.com/MiA6T.png

Why did not the garbage collector deallocate the memory allocated by the arrays a, b, c, d even after being set to null?

like image 233
Xegara Avatar asked Feb 06 '15 06:02

Xegara


People also ask

What is garbage collection in C programming?

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.

Does C have garbage collection?

C does not have automatic garbage collection. If you lose track of an object, you have what is known as a 'memory leak'. The memory will still be allocated to the program as a whole, but nothing will be able to use it if you've lost the last pointer to it. Memory resource management is a key requirement on C programs.

What is garbage collection with example?

Garbage collection in Java is the process by which Java programs perform automatic memory management. Java programs compile to bytecode that can be run on a Java Virtual Machine, or JVM for short. When Java programs run on the JVM, objects are created on the heap, which is a portion of memory dedicated to the program.

Why C has no garbage collection?

There are two reasons why C / C++ doesn't have garbage collection. It is "culturally inappropriate". The culture of these languages is to leave storage management to the programmer. It would be technically difficult (and expensive) to implement a precise garbage collector for C / C++.


2 Answers

The .NET garbage collector is an highly optimized, complicated beast of software. It is optimized to make your program run as fast as possible and using not too much memory in doing so.

Because the process of freeing memory takes some time, the garbage collector often waits to run it until your program uses a whole lot of memory. Then it does all the work at once, which results in a small delay of your program after a relatively long time (instead of many smaller delays earlier, which would slow down your program).

All this means, that the time the garbage collector runs is not predictable.

You may call your test several times (with some Sleep() in the loop) and watch memory usage slowly building up. When your program begins to consume a significant portion of available physical memory its memory usage will suddenly drop to near-zero.

There are a couple of functions (like GC.Collect()) which force several levels of garbage collection, but it's strongly advised not to use them unless you know what you are doing, because this tends to make your software slower and stops the garbage collector in doing its work in an optimal way.

like image 179
DrKoch Avatar answered Sep 28 '22 20:09

DrKoch


Even if it did de-allocate the memory internally, it's under no obligation to return it to the operating system. It will assume that more memory will be requested in the future and recycle the pages. The operating system's number knows nothing of how the program has chosen to use the memory it has claimed.

If you actually want to claim and release memory explicitly you'll have to call VirtualAlloc() through Pinvoke unsafe code.

like image 42
pjc50 Avatar answered Sep 28 '22 20:09

pjc50