• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Objects eligible for GC

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How many objects are eligible for garbage collection once execution has reached the line labeled Line A?

String name;
String newName = "Nick";
newName = "Jason";
name = "Frieda";

String newestName = name;

name = null;
//Line A

a) 0
b) 1
c) 2
d) 3
e) 4

Can someone take a look at this. Thanks
 
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer should be zero as String literals are not eligible for garbage collection.
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pls take time going thru this..
http://www.javaranch.com/journal/200409/ScjpTipLine-StringsLiterally.html
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After going through the article, i would go for 0 Strings. since none are created using the 'new' operator, none are created at runtime and hence none are eligible for GC
 
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi to all,

how could be this possible
for example
if we write the stmt like this
String x="xyz";


then the compiler will automatically creates an object internally.
otherwise we could not able to use its methods

for ex we can srite this stmt

x.concat("abc");

so what i want to say is, there will be an object for a string literal
so that we can use its properties.

am i right?if not
can anyone plz justify this issue


cinux
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer should be choice b, 1.

First off since "name" is not explicitly initialized to null I will assume these are class level variables that are implicitly initialized ;-)

Now, a new object is created when newName is assigned "Nick" but it is immediately dereferenced since newName is assigned to another string object "Jason". The "Nick" object is eligible for GC since it is in a dereferenced state. name is assigned to new String object with value "Frieda" but the reference is transferred to newestName so it will not be dereferenced when name is assigned null.. so "Frieda" is not eligible for GC since it can still be used using the newestName reference ;-)

Kevin

Originally posted by HS Singh:
How many objects are eligible for garbage collection once execution has reached the line labeled Line A?

String name;
String newName = "Nick";
newName = "Jason";
name = "Frieda";

String newestName = name;

name = null;
//Line A

a) 0
b) 1
c) 2
d) 3
e) 4

Can someone take a look at this. Thanks

 
Ranch Hand
Posts: 313
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From everything I've read they don't get garbage collected, but the do a heck of a job optimizing the "pool".

I did my best to try and "fill up the pool" from simple string concatenation in an infinite loop to random character generation and assembly into Strings. I then tried to watch a performance monitor to see if the memory usage climbed. The CPU would spike but after a very "small" initial memory consumption (...may not even be related to the string pool")it leveled off and remained steady even though it was still "looping". So in the backgroud it is either cleaning up after itself or doing some form of optimization with substrings.
[ May 26, 2005: Message edited by: Byron Estes ]
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic