The main function of the garbage collector, as far as .NET framework is concerned, is allocating and releasing the memory required for the application to run.
2. The main function of the garbage collector, as far as .NET framework is
concerned, is allocating and releasing the memory required for the application
to run. Whenever you define or create a new object, the .NET library better
known as the Common Language Runtime or CLR in short, is responsible for
assigning memory to the recently created object. The memory is borrowed from
the managed heap which can be called as an ocean of memory in layman’s
language. As far as this heap is available, the runtime library goes on to
allocate memory for newer objects created. One thing you must know as a
developer is that, the memory available is limited. After repeated use of it, the
garbage collector comes into the picture. It collects and frees the memory. The
optimizing engine decides the time for garbage collection and commands the
collector accordingly. The time is decided based on the memory allocations
made.
3.
4. What the garbage collector does is that, it looks for those objects stored in the
memory that are no more in use. It does the necessary operation to free their
memory and make it available for further use.
Whether the application is a web based one or a windows based one; memory
allocation and management is vital and a cause of concern. Therefore, one can
say that the garbage collector performs a very important task as far as .NET
framework is concerned. Now let us see the advantages of garbage collector.
5. Advantages of Garbage Collector in .NET:
1.Makes memory allocation for objects from the available managed heap.
2.Ensures that one object does not occupy other one’s memory. Thus, keeping an
eye on memory safety and security.
3.We can continue to develop an application without worrying about whether
sufficient memory is available for developing and running it. Garbage collector will
take care of it at the back end.
4.Frees the memory of those objects that are not in use and makes it available for
allocation in the future.
6. How is Memory Allocated in Managed Heap?
For creating a new object, the new operator is used. This new operator ensures
that the required memory or space by the object is available in the reserved
region. It also commits the storage space if needed. If the space is optimum and
the object fits, then the object pointer points in the heap and the constructor is
given a call. The address of the object is thus fetched.
The heap or the managed heap is nothing but, a bunch of memory segments.
Each of them is of fixed size about 16MB. When memory is to be assigned to a
new object, the next memory location on the heap is allocated. If there is sufficient
memory available, then the collector is not called in. If not, the garbage collector
searches and frees the old dead objects in the heap. Then new object is allocated
memory.
7. Some Important Points About Garbage Collector:
1.Memory allocation is carried out within a contiguous range of addresses.
2.The segments are divided into Gen 0, Gen 1 and Gen 2. Gen 0 and Gen 1 form
ephemeral segment. Gen2 are the next segments. Large object heap are yet
another set of segments.
3.Older objects are situated at lower memory locations, while newer are present at
higher address locations.
4.On a regular basis, the heap is freed of garbage and memory is made available
for newer objects.
5.Objects are never separated by gaps in the memory.
8. This was the basic part of garbage collection in .NET.
Learn .net courses in pune to enhance your knowledge in dept.
Related articles:
Microsoft .Net Framework 4.0| 4.5 | 4.6 for Beginners
What is New About Open Source .Net?