• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collection

 
Brian Podolny
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer to the following question is B - no way to tell.
However, I think it should be A - none. Any thoughts?
<pre>
1. public void countDown()
2. for( int i = 10; i >= 0; i-- ){
3. String tmp = Integer.toString( i );
4. System.out.println( tmp );
5. }
6. System.gc();
7. System.out.println("Boom!");
8. }</pre>
When the program reaches line 7, how many of the String Objects
created in line 3 will have been garbage collected?
Assume that the System.out object is not keeping a reference.
A - none
B - there is no way to tell
C - 10
D - 11
I thought that objects created in a method are not eligible for
garbage collection ( under any circumstances ) until that method
has been exited.
Thanks

[This message has been edited by Brian Podolny (edited September 02, 2000).]
 
Tony Alicea
Desperado
Sheriff
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to read the question carefully:
"When the program reaches line 7, how many of the String Objects created in line 3 will have been garbage collected?"
Answer: No way to tell. Eligible for GC does not mean GC'd.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The key wording here is "will have been" - the call to System.gc() - requests that the JVM make an effort to run GC but that is not a guarantee.
When line 7 is reached, there will be no references to the first ten Strings at all so they are eligible for collection.
There may be a reference to the final String but that depends on the cleverness of the compiler - turns out to be a complex story.
Bill
 
Brian Podolny
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your response.
While I know it is impossible to tell when something actually has been Garbage Collected --- isn't it, in some cases, possible to rule out that something has been garbage collected.
For example:
1. Object o = new Object();
2. System.gc();
We can state with certainty that the Object referenced by o has not been garbage collected because there is still a reference to it. In your mock test question isn't it true that while there are no references to the objects created in the for loop that we can say for certain that the objects have not been garbage collected because with or without a reference they are not eligible for garbage collection until the method has been exited?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"In your mock test question isn't it true that while there are no references to the objects created in the for loop that we can say for certain that the objects have not been garbage ollected because with or without a reference they are not eligible for garbage collection until the method has been exited?"
That is absolutely NOT true. It doesn't matter which method the object was created in, when there are no longer references to the object, it is eligible for collection.
The thing that causes confusion is where in the example method a reference ceases to exist in the variable that has a scope only inside the loop. Fortunately you are not going to get a question about GC this weird on the real exam.
Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic