• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question from JQ+

 
Padma Shyam
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. public void process(int count)
{
2. for ( int i = 1; i < count; i++ )
{
3. Object temp = " Hello "+i;
}
4.
}
The question is that:
How many objects are eligible for garbage collection when the control reaches line 4 if the method process() is called with the argument of 5?
The answer given is 0.Can anybody explain this?
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe the answer lies in the way String literals are handled in Java. I may need a little help on this one, but I'll try to explain it as best I can.
Look at the following code:

In line 1, a String literal is created. These literals are not, generally, garbage collected. If another String reference is created and initialized with the same literal (as in line 3), both variables will reference the exact same String literal. This optimization can be done because Strings are iummutable in Java. Even if r and t and both assigned null (as in line 4), if a new String is created with the same String literal (line 5), it will reference the same String object that was created initially for r. These String literals don't normally go away. If memory is getting tight, I think they will be garbage collected, but they don't normally become eligible.
However, if you create a String like s, as in line 2, it behaves like any other object. None of the other Strings (r or t) reference the same String object that s references. And, like other objects, when no references to it exist (as is the case after line 4), it becomes eligible for garbage collection.
So, since String literals are being used to create Strings, rather than the keyword new, no objects are eligible for garbage collection after that loop executes.
If you'd like to see some more evidence of this behavior, try the following code:

I hope that helps,
Corey
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't agree with that - new Strings are being created because of the expression.
"Hello " + i
Exactly how many are GC-able depends on how the JVM treats the temp reference after the scope of the loop has been left but before the method returns. See this for a discussion of a similar question.
http://www.lanw.com/java/LocalVariables.htm
This just goes to show why using String objects for discussion of GC is a bad idea.
Bill
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The number of objects eligible for gc after the loop ends will be the number created during the loop execution.
In this case, the character sequence " Hello " is a string literal, and will not be garbage collected.
However, the expression " Hello " +i has to be evaluated at runtime. The string concatenation operator causes an implicit call to a StringBuffer.append() to actually create the new concatenated string.
The new string is definately NOT a string literal. It's just a regular string as if you had written new String(" Hello " + i);
This string, being NOT a string literal, is eligible for garbage collection when there are no more references to it. This occurs in the example as soon as the for-loop block exits.
[ February 18, 2002: Message edited by: Rob Ross ]
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3764
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Bill and Rob. This question should be removed.
-Paul.
 
R K Singh
Ranch Hand
Posts: 5384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
check this
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic