Garbage Collection
Advantages of Garbage Collection
Garbage Collection provides the following benefits −
- You dont need to free memory manually while developing your application.
- It also allocates objects on the managed heap efficiently.
- When objects are no longer used then it will reclaim those objects by clearing their memory, and keeps the memory available for future allocations.
- Managed objects automatically get clean content to start with, so their constructors do not have to initialize every data field.
- It also provides memory safety by making sure that an object cannot use the content of another object.
Conditions for Garbage Collection
Garbage collection occurs when one of the following conditions is true.
- The system has low physical memory.
- The memory that is used by allocated objects on the managed heap surpasses an acceptable threshold. This threshold is continuously adjusted as the process runs.
- The GC.Collect method is called and in almost all cases, you do not have to call this method, because the garbage collector runs continuously. This method is primarily used for unique situations and testing.