garbage collecton

A

asit

Just look into the code snippet

class A{
static int ct;
A aob;
String name;
public A() {
ct++;
name = ct + "";
System.out.println("cretaed : " + name);
}
protected void finalize() {
System.out.println("freed : " + name);
}
public static void main(String args[]){
A a=new A();
A b=new A();
A c=new A();
a.aob=b;
b.aob=a;
c.aob=a.aob;
A d=new A().aob=new A();
c=b;
c.aob=null;
System.gc();
}
}

The question was when the control reaches System.gc() line, how many
objects should be eligible for CG ??

My analysis shows it should be 1, but why the output indicates 2
objects ???
 
A

Abu Yahya

asit said:
Just look into the code snippet

class A{
static int ct;
A aob;
String name;
public A() {
ct++;
name = ct + "";
System.out.println("cretaed : " + name);
}
protected void finalize() {
System.out.println("freed : " + name);
}
public static void main(String args[]){
A a=new A();
A b=new A();
A c=new A();
a.aob=b;
b.aob=a;
c.aob=a.aob;
A d=new A().aob=new A();
The first object being created here is not the one being assigned to d.
Rather, the object referenced by the member named "aob" of this first
object is being assigned to d. Since the reference to this (first)
object is lost, it is eligible for garbage collection.

Also, if you look at the value of d while debugging, you can see that
"aob" of d is null, since it has not been assigned any value.

The reference to the third object created is lost. So, this is another
object eligible for gc (the one you probably guessed correctly about).
c.aob=null;
System.gc();
The output I get from this line when I step over it is:
freed : 3
freed : 4

which indicates that the 3rd and 4th objects created are being freed.
 
R

Roedy Green

The question was when the control reaches System.gc() line, how many
objects should be eligible for CG ??

I modified your program to help you figure out what it is doing:

// explore how objects become accessible for garbage collection.
import static java.lang.System.out;
class A
{

/** id for objects */
String name;
A aob;

public A( String name )
{
this.name = name;
System.out.println("created : " + name);
}

protected void finalize()
{
System.out.println("freed : " + name);
}

public static void main(String args[])
{
A a=new A( "a" );
A b=new A( "b" );
A c=new A( "c" );
a.aob=b;
b.aob=a;
c.aob=a.aob;
// Confusing code. Don't cascade operators. You will just trip
yourself or other programmers.
A d=new A("mother").aob=new A("child");
c=b;
c.aob=null;
if ( a != null ) out.println("a: " + a.name );
if ( b != null ) out.println("b: " + b.name );
if ( c != null ) out.println("c: " + c.name );
if ( d != null ) out.println("d: " + d.name );
if ( a != null && a.aob != null ) out.println("a.aob: " +
a.aob.name );
if ( b != null && b.aob != null ) out.println("b.aob: " +
b.aob.name );
if ( c != null && c.aob != null ) out.println("c.aob: " +
c.aob.name );
if ( d != null && d.aob != null ) out.println("d.aob: " +
d.aob.name );
System.gc();
}
}


the output is :
created : a
created : b
created : c
created : mother
created : child
a: a
b: b
c: b
d: child
a.aob: b
freed : mother
freed : c


You see it creates 5 A objects named a, b, c, mother and child.
Then you pull your little shell game.

When in is all done, objects a, b, and child are still pointed to.
mother and c are dangling, ready for GC to pick them off.

You may have have confused yourself by cascading the = operator:

A d=new A("mother").aob=new A("child");


leaves d pointing to child, not mother!!!
--
Roedy Green Canadian Mind Products
http://mindprod.com

If everyone lived the way people do in Vancouver, we would need three more entire planets to support us.
~ Guy Dauncey
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top