aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes how many objects are available 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 are available for gc?" Watch "how many objects are available for gc?" New topic
Author

how many objects are available for gc?

Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
this is from whizlabs 1.4

how many string objects are eligible for gc when the program reaches line 8



1.0
2.10
3.11
4.none

i think the answer is 11 because 11 objects are created and upon reaching the line 8 all are available for gc since after the for loop the tmp variable is dead

but whizlabs says the answer is 10 since tmp is just out of scope and still has a reference

please explain

Hiruka Sandev
Greenhorn

Joined: Nov 22, 2009
Posts: 11
Ankur kothari wrote:
when the program reaches line 8


Doesn't this mean line 8 is yet to be executed?
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
no....line 8 isnt executed..just forget that line
rohan yadav
Ranch Hand

Joined: Oct 13, 2009
Posts: 156
Of course it will still has reference to tmp, because tmp is just out of scope doesnt necessarily mean it is eligible for gc


Sage of The Monstrous Toad of Mount Myoboku
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
but i think the variable tmp is dead after the loop
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
and please go by the // 8 i have given which is the line 8
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
so doesnt this mean that no one is referring to the any of the 11 objects(strings)
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
FYI -

Whenever you see a mock exam question that mixes String objects with GC eligibility, substitute objects with made up type (like MyClass), not String objects. The real exam NEVER mixes these because the String constant pool makes the whole GC topic too muddy.


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
ok so what if instead of strings there were MyClass objects....then how many of them would be available for gc after the for loop?
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
I also think there should be 11 objects available for gc.


SCJP 6
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
i wished there was a way to find this out....like compiling or running it
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
the answer for above would be 11 definitely but this one...will be 10...gc is all about reference variables




Hiruka Sandev
Greenhorn

Joined: Nov 22, 2009
Posts: 11
See this on invisible objects. According to whats said in that book the 11th object may not be eligible for GC until the method completes.

Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
this invisible objects topic confused me....the topic above it said that when the method returns it goes out of scope and object can be gc

in invisible objects topic it said that the object will be weakly held even after the block finishes...what is weak and string doing here?

can someone please clear my doubt
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Ankur kothari wrote:this invisible objects topic confused me....the topic above it said that when the method returns it goes out of scope and object can be gc

in invisible objects topic it said that the object will be weakly held even after the block finishes...what is weak and string doing here?

can someone please clear my doubt


Actually the topic is saying about method + reference variable placement on the stack.

Actually methods are loaded in the stack, and whatever objects are created in that method those objects' references are also loaded in the same stack with method.

Suppose you have main() method


Then what will happen here.
1. when control enters to main() method. man() is loaded in the stack.
2. Then from new Object() statement, a new Object is created in the heap.
3. objRef reference variable is created on the same stack where main() is loaded.
4. Reference of that new Object() on the heap is stored in objRef reference variable.

Now when you say this:

5. objRef=null; explicitly breaking the link between objRef on the stack and new Object() on the heap.
GC can see that new Object() on the heap is broken. So it can gc it after line 1.

But when this is done


here after line1 is executed just scope of objRef local reference variable is over, but we are not breaking the link between objRef and new Object() explicitly. And objRef in not destroyed until main() method returns and stack of main() is destroyed. So till main() is living on the stack, objRef is also living on the stack, but after line1 we cannot use objRef as it is not visible after that, it is called invisible after that.

But it is still residing on the stack and is invisible to us and keeping a reference of new Object() on the heap. Garbage collector can't collect new Object() from the heap as its reference is on the stack.






Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
hey but can we access the objref referecne outside the if loop? doesnt being able to acess the variable and scope be the same thing?

or do you mean variables are stored only in the stacks with the methods they are in and stay there until the method gets over...or returns
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Ankur kothari wrote:hey but can we access the objref referecne outside the if loop? doesnt being able to acess the variable and scope be the same thing?


Ya accessing and scope is the same thing, we cannot access objref outside the if block, but GC is a completely different thing.
Here objref is just marked as not accessible outside the block, it scope status is changed and set to not accessible, but its reference with new Object() in the heap is still alive but not usable by us, so just a memory waste or leak.

or do you mean variables are stored only in the stacks with the methods they are in and stay there until the method gets over...or returns


you are correct here.
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
superb punit...thanks a tonne....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how many objects are available for gc?