wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes 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 "Objects eligible for GC" Watch "Objects eligible for GC" New topic
Author

Objects eligible for GC

mohammad shaid
Ranch Hand

Joined: May 05, 2010
Posts: 86
From Deavaka's ExamLabSimulator scjp 5


According to me only 1 object must be available for GC.. but the answer is 2..(Have figured out using pen and paper)
How is that???

I want to know where i went wrong and wanna rectify before appearing the scjp exam..


Thanks & Regards,
shaad
rewati raman
Ranch Hand

Joined: May 22, 2010
Posts: 62
c.aob=a.aob();
this looks not correct.
mohammad shaid
Ranch Hand

Joined: May 05, 2010
Posts: 86
Rectified the code
vendikonda sravan
Ranch Hand

Joined: May 09, 2010
Posts: 38
Hi Shahid i'm jus try in to answer to this but not sure this is correct , i request experienced ranchers please do correct this if wrong.

Total no. of objects got created are 4
1.A a=new A(); //Object one
2. A b=new A(); //Object two
3. A c=new A(); //Object three
4.A d= new A().aob=new A();////Object four

As c.aob = null; we can not be access(reach) object created on line 4 above so available for GC.

then in line "c=b"now c also points(refers)the same object which got created on line 2 above
so the object which got created on line 3 is not reachable so available for GC .

Thanks
Sravan
mohammad shaid
Ranch Hand

Joined: May 05, 2010
Posts: 86
A d=new A().aob=new A(); // fourth object also made to point to fifth object


Hi Sravan.. the line above creates 2 objects .. and it has no links with the other objects created there.. and according to me the object that will be GCted id c.aob as it will be referred to null;;
A a=new A(); // first object
A b=new A(); // second object
A c=new A();// third object

creates 3 objects then
a.aob=b;
will point to what b is pointing (i.e second object)

b.aob=a;
will point to what a is pointing (i.e First object)
c.aob=a.aob;
c.aob will point to what a.aob is pointing (i.e second object)
c=b;
c will be made to point to what b is refering(i.e second object)
but the object which c was refering earlier is till refering to second object(c.aob=a.aob)
c.aob=null;
and this is where that object( c.aob refering to what a.aob was refering) is made null ;

so just 1 object;;;

guys please correct me if i am wrong!



Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18553
    
  40


Basically, you need to remember that objects are GC'ed, and not references. So... you need to track the objects and their reachability -- and not whether something is set to null or not.

Anyway, two objects will be gc'ed. They are...

1. The object created at line 6, will become unreachable after line 11.
2. One of the objects created at line 10, will become unreachable after the same line (line 10).

Henry

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
mohammad shaid
Ranch Hand

Joined: May 05, 2010
Posts: 86
One of the objects created at line 10, will become unreachable after the same line (line 10).

how???

the other one is correct.. but my question is how is this happening at line 10 that one of the object here becomes unreachable ??
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18553
    
  40

mohammad shaid wrote:
but my question is how is this happening at line 10 that one of the object here becomes unreachable ??


Let's turn the question around... can you tell us, how the two objects created at line 10 are both still reachable?

Henry
mohammad shaid
Ranch Hand

Joined: May 05, 2010
Posts: 86
A d=new A().aob=new A();

d reference var is pointing to one object which is made to point to another object.the link is not broken... In short no object is released..

thats the way i am looking at it.. please clarify how we need to look at this ??
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18553
    
  40

mohammad shaid wrote:
thats the way i am looking at it.. please clarify how we need to look at this ??



Well... it looks like this ...



This expression...



evaluates to something like this...



The d reference is assigned to the result of the other assignment. And the result of the assignment is the RHS of the assignment.

Henry
mohammad shaid
Ranch Hand

Joined: May 05, 2010
Posts: 86

(aob)
Obj1 ------------> Obj2
^
|
|
|
d ------------------------+


so obj1 here becomes GCed... thanks a lot brother.. i got it now...
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
This is Like

A d = Instance-Varibale = new A();

Therefore d= same #s , the one goes into Instance variable

obj1--->obj2


d-------->Obj2 and (d is not pointing to Obj1)

Hence obj1 is also eligible for GC.
Other Object eligible for GC is indeed clear.

Hence asnswer is 2 and not 1.

Tricky Question Indeed.



SCJP 6.0 96%

(Connecting the Dots ....)
 
Don't get me started about those stupid light bulbs.
 
subject: Objects eligible for GC
 
Similar Threads
Problem in Garbage collector
Garbage Collection
question on garbage collection
GC program needs explanation
Garbage Collection Question