• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Barry Boone's Question

 
Sathi Chowdhury
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1.public void soundOff(){
2.for (int i=1;i<10;i++) {
3.String tmp=Integer(i).toString()
4.System.out.println(tmp+ �,�);
5.}
6.System.out.println(�10�);
7.}
When the program reaches at line 6 how many of the String objects created at line 3 are elligible for garbage collection?
The given Answer is 8 with the logic that the last object of the 9 created still has a reference .
but I doubt it would be 9 ,becoz the variable temp which is holding the reference to the last created object is out of scope
outside the for loop.
Can anybody clear my confusion
thanks

[This message has been edited by Sathi Chowdhury (edited May 31, 2001).]
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The variable will go out of scope at the end of the for block, but variables are keep in a stack that is aligned to the method. The variable is not DESTROYED until after line 7. Until the variable is destroyed it still holds the reference.
 
Bin Wang
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for int (i=1;i<10;i++) doesn't compile, so this syntax is not legal, right?
 
Sathi Chowdhury
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry ,it was a typing mistake,I have changed the original message
 
Charlie Swanson
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the correct answer for this one?
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The same.
 
Daniel Dunleavy
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cindy,
I thought that java knows where the last reference to an object is in the code, and it is eligible for GC the line after the last reference in the code.
Your saying (?) that it keeps all the latest references as in
a = "aaa"
b = "bbb"
b = a --- at this point str obj "bbb" for GC
a = "xxx"
--- a and b still kept until end of method
Is that correct?
Thanks
Dan
 
Scott Appleton
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel, your example is technically incorrect because you're assigning literals to the String variables a and b, and String literals in the pool are never gc. However, if we assume you meant assignments like a = new String("aaa") then I believe that you are correct in that the 2 Strings whose values are "aaa" and "xxx" would not be gc until the enclosing method is complete.
 
Shiraz Bhagwagar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't you think that it is not possible to determine how many objects will be garbage collected. If I am wrong, please could someone explain.
 
Sathi Chowdhury
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Cindy Glass:
The variable will go out of scope at the end of the for block, but variables are keep in a stack that is aligned to the method. The variable is not DESTROYED until after line 7. Until the variable is destroyed it still holds the reference.

Dear Cindy ,
I still have a strong doubt.The Exam Cram Book (page 55) says
[B]"Normally,all objects created for local variables in a method become eligible for garbage collection when the method exits.Exceptions would be if an object was added to a collection somewhere or returned to the calling method."[B]
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel,
NOWHERE does it say that "a variable is eligible for GC the line after the last reference in the code." That is a GREAT misstatement.
# 1- Variables are NEVER ready for the gc. ONLY objects, which are created on the heap, are garbage collected. Variables, which are created on the stack, are destroyed by the operating system/JVM.
#2 Objects are not ready for the gc after the last reference to them (as in last time they are mentioned). NO NO NO. Objects are ready for the gc after there are no more references TO them held in variables. This is a GREATLY different concept.
#3 The life of a variable depends on where is was created. IF it was created in a method, then it dies when the method dies. If it is an instance variable then it lives as long as the object lives.
Shiraz,
It is ALWAYS possible to know what objects are ready for the gc. You just have to know the rules.
Sathi,
Yes, the exam cram says "all objects created for local variables in a method become eligible for garbage collection when the method exits EXCEPT . . . . ."
That means that if the objects created in the method are only referenced by variables in that method, then when the method dies, those objects can be gc'd EXCEPT if you have passed a reference to the object to something outside the method. In that case (even though the OBJECT - not the variable- was created in the method) the object will continue to exist on the heap until the outside reference is gone.
Please do not confuse the life of the variable with the life of the object. They are two COMPLETELY different things.
[This message has been edited by Cindy Glass (edited June 01, 2001).]
 
Daniel Dunleavy
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cindy,
Thanks for the info.
Since I have been here, I have read many of your responses, and they are very detailed and informative.
Thanks so much for your dedication !!!
Also you might consider a book "Cindy's Collected Java Posts"
Dan
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

All I need now is a Literary Agent . You available?
 
Scott Appleton
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very well summarized, Cindy. I think you could paste that post into about half the gc threads and answer whatever the original question was.
P.S., if you find a literary agent, let me know...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic