• 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

String pool objects and garbage collections

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi fellow ranchers

In the Enthuware SCJP test one can find the following code:

with the question: "How many objects will be eligible for GC just after the method returns?"

The answer is 1, since "abc" is in the string pool and therefore not eligible for GC, however I'm not sure whether this is true, because I would think that the string pool manager can choose to GC an object from the string pool?

Any thoughts you may have on this will be appreciated.

- Morten
 
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I feel its 2.
Because, String "abc" is an object.
This object is created locally and hence eligible for garbage collection.
So, totally there are 2 objects for garbage collection.
What I dont understand is...Aren't the string objects in string pool good enough for garbage colection, if the relevant string objects are not in the scope?
Correct me if I am wrong.

Regards,
Sriram.
 
Morten Monrad Pedersen
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sriram

Thank you for your answer.

Originally posted by Sriram Sharma:
I feel its 2.
Because, String "abc" is an object.
This object is created locally and hence eligible for garbage collection.
So, totally there are 2 objects for garbage collection.
What I dont understand is...Aren't the string objects in string pool good enough for garbage colection, if the relevant string objects are not in the scope?
Correct me if I am wrong.



My answer to the question was also "2", because I thought that strings in the string pool could be garbage collected, but the makers of the Enthuware test seems to think differently.
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why should it be 2. I guess it will be just 1. The 'str' that is created is just a reference of String type created in the stack. The actual object is created in the heap, the String pool in this case. So, as such str is not eligible for GC.

Link1
Link2

regards,
vijay.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just for all of you out there keeping score, remember that although some mock exams might mix the concept of the String pool in with the concept of garbage collection, the real 1.4 and 5.0 exams WILL NOT mix those two together in a question. If the real exam wants to ask about how many objects might be eligible for the GC it will use objects of a type other than String objects.

hth,

Bert
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
As i read an article i understood that strings from the string literal pool cannot be garbage collected. So what happens to these objects finally? Dont you think its a heavy memory consumption if they live forever? Or is there any other mechanism that happens to prevent this?

Regards,
Chaitra
 
Morten Monrad Pedersen
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vijay

Thanks for the answer and the links.

Originally posted by Vijay Raj:
Why should it be 2.



As I wrote above, I thought that the string pool manager could choose to GC objects in the pool. However the links you provided have shown that this isn't so.
 
Morten Monrad Pedersen
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bert

Thank you for your informative post.

Originally posted by Bert Bates:
Just for all of you out there keeping score, remember that although some mock exams might mix the concept of the String pool in with the concept of garbage collection, the real 1.4 and 5.0 exams WILL NOT mix those two together in a question. If the real exam wants to ask about how many objects might be eligible for the GC it will use objects of a type other than String objects.



That's really nice to know, thanks .
 
Morten Monrad Pedersen
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chaitra

Originally posted by chaitra mathur:
As i read an article i understood that strings from the string literal pool cannot be garbage collected.



It might be this article by Corey McGlone you have read. It states that objects in the string pool can't be garbage collected.
 
And when my army is complete, I will rule the world! But, for now, I'm going to be happy with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic