aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How many objects eligible for GC?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How many objects eligible for GC??" Watch "How many objects eligible for GC??" New topic
Author

How many objects eligible for GC??

ashni Prakash
Ranch Hand

Joined: Dec 05, 2006
Posts: 50

class Car {}
class Ferrari extends Car {}
class Camry extends Car {}

public class GC
{
public static void main(String[] args)
{
Car a1 = new Ferrari();
Car a2 = new Camry();
Car a3 = a1;
a1 = null;
Car a4 = a1;
Car a5 = a2;
a1 = a2;
a2 = a4;
a5 = null;
a3 = a1;

// Here
}
}


My doubt is while GC wether we must count reference varaibles equal to null or count objects without any references .Please someone help me in understanding this.
Jitendra Jha
Ranch Hand

Joined: Jan 28, 2007
Posts: 91
I guess objects not pointed by any reference variables are eligible for gc.we garbage collect objects not reference variables.
for example if an object is pointed by 2 variables and one of them is set to null,it is still being pointed by the other and hence will not be collected.

gc can also be called when there exists an island of isolation.but that is not the case with your code.by the time the execution reaches //Here
all reference variables are null,hence both objects will be eligible for collection


Jitendra
SCJP1.5
SCWCD1.5
Varalakshmi Ramanarayan
Ranch Hand

Joined: Oct 28, 2007
Posts: 108
As per my calculations only ferrari object is eligible for GC.
a3 and a1 are still pointing to camary at //here.

Am I wrong?
[ December 04, 2007: Message edited by: Varalakshmi Ramanarayan ]

SCJP 5 - 87%<br />Next SCWCD
Jitendra Jha
Ranch Hand

Joined: Jan 28, 2007
Posts: 91
As a matter of fact my calculations were wrong.
Only ferrari is eligible.

my sincerest apology
Jitendra Jha
Ranch Hand

Joined: Jan 28, 2007
Posts: 91
As a matter of fact my calculations were wrong.
Only ferrari is eligible.

my sincerest apology
nico dotti
Ranch Hand

Joined: Oct 09, 2007
Posts: 124
I agree that a1 and a3 are still pointing to Camry and Ferrari is eligible FWIW, I have to draw pictures for the GC stuff. I use solid lines to objects and then when a reference 'breaks away' I use dotted lines. Works for me.
nico dotti
Ranch Hand

Joined: Oct 09, 2007
Posts: 124
I agree that a1 and a3 are still pointing to Camry and Ferrari is eligible FWIW, I have to draw pictures for the GC stuff. I use solid lines to objects and then when a reference 'breaks away' I use dotted lines. Works for me.
ashni Prakash
Ranch Hand

Joined: Dec 05, 2006
Posts: 50
Hi all,

Thanks for all your reply...
Only ferrari is eligible for Gc...means 1 object...
We should not consider reference varaible a2=a4=a5=null into count.Because if count them then its 4 objects available for GC.

Please someone tell me whether whatever i have understood is correct.
nico dotti
Ranch Hand

Joined: Oct 09, 2007
Posts: 124
I agree that a1 and a3 are still pointing to Camry and Ferrari is eligible FWIW, I have to draw pictures for the GC stuff. I use solid lines to objects and then when a reference 'breaks away' I use dotted lines. Works for me.
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
For an example of how to use a diagram-based approach to GC questions, check out Burkhard's ultra nifty post from an older thread.


SCJP 5.0
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
I have a question..
If objct A is = object B
and Object B = Object C,
Then would it automatically mean..
Object A = Object c ??


The future belongs to those who believe in the beauty of their dreams.Dream BIG!
 
 
subject: How many objects eligible for GC??