Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

garbage collection  RSS feed

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question from MindQ
36. How many objects are eligible for garbage collection once execution has reached 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
I thought that the answer should be c)2 , but the answer given is b
newname = "Nick"; becomes eligible for garbage collection once the statement newname = "Jason"; is executed and name = null;
is the second object which is eligible
So how come the answer is b) 1
Thanks,
Srini
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd think it's a mistake because I also get two objects eligible for GC, "Nick" and "Jason". newName is referencing "Frieda".
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would think the answer (b) given is right.. this is why
You must know two most important points about GC .
(1)Objects created on String pool are not gc'ed even though you assign null to them.
(2) Objects created on heap are the only ones available for gc

Keeping this in mind let us analyze the code

Hence only one object is eligible for gc. That is the one created on heap.
Hope it clears it!!
-Sandeep Nachane

------------------
Visit my homepage at
www.tipsmart.com
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually here is why:
String name;
String newname = "Nick";
newname = "Jason";
/* The object "Nick" can no longerbe referenced.(No variable pointing to "Nick" string as newname now points to "Jason")
Therefore at this point one String object eligible for GC-- "Nick". */
name = "Frieda";
String newestName = name;
/* After the above statement, two handles point to the same object. Both newestName and name variables point to the String "Freida". */
name = null;
/* In the above stmt, the name variable no longer points to the STring "Freida". But the newestName continues to point to the String " Freida" */
// Therefore, only one object is eligible for garbage collection.
//and that is " Nick".
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree b is the right answer. Here is some code to test it out.

C:\JavaRanch>java GCTest
they point to the same object
newname = Jason
newestName = Frieda
name = null
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Sandeep,
can pls be more clear what do you mean by string pool and heap
Yogesh
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!