aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Objects available for GC.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Objects available for GC.." Watch "Objects available for GC.." New topic
Author

Objects available for GC..

Lakuma Yalamanchili
Greenhorn

Joined: Sep 24, 2008
Posts: 26
Hi,

Here's a code that I came across recently:

After line 6, how many objects are eligible for garbage collection?

The answer is "2" which I'm unable to comprehend...!!!

This is what I know:

1. C1 is given a reference to a newly created C's object on the heap (let's call it Obj_c1)
2. When m1(C1) is called, a copy of the reference to the same object above is passed, i.e., the reference of Obj_c1 is passed. We now have the copy of the reference in variable ob1 - local variable of the method!
3. Now, ob1 is given a new object's reference, let's call the object on the heap, Obj_ob1. Here, the reference to Obj_ob1 is returned to variable c1.
4. In the previous step, since the ref to Obj_c1 is copied into the local method variable, and this local variable is changed, there is no change to variable c1, which still holds the reference to Obj_c1.
5. After the method execution we now have, c1 -> Obj_c1 and c2->Obj_ob1
6. Now, c3 is given a new reference to the heap object Obj_c3
7. Line # 6 would create c2 to get the reference of c3, which is c2->Obj_c3
8. Finally there are three objects on the heap, Obj_c1, Obj_ob1 and Obj_c3
9. The only dangling object without a reference variable is Obj_ob1, as line 6 removes the reference and re-refers variable c2 to Obj_c3
10. So this makes only one object GC elligible!

Am I right with those steps above or is there a point where I made a BIG blunder!!!

Thanks in advance,
Lakuma

[ September 29, 2008: Message edited by: Lakuma Yalamanchili ]

[ September 29, 2008: Message edited by: Lakuma Yalamanchili ]
[ September 29, 2008: Message edited by: Lakuma Yalamanchili ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Well, this is one of the reasons why we require that you Quote Your Sources.

Thanks,
Henry


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

Joined: Dec 17, 2006
Posts: 49
4. In the previous step, since the ref to Obj_c1 is copied into the local method variable, and this local variable is changed, there is no change to variable c1, which still holds the reference to Obj_c1.


------------------------------------------------------------------------

As per my understanding the answer is 2. The refernce c1 is passed to the method, and inside the method this reference has been assigned a new object. So, the object created in the previous step is eligible for garbage colletion. (Don't get confused with reference being assigned to a local variable inside the method. The variable still holds refence to the object created in previous step.)

someone correct me if i'm wrong


-Aravind<br />SCJP 5.0 (91%), Preparing for SCBCD.
Lakuma Yalamanchili
Greenhorn

Joined: Sep 24, 2008
Posts: 26
Henry,

I'm sorry, I wasn't aware that we had to quote our source. Thanks for mentioning it Henry.

Here's where I got the source. Question# 13 in the link below:
http://www.examsguide.com/scjp/freequestions2.html

Aravind,

Thanks for your response! But, as far as I knew Java is pass by value and the local variable just has the same reference as c1, but is not c1 itself..!

I added print statements to the same source which kind of shows the value (address) that c1 holds before and after the method call m1 and they happen to be the same!!



OUTPUT:
c1 C@187c6c7
c1 after method call C@187c6c7
c2 C@10b62c9
c3 C@82ba41
c2 C@82ba41
Aravind Jerubandi
Ranch Hand

Joined: Dec 17, 2006
Posts: 49
Lakuma,

You are right. Thanks for correcting me!
Lakuma Yalamanchili
Greenhorn

Joined: Sep 24, 2008
Posts: 26
So is 1 the right answer or 2...?? As far as I know it's 1...but I wonder if a mock exam site can give a wrong answer..!!

Someone comment!!
Lakuma Yalamanchili
Greenhorn

Joined: Sep 24, 2008
Posts: 26
Noone to clarify this one here..?? It kind of questions my Java basics...if I'm wrong with this...looks like I'll have a lot of study to do!!
vidhya suvarna
Ranch Hand

Joined: Aug 28, 2008
Posts: 148
Lakuma, accordingly to me the answer should be 1.

The explaination provided by you is correct.
[ September 30, 2008: Message edited by: vidhya suvarna ]

SCJP 1.4 - 88%<br />SCWCD 1.5 - Preparing
aslika bahini
Ranch Hand

Joined: Mar 03, 2007
Posts: 111
Hi Lakuma,

According to me ob1(local var) is still referencing the obj-
ob1. Since, it is created inside the method there is no reference?
Please correct me.

Thank You
Saritha
Lakuma Yalamanchili
Greenhorn

Joined: Sep 24, 2008
Posts: 26
Vidhya,

Thanks for your update, I feel better..!!

Thanks again!

Saritha,

The local variable ob1 does refer to obj_ob1, but once the execution call is out of the method "m1", the local variable does not exist as the scope of it lies within the method. But the reference that it created is passed onto c2. So after the method execution:
  • Obj_ob1 exists
  • The reference is given to c2
  • and ob1 is destroyed!


  • Did I make it clear? Do let me know!

    Regards,
    Lakuma
    [ September 30, 2008: Message edited by: Lakuma Yalamanchili ]
    aslika bahini
    Ranch Hand

    Joined: Mar 03, 2007
    Posts: 111
    Hi Lakuma,

    Thank you very much. It's clear.

    Thanks
    Saritha
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Objects available for GC..