Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

How the Garbage Collector works in .NET? Is it possible to invoke the GC programatically? and how?

user-image
Question ajoutée par Kashif Akhter , VP - Wing Head Application Development , National Bank of Pakistan
Date de publication: 2013/04/18
Zaid Rabab'a
par Zaid Rabab'a , Software Development Team Leader , Al-Safa Co. Ltd.

about the GC you mention its a memory managment system , it will check if the object you create been used or not , if not it will destroy the object to save memory , i think it works with variables if you want to call the GC programatically , use this GC.Collect();

Ahmed Gamal
par Ahmed Gamal , Senior Microsoft Dynamics AX Developer , Bi-Technologies

In C#, most of the time, you do not need to code destructors or finalizers because you can trust GC to clean up for you with dtors.
These functions are called implicitly by the C# runtime system's garbage collector.
http://www.c-sharpcorner.com/uploadfile/puranindia/garbage-collection-in-C-Sharp/

this article show all thing about garbage collector http://www.c-sharpcorner.com/uploadfile/puranindia/garbage-collection-in-C-Sharp/

Ammar Abdulhakim Ghanem Al-Qershi
par Ammar Abdulhakim Ghanem Al-Qershi , ICT Officer , ADRA

The GC is memory managemt, it reclaims the resources used by objects that are no longer referenced, however, objects are classified into two, managed objects and non-managed objects, for these managemed objects you don't have to call The GC programmatically it will work automatically, but in case of non managed resource like crystal reports viewer you have to call the GC programmatically otherwise you will run out of resources and eventually your system will crash and you will need to restart at leat your IIS or else the whole server, it can make your life awful...
in .Net all you have to do just call GC.Collect, it's advantegouse for you to call the GC in events of Page Unload and CrystalReportViwer Unload in addition to explecitly call of dispose and release for these un managed resources..

Muhammed Effat Ahmed Afifi
par Muhammed Effat Ahmed Afifi , Project Leader [Microsoft Business Unit] , Wipro Arabia Limited

The .NET garbage collector provides a high-speed allocation service with good use of memory and no long-term fragmentation problems.
Garbage collection in the Microsoft .NET common language runtime (CLR) environment completely absolves the developer from tracking memory usage and knowing when to free memory.
About the mechanism of GC, resource allocation and finalization and more kindly read carefully the content in the following link : http://msdn.microsoft.com/en-us/magazine/bb985010.aspx http://msdn.microsoft.com/en-us/library/ms973837.aspx About invoke the GC programmatically , It is "Not" possible because in .net there is GC.Collect() it'll tell it to run a collection.
But, it won't collect specific objects.
The GC is non-deterministic in relation to which objects are collected or not and when.
Note : don't forget to vote up my answer if it helps you ...

The .NET Garbage Collector (GC) manages the managed heap automatically.
It has Different Generations (Memory Levels) in which CLR places new instantiated objects.
One the Level - 0 is full, the GC is called by CLR and it claims the memory of UNUSED objects, and promotes in use objects to the next Generation (Level - 1).
You should not call GC directly, but you can do using GC.Collect.
Moreover, GC just reclaims memory, Underlying OS resources like file handles, DB connections should be closed/disposed explicitly by the programmer.

Hazem Salama
par Hazem Salama , Senior Member of Technical Staff , Verizon Communications

When writing C code, you used to track your memory allocation and de-allocate them when you are done with them.
For example, if you created an array, some memory has been allocated to that array depending on its size.
Now, if you don't de-allocate that memory block, it will stay occupied forever.
The more you have of these, the faster you run out of memory.
Modern languages like Java, C#, etc take on that responsibility by keeping count as to how many pointers are pointing to that piece of memory, once that reaches zero, it is de-allocated.
However, that process does run all the time, it runs randomly.
But you can always invoke it via code, like so GC.Collect which will force the collector to run immediately.
You can read more about the GC class here http://msdn.microsoft.com/en-us/library/system.gc.aspx

Maher Alsilwy
par Maher Alsilwy , CONSULTANT , B7R DESIGN WWW.B7RDES.COM

There are two method in which the garbage collector will be invoked: - Collect: in this method the garbage collector will identifies the unused space and compact them together to create one single block of memory space and also adjust address dependent constants.
- Destroy: it is the process of releasing memory based on the scope collected during *Collect process.
For example if there is no room for the new object in memory, the garbage collector comes into the picture to identifies the unused objects and free them from the memory.
*Collect process: is the process of scanning the complete source code and find the details like, class names, is it inherited?, static variables, public functions,...etc.
Collect process also finds out the scope of variables/objects, and the storage classes (auto,register,static,extern)

Anas KVC
par Anas KVC , Senior Software Engineer , Cerulen Infotech

"The garbage collector checks to see if there are any objects in the heap that are no longer being used by the application.
If such objects exist, then the memory used by these objects can be reclaimed" Just call GC.Collect(); to invoke gc programatically.

More Questions Like This