garbage collection garbage collection is dynamic approach used for automatic memory management to reduce the memory leak problems the garbage collection process identifies unused memory blocks and reallocates that storage for reuse garbage collection is implemented using the following approachesmark-and-sweep in this approach when memory runs outthe garbage collection process locates all accessible memory and then reclaims the available memory reference counting the garbage collection process here maintains reference count of referencing number for each allocated object when the memory count becomes zerothe object is marked as garbage and then destroyed by freeing its memory the freed memory is finally returned to the memory heap copy collection the garbage collection process maintains two memory partitions when the first partition is fullthe garbage collection process identifies all accessible data structures and copies them to the second partition then it compacts memory to allow continuous free memory note some programming languages like javac#netetc have built-in garbage collection process to self-manage memory leak problem advantages of garbage collection garbage collection frees the programmer from manually dealing with memory de-allocationthereby eliminating or substantially reducing following types of programming bugsdangling pointer bugs are often encountered when the memory allocated to variable (or an objectis freed but there are still pointers pointing to it if such pointers are de-referenced especially when that memory is re-allocated to another variable or objectthen results are simply unpredictable double free bugs occur when the program tries to free piece of memory that has already been freed it may be case that the freed memory has now been re-allocated to some other variable or object of the same or different program in such casethe program will again give erroneous results memory leaks occur when program is unable to free memory occupied by objects that have become unreachable garbage collection also helps in efficient implementation of persistent data structures