File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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?

ravi satti
Greenhorn

Joined: Oct 16, 2007
Posts: 13
How many objects eligible for garbage collection?

class Test{
String s;
public static void main(String agrs[]){
Test t = new Test();
t.s = new String("abc");
t = null;
System.gc();
}
}

Should the answer be 2 objects?

I had a doubt. Because when we instantiate the String object, we don't know how many other type of instances it has created inturn. When i have looked into the source code of the class String, it has some char array objects and other types as it's instance variables. So do we have to count those objects also for the number of objects eligible for garbage collection?

So technically for me the proper answer would be - 'can't be decided', until the source code for all the related classes is provided.

What should be the answer we have to give atleast in the exam perspective for such type of questions (even for user defined types if not String) ?
ravi satti
Greenhorn

Joined: Oct 16, 2007
Posts: 13
is anybody out there to answer the above question?
Doug Slattery
Ranch Hand

Joined: Sep 15, 2007
Posts: 294
Hi Ravindra,

Originally posted by ravindra rs:
How many objects eligible for garbage collection?

Should the answer be 2 objects?

So do we have to count those objects also for the number of objects eligible for garbage collection?

What should be the answer we have to give atleast in the exam perspective for such type of questions (even for user defined types if not String) ?


Since there are no other references to t and t.s, you are correct that 2 objects are eligible for GC.

The topics on GC in K&B (as well as the mock questions) are in this context. Objects instantiated within the declared objects are not considered for GC.

Hope this helps...
Aloha,
Doug

-- Nothing is impossible if I'mPossible
ravi satti
Greenhorn

Joined: Oct 16, 2007
Posts: 13
Thanks Doug,

So for the answer to the exam questions it is 2 objects, but technically there could be more than 2 objects eligible for GC. Right?
Doug Slattery
Ranch Hand

Joined: Sep 15, 2007
Posts: 294
Originally posted by ravi satti:
Thanks Doug,

So for the answer to the exam questions it is 2 objects, but technically there could be more than 2 objects eligible for GC. Right?


That's correct. You can think of it like a chain reaction. But, technically speaking, there could be more.

You should also read about isolated islands where two objects could point to each other. They should be eligible for GC, but don't. See K&B pg. 249-250 for details.

Aloha,
Doug
ravi satti
Greenhorn

Joined: Oct 16, 2007
Posts: 13
Thanks Doug for the reply
Rahul Shilpakar
Ranch Hand

Joined: Aug 29, 2006
Posts: 132
so how many objects are eligible for garbage collection?


Perform for today. Adapt for tomorrow.
ravi satti
Greenhorn

Joined: Oct 16, 2007
Posts: 13
it should be more than 2
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8815
    
    5
as always - the real exam will NEVER use objects of type String in a GC question. When you're studying for the exam and you see a mock question of this type substitute objects of a different type so that you don't start worrying about the String constant pool.


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Xyz Abc
Greenhorn

Joined: Aug 24, 2007
Posts: 27
Still didn't understand, which are the 2 objects here?
Doug Slattery
Ranch Hand

Joined: Sep 15, 2007
Posts: 294
Hi Nicole,

Originally posted by Nicole Facompre:
Still didn't understand, which are the 2 objects here?


The class t and its String member t.s

After t is set to null, there are no remaining references to it. As such,
t.s is sort of implicitly eligible as well.

Hope this helps...
Aloha,
Doug

-- Nothing is impossible if I'mPossible
Dinesh Tharanga
Greenhorn

Joined: Nov 29, 2006
Posts: 4
class Test{
String s;//Instance variable
public static void main(String agrs[]){
Test t = new Test();//inside "t" reference object "s" will live
t.s = new String("abc");//"s" refer new object it contains abc
t = null;//at this point cut "t" reference also "s" reference become useless(island of isolation)
System.gc();
}
}
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8815
    
    5
careful guys!

There is no such thing as a "reference object". There are objects, which are the only things that can be garbage collected, and then there are references to objects, which are NOT objects.

For the purpose of the exam, objects ALWAYS live in the heap (the exam doesn't get into constant pools in regards to GC). Remember, reference variables are NOT objects. That said, reference variables are usually thought of as living on the stack - and remember the stack's contents cannot be GCed - but objects can "contain" reference variables. So if an object "has" a reference variable, and the object gets GCed, then of course its reference variable gets GCed too - but that only counts as one object...hmmm...unless of course that object's reference variable referred to another object - then that object gets GCed, and IT counts as a GCed object too.

But the first thing you MUST get clear is that reference variables are not objects - and any exam question that asks "how many objects are eligible for GC" is talking about objects only - never any reference variables associated with these objects.
Doug Slattery
Ranch Hand

Joined: Sep 15, 2007
Posts: 294
Good catch Bert and thanks for the clarification. I keep forgetting about that String thing and GC...

Aloha,
Doug

-- Nothing is impossible if I'mPossible
 
wood burning stoves
 
subject: how many objects eligible for GC?