Unit33 GarbageCollection
Unit33 GarbageCollection
What is Garbage?
Garbage: unreferenced objects
Ali Object
ail
khalid
Khalid Object
Mark-and-sweep
Stop-and-copy garbage collection.
Example
Object p= new Integer(57); Object q = p;
57 refCount = 0
q 99 refCount = 2
head
ListElements next
refCount = 1
ListElements next
refCount = 1
ListElements next
refCount = 1
Disadvantages
Reference counting does not detect garbage with cyclic references. The overhead of incrementing and decrementing the reference count each time. Extra space: A count field is needed in each object. It may increase heap fragmentation.
Disadvantages
It may increase heap fragmentation. It does work proportional to the size of the entire heap. The program must be halted while garbage collection is being performed.