• 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 objects and garbage collection

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, there's a practice paper : http://www.javaranch.com/maha/_Mock_Exams/MindQ_s_Sun_Certified_Java_Programmer_Practice_Test.htm

with the question:

36. How many objects are eligible for garbage collection once execution has reached the line labeled Line A?
where the answer is a multiple choice of 0,1,2,3 or 4

I answered 0 while the answer sheet is saying 1, i'm not sure I understand the reason.

This was my logic:

String name; //1 variable, 0 objects
String newName = "Nick"; //2 variables, 1 object(value is obtained from string pool)
newName = "Jason"; //same object now references the "Jason" literal
name = "Frieda"; //2 variables, 2 objects

String newestName = name; //3 variables, 2 objects

name = null; //3 variables(one of which is null), still 2 objects.

So I ended up with 0 objects eligible for garbage collection, where am I going wrong with this please?
[ September 05, 2006: Message edited by: Allan Morris ]
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Allan,

newName refers to "Nick" but in the next line, it refers to "Jason". "Nick" is now eligible for garbage collection.
 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Allan,

You are quite correct !!!

With no fear to miss this question on the exam. There are NO objects eligible for GC.

Any hardcoded strings created in a Java application are stored on String pool.

For those objects be eligible for gc they must be created with new.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I don't know exactly but, I guess when the object in the pool is not been pointed by any object it should be eligilble for the Garbage collection right. If not at one time or the other, the pool will bulge and the porcessing time will grow. am I right?

-ram.
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just found this in this very site.
 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Garbage collection always takes place on the heap. Since none of the String instances above are on the heap, the no. of objects garbage collected is 0.
[ September 06, 2006: Message edited by: Aniket Patil ]
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SCJP page 413-415 reads:

String s = "abcdef";

is a shortcut for

String s = new String("abcdef");

Then they go on to show that the 1st example creates "abcdef" on the heap. figure 6.1

So it seems to me, by the K&B definition, that the answer to the original question is: 1 object is eligible for garbage collection.
 
Dave Reinhardt
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just re-read the link that Fred posted.

It appears that objects in the string pool are eligible for garbage collection, but not until the program is unloaded. So, for the exam, it seems to me it makes sense to think of all strings as objects that are eligible for GC if nothing references them. The specifics of the pool are not important for the exam.
 
reply
    Bookmark Topic Watch Topic
  • New Topic