The .Net Garbage
Collection offers a lot of advantages like frees the developers from having
to allocate and dispose memory for the objects they create in their programs,
using the most efficient way to allocate and dispose memory, etc.
Though there are many advantages of Garbage Collection, there are a few limitations in the way Garbage Collection operates; the following are some of the Limitations of Garbage Collection.
1. Unused objects are not disposed immediately:Though there are many advantages of Garbage Collection, there are a few limitations in the way Garbage Collection operates; the following are some of the Limitations of Garbage Collection.
When a method starts executing it creates object
instances, these objects are placed in the heap and references to these objects
are placed in stack in a separate stack frame. Once the method execution gets
competed the Stack frame is cleared thereby clearing the object reference in
the stack which points to the object in the Heap. But the actual object in the
Heap is not cleared, it remains in the Heap till the time Garbage Collection
runs. Garbage Collection runs only when the system runs short of physical memory
or manages heap space, hence unused objects will remain in memory till the time
Garbage Collection runs and clears them out.
2. Unused objects with references are not cleared.
If an object exists in the Heap without any reference from the Stack then the object is supposed to be cleared from the Heap, but if the object is making a reference to another object which is still being used then both the objects will not be cleared from the Heap. As a result the unused object is also retained in the Heap, many such un-used objects with references will lead to memory leaks.
3. Premature Out of Memory
2. Unused objects with references are not cleared.
If an object exists in the Heap without any reference from the Stack then the object is supposed to be cleared from the Heap, but if the object is making a reference to another object which is still being used then both the objects will not be cleared from the Heap. As a result the unused object is also retained in the Heap, many such un-used objects with references will lead to memory leaks.
3. Premature Out of Memory
Large objects are stored in Large Object Heap and
smaller objects are stored in Small Object Heaps.
When objects in the Small Object Heap get cleared the Heap is de-fragmented to rearrange objects in the heap in such a way that the free space gaps left between objects is consolidated and reused for other new objects.
When objects in the Large Object Heap get cleared, the heap is not de-fragmented; this leads to gaps in the Heap allocation. The run-time tries to assign the new objects into these gaps if possible. When the application runs for a longer period of time, a lot of gaps get created in the Heap. If these gaps are not sufficient enough to accommodate a new object then an Out of Memory error occurs though there is space in the Heap. The problem here is that Garbage Collection doesn't de-fragment the Large Object Heap to reclaim the space hence this issue.
When objects in the Small Object Heap get cleared the Heap is de-fragmented to rearrange objects in the heap in such a way that the free space gaps left between objects is consolidated and reused for other new objects.
When objects in the Large Object Heap get cleared, the heap is not de-fragmented; this leads to gaps in the Heap allocation. The run-time tries to assign the new objects into these gaps if possible. When the application runs for a longer period of time, a lot of gaps get created in the Heap. If these gaps are not sufficient enough to accommodate a new object then an Out of Memory error occurs though there is space in the Heap. The problem here is that Garbage Collection doesn't de-fragment the Large Object Heap to reclaim the space hence this issue.
4.
Unpredictable on when Garbage Collection will happen
Since Garbage Collection is automatic and controlled by
the run-time we cannot predict on when Garbage Collection will happen. When you
are performing a critical calculation, Garbage Collection might get triggered if
it finds that the available memory is low, this might affect your calculation
since there will be performance degradation when Garbage Collection runs.
No comments:
Post a Comment