GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes GC help 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 "GC help" Watch "GC help" New topic
Author

GC help

Raksha Mishra
Greenhorn

Joined: Jul 11, 2010
Posts: 10
Hi ,

Self test (Kathy Sierra Ch3 :Q1) :-

Q:
When //doStuff is reached how many objects are eligible for GC ?

----I was wondering why only c1 & it's Short object are eligible for GC ....c3 is assigned a null reference via c1.go(c2){c2=null}....won't that be eligible as well ??
please help...Thanks in advance...
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

Welcome to javaranch Raksha. Plese post your code inside the CODE tags. That will make it easier for people to read your post and thereby increase the chances of you getting a good response


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

UseCodeTags when posting your coding. You don't create any object for variable c3. It's just a variable. So it isn't eligible for GC!

And Welcome to JavaRanch!~


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 444
    
    1

we are assigning null to c3 but before that c3 was not referring to some object
you already considered that the c3 object was referring to some object
Remember the c3 is mere a reference variable of the type CardBoard,
the variable has not been assigned some object

if we had written like this

then your assumption would have been right
the object referred by c3 would have been eligible for garbage collection because by making c3 null
the reference to the object through c3 is lost

merely creating a reference variable and assigning null value to it neither creates object nor makes some object eligible for GC
hope this helps


SCJP 6 [86%], OCPWCD [84%], OCEJPAD [83%]
If you find any post useful, click the "plus one" sign on the right
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

why is c1 and it's short variable eligible for garbage collection ?

an object is eligible for GC,if there is no more references to it.
new CardBoard();
can still be accessed using c2


OCPJP 6.0 93%
OCPJWCD 5.0 98%
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

mohitkumar gupta wrote:why is c1 and it's short variable eligible for garbage collection ?

an object is eligible for GC, if there is no more references to it.

Let's put that coding in a Code Tag.


After Line 01, can you access the Object which we've created in the Line Ob 01?
Raksha Mishra
Greenhorn

Joined: Jul 11, 2010
Posts: 10
Thanks a lot everybody...
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Abimaran Kugathasan wrote:
mohitkumar gupta wrote:why is c1 and it's short variable eligible for garbage collection ?

an object is eligible for GC, if there is no more references to it.

Let's put that coding in a Code Tag.


After Line 01, can you access the Object which we've created in the Line Ob 01?




when line 01 is removed how many object are eligible for garbage collection.........................


SCJP6.0,My blog Ranchers from Delhi
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

phil sohan wrote:
when line 01 is removed how many object are eligible for garbage collection.........................


Nothing will be eligible for Garbage Collection...
sumit kothalikar
Ranch Hand

Joined: Apr 15, 2010
Posts: 91


class CardBoard{
Short story = 200 ;
CardBoard go(CardBoard cb) {
cb = null ;
return cb ;
}
public static void main(String [] args){
CardBoard c1 = new CardBoard();
CardBoard c2 = new CardBoard();
CardBoard c3 = c1.go(c2) ;
c1 = null ;
// do stuff
}
}


So there are two objects eligible for garbage collection one is c1 and
other one is instance Short variable story of CardBoard class


Thanks & Regards
Sumit Kothalikar
Gary Marshall
Ranch Hand

Joined: Feb 19, 2007
Posts: 121
I had the same nagging concern when I reviewed the answer to this question. I'm glad that I decided to search this forum and found this posting.

I was under the belief that "null" is a value when in comes to a reference variable. After reviewing the responses that the kind folks placed into this posting, I looked back in Chapter 3 of the K&B book. Take a look at the first line item in the chart at the bottom of page 193 as well as the text on page 195, and there is the answer. While the code:
returns null, as stated in the text, "A null value means the reference variable is not referring to any object on the heap." So C3 does not qualify for GC.

Thank you Java Ranch!

G


G
 
GeeCON Prague 2014
 
subject: GC help