الأخوة الكرام
لدي هذه الاطروحات عن garbag collector
وأرجو أن تساعدوني في فهم معناها..(Garbage collection (GC:
is a technology that frees programmers from the hassle of explicitly managing memory allocation for every object they create. Traditionally, the benefit of this automation has come at the cost of significant overhead. However, more efficient algorithms and techniques, coupled with the increased computational power of computers have made the overhead negligible for all but the most extreme situations.
Garbage Collection :
The Automatic memory management .NET frameworks Garbage Collector manages allocation and release of memory for our application. (Hey do you know ,Application memory is divided in Stack and Heap).Garbage collector is a low priority thread under normal circumstances. When memory becomes limited, Garbage collector becomes high priority and release memory automatically. But we do not actually know when Garbage Collector does the job.
Garbage collection:
is the process that frees up the memory after an object is no longer referenced and thus no longer used.
Garbage collection:
is not a new thing and is used in languages like JAVA and there are implementations of it for C++ and C as well.
When the garbage collection is kicked off it will evaluate the heap and check which memory spaces are in use or being referenced. If a space is no longer used it will clear that memory. Once this is complete the next task is to compress the memory and remove the gaps between the used memory, pushing all the unused memory to the end of the heap. And then of course change the addresses for the referencing objects to point to the correct memory address.
Garbage Collection in Various Environments
One of the new features inherently available in the .Net framework is automatic garbage collection. The term garbage collection can be defined as management of the allocation and release of memory in an application. In C++, developers are responsible for allocating memory for objects created in the application and releasing the memory when the object is no longer needed. COM introduced a new model for memory management – Reference counting. Programmers were only responsible for incrementing the reference count when the object is referenced and decrementing the counter when the object goes out of scope. When the object’s reference count reaches zero, the object is deleted and the memory gets freed. Both these schemes are dependent on the developer and result from time to time in memory leaks and application exceptions – conditions that occur at run-time and are not detectable at compilation.
Garbage Collection in .Net
The garbage collector in .Net takes care of bulk of the memory management responsibility, freeing up the developer to focus on core issues. The garbage collector is optimized to perform the memory free-up at the best time based upon the allocations being made. Java developers have enjoyed the benefits of Garbage collection. VB developers are also used to a certain amount of flexibility in these terms and .Net provides full-fledged memory management capabilities for managed resources
System.GC class
The System.GC class provides methods that control the system garbage collector. Use methods from this class in your application with extreme caution.
Programmatically Invoking the Garbage Collector
In applications with significant memory requirements, you can force garbage collection by invoking the GC.Collect method from the program. This is not recommended and should be used only in extreme cases.
Garbage Collection basis
Almost every program uses resources such as database connection, file system objects etc. In order to make use of these things some resources should be available to us.
First we allocate a block of memory in the managed memory by using the new keyword. (This will emit the new obj. instruction in the Microsoft intermediate language code generated from C#, VB.NET, Jscript.NET or any other .NET compliance language).
Use the constructor of the class to set the initial state of the object.
Use the resources by accessing the type’s members.
At last CLEAR THE MEMORY.
However for automatic memory management, the garbage collector has to know the location of the roots i.e. it should know when an object is no longer in use by the application. This knowledge is made available to the GC in .NET by the inclusion of a concept know as metadata. Every data type used in .NET software includes metadata that describes it. With the help of metadata, the CLR knows the layout of each of the objects in memory, which helps the Garbage Collector in the compaction phase of Garbage collection. Without this knowledge the Garbage Collector wouldn't know where one object instance ends and the next begins.
Garbage Collection Algorithm
Application Roots
Every application has a set of roots. Roots identify storage locations, which refer to objects on the managed heap or to objects that are set to null.
For example:
• All the global and static object pointers in an application.
• Any local variable/parameter object pointers on a thread's stack.
• Any CPU registers containing pointers to objects in the managed heap.
• Pointers to the objects from Reachable queue
• The list of active roots is maintained by the just-in-time (JIT) compiler and common language runtime, and is made accessible to the garbage collector's algorithm.


