• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

question with GC

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
ved dixit
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It will be available for gc at the end of method on line 14.
 
ved dixit
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankss...i got it !!
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey ,but i studied that String is also an object
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
correct..Abimaran..
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
on the real exam String objects are never used in GC questions
 
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think yes.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just read on ScjpFAQ about eligibility of String Literals for GC
It doesn't mention about String objects though.
 
Trivikram Kamat
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic