Garbage Collection Introduction What is garbage and how can we deal with it? Garbage collection...

21
Garbage Collection Introduction What is garbage and how can we deal with it? Garbage collection schemes Reference Counting Mark and Sweep Stop and Copy A comparison

Transcript of Garbage Collection Introduction What is garbage and how can we deal with it? Garbage collection...

Page 1: Garbage Collection Introduction What is garbage and how can we deal with it? Garbage collection schemes Reference Counting Mark and Sweep Stop and Copy.

Garbage Collection

Introduction

What is garbage and how can we deal with it?

Garbage collection schemes

Reference Counting

Mark and Sweep

Stop and Copy

A comparison

Page 2: Garbage Collection Introduction What is garbage and how can we deal with it? Garbage collection schemes Reference Counting Mark and Sweep Stop and Copy.

How 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.

Page 3: Garbage Collection Introduction What is garbage and how can we deal with it? Garbage collection schemes Reference Counting Mark and Sweep Stop and Copy.

How Java Objects Memory Reclaimed

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

Page 4: Garbage Collection Introduction What is garbage and how can we deal with it? Garbage collection schemes Reference Counting Mark and Sweep Stop and Copy.

What is Garbage?

Garbage: unreferenced objects

Student ali= new Student();

Student khalid= new Student();

ali=khalid;

Now Ali Object becomes a garbage,

It is unreferenced Object

Ali Object

ail

khalid

Khalid Object

Page 5: Garbage Collection Introduction What is garbage and how can we deal with it? Garbage collection schemes Reference Counting Mark and Sweep Stop and Copy.

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

Page 6: Garbage Collection Introduction What is garbage and how can we deal with it? Garbage collection schemes Reference Counting Mark and Sweep Stop and Copy.

Reduce, Reuse, Recycle

Modern Societies produce an excessive amount of waste?

What is the solution?

Reduce

Reuse

Recycle

The same Applies to Java!!!

Page 7: Garbage Collection Introduction What is garbage and how can we deal with it? Garbage collection schemes Reference Counting Mark and Sweep Stop and Copy.

Reduce 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.

Page 8: Garbage Collection Introduction What is garbage and how can we deal with it? Garbage collection schemes Reference Counting Mark and Sweep Stop and Copy.

Reuse GarbageReuse objects instead of generating new ones.

for (int i=0;i<1000000; ++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 i=0;i< 1000000; ++i) { obj.setInt(i); System.out.println(onj);

Using only one object and implementing the setInt() method, we dramatically reduce the garbage generated.

Page 9: Garbage Collection Introduction What is garbage and how can we deal with it? Garbage collection schemes Reference Counting Mark and Sweep Stop and Copy.

Recycle 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.

Page 10: Garbage Collection Introduction What is garbage and how can we deal with it? Garbage collection schemes Reference Counting Mark and Sweep Stop and Copy.

Helping the Garbage Collector

Sometimes we need the garbage collector to run more frequently.

How we can help the collector?

Eliminate 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

Page 11: Garbage Collection Introduction What is garbage and how can we deal with it? Garbage collection schemes Reference Counting Mark and Sweep Stop and Copy.

Reference 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.

ExampleObject p= new Integer(57);Object q = p;

57

refCount = 2

p

q

Page 12: Garbage Collection Introduction What is garbage and how can we deal with it? Garbage collection schemes Reference Counting Mark and Sweep Stop and Copy.

Reference Counting (cont'd)The update of reference field when we have a reference

assignment ( i.e p=q) can be impelmented as follows

if (p!=q){ if (p!=null) --p.refCount; p=q; if (p!=null) ++p.refCount;}

Object p = new Integer(57);

Object q= new Integer(99);

p=q;

57

refCount = 0

p

q

99

refCount = 2

Page 13: Garbage Collection Introduction What is garbage and how can we deal with it? Garbage collection schemes Reference Counting Mark and Sweep Stop and Copy.

Reference 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.

Page 14: Garbage Collection Introduction What is garbage and how can we deal with it? Garbage collection schemes Reference Counting Mark and Sweep Stop and Copy.

Reference 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));

Page 15: Garbage Collection Introduction What is garbage and how can we deal with it? Garbage collection schemes Reference Counting Mark and Sweep Stop and Copy.

Reference 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

next

refCount = 1

ListElements

refCount = 1

ListElements

next

refCount = 1

ListElements

next

head

Page 16: Garbage Collection Introduction What is garbage and how can we deal with it? Garbage collection schemes Reference Counting Mark and Sweep Stop and Copy.

Mark-and-Sweep Garbage Collection

It is the first garbage collection algorithm that is able to reclaim cyclic data structures.

Mark and sweep algorithm consists of two phases:

mark phase

sweep phase

for each root variable r mark(r); sweep();

Page 17: Garbage Collection Introduction What is garbage and how can we deal with it? Garbage collection schemes Reference Counting Mark and Sweep Stop and Copy.

Mark and Sweep (cont'd)void sweep(){ for each Object p in the heap { if (p.marked) p.marked=false; else heap.release(p);

}}

program

Page 18: Garbage Collection Introduction What is garbage and how can we deal with it? Garbage collection schemes Reference Counting Mark and Sweep Stop and Copy.

Mark 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.

Fragmentation problem.

Page 19: Garbage Collection Introduction What is garbage and how can we deal with it? Garbage collection schemes Reference Counting Mark and Sweep Stop and Copy.

Stop-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 inactive regions reverse their roles

The Algorithm

for each root variable r r=copy(r,inactiveHeap);swap (activeHeap,inactiveHeap);

Page 20: Garbage Collection Introduction What is garbage and how can we deal with it? Garbage collection schemes Reference Counting Mark and Sweep Stop and Copy.

Stop-and-Copy Garbage Collection (cont'd)Object copy(Object p, Heap destination) { if (p==null) return null; if (p.forward==null) {

q=destination.newInstance(p.class); p.forward= q; for each field f in p { if ( f is primitive type) q.f=p.f; else q.f= copy(p.f,

destination); }q.forware = null;

}return p.forware;

}

A’

null

B’

null

C’

null

headinactive

active

Page 21: Garbage Collection Introduction What is garbage and how can we deal with it? Garbage collection schemes Reference Counting Mark and Sweep Stop and Copy.

Stop-and-Copy Garbage Collection (cont'd)

Advantages

Defragments the heap.

Disadvantages

All objects should be copied when the garbage collector is invoked.

It requires twice as much memory as the program actually uses.