0% found this document useful (0 votes)
48 views14 pages

Garbage Collection

The document discusses memory management in C++ and Java. In C++, the delete operator is used to free memory for objects. In Java, garbage collection automatically reclaims memory from unused objects. For an object to be garbage collected, there must be no references to it. The finalize() method allows an object to perform cleanup before being removed by the garbage collector.

Uploaded by

Hitesh Kar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views14 pages

Garbage Collection

The document discusses memory management in C++ and Java. In C++, the delete operator is used to free memory for objects. In Java, garbage collection automatically reclaims memory from unused objects. For an object to be garbage collected, there must be no references to it. The finalize() method allows an object to perform cleanup before being removed by the garbage collector.

Uploaded by

Hitesh Kar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 14

visit www.muenggg.blogspot.

com for more study material

Objects dynamically allocated from a pool of free memory using new operator Possible for new to fail insufficient memory Key: Recovery of free memory from unused objects, making memory available for subsequent reallocation

visit www.muenggg.blogspot.com for more study material

C++ : delete operator to free memory


Java: Garbage automatically collection techniquereclaims objects

When no reference to an object exists, that object is assumed to be no longer used and memory occupied by object is recycled
Occurs only sporadically during execution of a program It will not occur simply because one or more objects exist that are no longer used

visit www.muenggg.blogspot.com for more study material

2 conditions are met


Objects to recycle need to recycle them Overhead : JVM does it only when necessary cannot know precisely collection will occur when garbage

visit www.muenggg.blogspot.com for more study material

Before an object is removed by garbage collection, finalize() method is invoked to give it a last opportunity to clean up its act and free other kind of resources it may hold
e.g. closing files , terminating network connections , non window resources ensures that object terminates cleanly

visit www.muenggg.blogspot.com for more study material

finalize() method is called once and only once before the object is garbage collected
No guarantee when this will happen may never run on a system that is not short of memory Finalization and Collection occur in two distinct phases of garbage collection process First items are finalized and then collected

visit www.muenggg.blogspot.com for more study material

Syntax
protected void finalize() { //finalization code here }

protected: access specifier that prevents access to finalize() by code defined outside its class

visit www.muenggg.blogspot.com for more study material

finalize() called before garbage collection


not called when object goes out of scope

visit www.muenggg.blogspot.com for more study material

class FDemo { int x ; FDemo (int i ) { x=i; }

visit www.muenggg.blogspot.com for more study material

protected void finalize() { System.out.println(Finalizing + x); }


void generator (int i) { FDemo o = new FDemo( i ); } }
visit www.muenggg.blogspot.com for more study material

class Finalize { public static void main(String args[]) { int count; FDemo ob = new FDemo(o); for (count = 1 ; count < 100000; count++) ob.generator(count); } }

visit www.muenggg.blogspot.com for more study material

Sometimes a method will need to refer to object that invoked it this keyword When a method is called it is automatically passed an implicit argument that is reference to invoking object. Reference is called this.

visit www.muenggg.blogspot.com for more study material

class Point extends Object { public double x ; public double y; Point() { x = 0.0; y = 0.0; }

visit www.muenggg.blogspot.com for more study material

Point (double x , double y) { this.x = x ; this.y = y; } }

visit www.muenggg.blogspot.com for more study material

You might also like