This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question with GC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question with GC" Watch "question with GC" New topic
Author

question with GC

ved dixit
Greenhorn

Joined: Feb 05, 2010
Posts: 25
I am preparing for SCJP 1.6
I came across this question from the link someone has posted in the forum of java ranch.
The link is here: http://enthuware.com/forum/viewtopic.php?f=16&t=4
I have a doubt for a question related to GC .


How many objects will be eligible for GC just after the method returns?



Ans : 1 .

Please explain the reason.



Thanks


Best Regards,
Ved.
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
object refered by 'a' is the only object in the method. 'x' is a variable of type int and 'str' refers to 'abc' which is string literal and will be in the string pool.
so there is only 1 object eligible for gc.


SCJP 1.6 96%
ved dixit
Greenhorn

Joined: Feb 05, 2010
Posts: 25
Hi Neha !!

Thanks for the reply.
So the Object a will be eligible for GC
on line 9 or at the end of the method on line 14 or
when the main method ends (Suppose i have called it from main())?

Thanks.
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
It will be available for gc at the end of method on line 14.
ved dixit
Greenhorn

Joined: Feb 05, 2010
Posts: 25
Thankss...i got it !!
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Hey ,but i studied that String is also an object


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

Joined: Nov 04, 2009
Posts: 2066

Shanky Sohar wrote:Hey ,but i studied that String is also an object

Shanky, it's true, that String is also an object. But, here, it's created as String literals on the Pool. So it's not eligible for GC!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

correct..Abimaran..
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8805
    
    5
on the real exam String objects are never used in GC questions


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

Joined: May 18, 2010
Posts: 634

why object a would be Gc ??
there exists a refernece to object a on line 8.

i think it would be GC when function is no longer needed...


OCPJP 6.0 93%
OCPJWCD 5.0 98%
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

GC basically happens when an object has no more reference to it.
The object created on line 8 has only 1 reference and that one goes out of scope end the end of line 14.
So after that it's GC'ed.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Trivikram Kamat
Ranch Hand

Joined: Sep 26, 2010
Posts: 155
Bert Bates wrote:
on the real exam String objects are never used in GC questions


But just out of curiosity, if Line 12 is replaced by:

will there be two objects available for GC after the method returns?


OCPJP6
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
I think yes.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Trivikram Kamat wrote:
But just out of curiosity, if Line 12 is replaced by:

will there be two objects available for GC after the method returns?

I think, the object on the heap will eligible for GC. And the String Literal Pool object won't GCed!

Trivikram Kamat
Ranch Hand

Joined: Sep 26, 2010
Posts: 155
Just read on ScjpFAQ about eligibility of String Literals for GC
It doesn't mention about String objects though.
Trivikram Kamat
Ranch Hand

Joined: Sep 26, 2010
Posts: 155
From Page 434 on K&B

SCJP6 Study Guide wrote:
String s = new String("abc"); // creates two objects,
// and one reference variable

In this case, because we used the new keyword, Java will create a new String object in normal (nonpool) memory, and s will refer to it. In addition, the literal "abc" will be placed in the pool.


This means if we put String str = new String("abc");, there will be two objects available for GC - Object of class Object and that of String.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Trivikram Kamat, did you go through that Faq? They have mentioned that,

The objects created in the String pool are not subjected to GC until the class is unloaded by the JVM. They get discarded at that time or just before the virtual machine unloads the class' bytecode.

So, the object, which is created on the literal pool, isn't eligible for normal GC but for VM shut down, etc...
Trivikram Kamat
Ranch Hand

Joined: Sep 26, 2010
Posts: 155
Hi Abimaran Kugathasan,

I agree with you, according to the explanation given in the FAQ on String Literal GC
But it's little confusing, when it says objects created in the String pool are not subjected to GC.

I guess String pool contains literals, and not objects.
Does String str = new String("abc"); creates object in the String pool memory?

According to K&B (as quoted in previous post), Java should create a new String object in normal (nonpool) memory and not in String pool.
However, the literal "abc" is placed in the String pool.

So, the String Object in nonpool memory should be available for GC after the method returns.
However, the literal "abc" will be managed by String pool memory management.
Trivikram Kamat
Ranch Hand

Joined: Sep 26, 2010
Posts: 155
I wrote a small code for testing String objects:


The line 5 prints true.
I guess the reason is both variables are referring to same literal in the String pool.

The line 9 prints false.
I guess the reason is str3 and str4 are two different objects in nonpool memory, both of which contain literal "Javaranch" which is in string pool.

So, objects str3 and str4 should be available for GC at Line 10
Please correct me if I'm wrong.
Thanks.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

That's correct Trivikram Kamat, but, let's remove the 3,4 and 5 th lines in your above code.

Then, there will be 3 objects totally. One is in the String Constant Pool and other two are in the heap. [String Constant Pool is a special heap, leave it for a while]
Then at the 10th line, the two objects referred by str3 and str4 are only eligible for GC. Not the String Constant Pool objects even though it doesn't have explicit references.
Trivikram Kamat
Ranch Hand

Joined: Sep 26, 2010
Posts: 155
Thanks Abimaran for your quick reply.
So, in the original question if we use String str = new String("abc");, the new String Object created on nonpool memory will be available for GC.

But, I still have one doubt.
Abimaran Kugathasan wrote:
Then, there will be 3 objects totally. One is in the String Constant Pool and other two are in the heap.


The one on the String constant pool ("Javaranch") is a literal, and not an object right?
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Trivikram Kamat wrote:
The one on the String constant pool ("Javaranch") is a literal, and not an object right?

I think, in Java, other than primitives, all are O(o)bjects. So, that's also an object!
Trivikram Kamat
Ranch Hand

Joined: Sep 26, 2010
Posts: 155
Abimaran Kugathasan wrote:
I think, in Java, other than primitives, all are O(o)bjects. So, that's also an object!

I'm not sure whether a literal can be called an object or not...

The definition of Literal from wikipedia says that:
A literal is a notation for representing a fixed value in source code.

In Oracle Java SE tutorials website, literals is explained under Primitive Data Types.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Trivikram Kamat wrote:
In Oracle Java SE tutorials website, literals is explained under Primitive Data Types.

That isn't about the String Constant pool literals. For example true and false are the literal of boolean data type. That's different! Have a look on this JLS

String Constant Pool Literals are String and hence Objects!
 
jQuery in Action, 2nd edition
 
subject: question with GC
 
Similar Threads
Application Server vs Web Server
Garbage Collection
Another garbage collection question
Doubt Garbage Collection
Eye witness for GC