Title: Garbage Collection
1Garbage Collection
- What is garbage and how can we deal with it?
- Garbage collection schemes
- Reference Counting
- Mark and Sweep
- Stop and Copy
- A comparison
2How Objects are Created in Java
- An object is created in Java by invoking the
new() operator. - Calling the new() operator, the JVM will do the
following - allocate memory
- assign fields their default values
- run the constructor
- a reference is returned.
3How Java Reclaims Objects Memory
- Java does not provide the programmer any means to
destroy objects explicitly - The advantages are
- No dangling reference problem in Java
- Easier programming
- No memory leak problem
4What is Garbage?
Ali Object
- Garbage unreferenced objects
- Student ali new Student()
- Student khalid new Student()
- alikhalid
- Now ali Object becomes a garbage,
- It is unreferenced Object
ail
khalid
Khalid Object
5What is Garbage Collection?
- What is Garbage Collection?
- Finding garbage and reclaiming memory allocated
to it. - When is the Garbage Collection process invoked?
- When the total memory allocated to a Java program
exceeds some threshold. - Is a running program affected by garbage
collection? - Yes, the program suspends during garbage
collection.
Refference to ali object
Refference to khalid object
Khalid Object
6Strategies for Handling Garbage
- Modern Societies produce an excessive amount of
waste? - What is the solution?
- Reduce
- Reuse
- Recycle
- The same Applies to Java!!!
7Reduce Garbage
- A Java program that does not create any objects
does not create garbage. - Objects used until the end of the program do not
become garbage. - Reducing the number of objects that may be used
until the end of the program will reduce the
amount of garbage generated.
8Reuse Garbage
- Reuse objects instead of generating new ones.
- for (int i0ilt1000000 i)
- SomeClass obj new SomeClass(i)
- System.out.println(obj)
-
- This program generates one million objects and
prints them out. - SomeClass obj new SomeClass()
- for (int i0ilt 1000000 i)
- obj.setInt(i)
- System.out.println(onj)
-
- Using only one object and implementing the
setInt() method, we dramatically reduce the
garbage generated.
9Recycle Garbage
- Don't leave unused objects for the garbage
collector. - Put them instead in a container to be searched
when an object is needed. - Advantage reduces garbage generation.
- Disadvantage puts more overhead on the
programmer.
10Helping the Garbage Collector
- Sometimes we need the garbage collector to run
more frequently. - How we can help the collector?
- Eleminate all references to objects that are no
longer needed - This can be done by assigning null to every
variable that refers to an object that is no
longer needed
11Reference Counting Garbage Collection
- Main Idea Add a reference count field for every
object. - This Field is updated when the number of
references to an object changes. - Example
- Object p new Integer(57)
- Object q p
p
57
refCount 2
q
12Reference Counting (cont'd)
- The update of reference field when we have a
reference assignment ( i.e pq) can be
implemented as follows - if (p!q)
-
- if (p!null)
- --p.refCount
- pq
- if (p!null)
- p.refCount
Example Object p new Integer(57) Object q
new Integer(99) pq
p
57
refCount 0
q
99
refCount 2
13Reference Counting (cont'd)
- What in case of indirect references?
- We can still use reference counting, provided we
consider all references to an object including
references from other objects. - Object p new Association(new Integer(57), new
Integer(99))
14Reference Counting (cont'd)
- When does reference counting fail?
- When head is assigned to null, first object
reference count becomes 1 and not zero - Reference counting will fail whenever the data
structure contains a cycle of references
ListElements
ListElements
ListElements
head
next
next
next
refCount 1
refCount 1
refCount 1
15Reference Counting (cont'd)
- Advantages and Disadvantages
- Garbage is easily identified.
- Garbage can be collected incrementally.
- - Every object should have a reference count
field. - Overhead for updating reference count fields.
- It fails in the case of cyclic references.
- It does not de-fragment the heap
16Mark-and-Sweep Garbage Collection
- It is the first garbage collection algorithm that
is able to reclaim garbage even for cyclic data
structures. - Mark and sweep algorithm consists of two phases
- mark phase
- sweep phase
-
- for each root variable r
- mark(r)
- sweep()
17Mark and Sweep (cont'd)
- void sweep()
- for each Object p in the heap
-
- if (p.marked)
- p.markedfalse
- else
- heap.release(p)
-
-
program
18Mark and Sweep (cont'd)
- Advantages
- It correctly identifies and collects garbage even
in the presence of reference cycles. - No overhead in manipulating references.
- Disadvantages
- The program suspends while garbage collecting.
- It does not De-Fragment the heap.
19Stop-and-Copy Garbage Collection
- This algorithm collects garbage and defragments
the heap. - The heap is divided into two regions active and
inactive. - When the memory in the active region is
exhausted, the program is suspended and - Live objects are copied to the inactive region
contiguously - The active and in active regions reverse their
roles - The Algorithm
- for each root variable r
- rcopy(r,inactiveHeap)
- swap (activeHeap,inactiveHeap)
20Stop-and-Copy Garbage Collection (cont'd)
head
- Object copy(Object p, Heap destination)
-
- if (pnull)
- return null
- if (p.forwardnull)
-
- qdestination.newInstance(p.class)
- p.forward q
- for each field f in p
-
- if ( f is primitive type)
- q.fp.f
- else
- q.f copy(p.f,
destination) -
- q.forware null
-
- return p.forware
-
inactive
A
active
null
B
null
C
null
21Stop-and-Copy Garbage Collection (cont'd)
- Advantages
- It works for cyclic data structures
- It Defragments the heap.
- Disadvantages
- All objects should be copied when the garbage
collector is invoked it does not work
incrementally. - It requires twice as much memory as the program
actually uses.