• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

gc ??

 
Muhammad Farooq
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found this question at MindQ
36. 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
any comments/answers???.
--Farooq
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13073
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My comment is that questions about GC should NEVER use String as an example due to the complication introduced by the String constant pool. In this code no new String objects have been created so there are none that are eligible. "Nick" etc are all constants created when the class loaded.
Bill
------------------
author of:
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Muhammad,
Bill is correct; string literals are not gc'd however many of the mocks use them in gc questions.
Here's an earlier thread on the same question that explains the question.

------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Muhammad Farooq
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bill and Jane,
I appreciate your help, I found this question on MindQ and the answer there was one object.
--Farooq
 
Muhammad Farooq
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How many objects are eligible for gc after the last line??
String a = new String ("Hello");
String b = new String ("World");
String c = a + b;
a = null;
b = c;
c = null;
b = c;

AAA
--Farooq
 
Ashik Uzzaman
Ranch Hand
Posts: 2373
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it should be 2 Farooq!
------------------
azaman
 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also think two objects are eligible for GC.
Pls. correct if wrong ..

How many objects are eligible for gc after the last line??
String a = new String ("Hello");
String b = new String ("World");
String c = a + b; // c contains "HelloWorld"
a = null; // as a has no more reference, eligible for GC
b = c; // b contains "HelloWorld"
c = null; // c has no reference, but b still contains "HelloWorld"
b = c; // b now contains null reference , so it is eligible
for GC

 
swati bannore
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what MindQ TBTs say about String literals

String literals, which are enclosed in double quotation marks, create an instance of a String class object whose contents cannot be changed.

is it correct? I know, there had been lot of dicussion on this topic..but still i seems to bring confusion with every new post..
Thanx
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic