• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Bear Bibeault
  • Liutauras Vilda
  • Devaka Cooray
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Garbage Collection

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How many objects are elegible for GC once execution has rechead the line labeled LINE A?

String name;
String newName = "Nick";
newName = "Jason";
name= "Fried";
String newestName = name;
name = null;
//LINE A

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

Answer b

This is right? why?
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


When you get to LINE A:
newName refers to "Jason"
newestName refers to "Fried"
nothing refers to "Nick", so its eligible for garbage collection.

By the way, since "Nick" is in the String Literal Pool, which isn't garbage collected, the answer may be "a".
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Three String objects have been created here.
When line A is reached, the variable newName refers to String object "Jason", newestName refers to "Fried" and name refers to null. The object "Nick" has no variable referencing it. Hence only this object is eligible for GC.
 
Seema Manivannan
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops! I think we both replied around the same time :-)
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike said: "By the way, since "Nick" is in the String Literal Pool, which isn't garbage collected, the answer may be "a"."

I'll bet my money on a for that reason.
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arent all string literals cannot be garbage collected since there will be a reference to the string literal pool.
 
Celso Serrano Araujo
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is A or B right? the literal string could to be gabage collected?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String literals are not garbage collected.
 
Fran Kindred
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with the answer being A
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read from Ivor Horton that Since the string is in storage pool, this cannot be garbage collected and hence the answer must be 'a';
 
New rule: no elephants at the chess tournament. Tiny ads are still okay.
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!