This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Barry Boone's Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Barry Boone Watch "Barry Boone New topic
Author

Barry Boone's Question

Sathi Chowdhury
Ranch Hand

Joined: Mar 16, 2001
Posts: 52
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

Joined: Sep 29, 2000
Posts: 8521
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.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Bin Wang
Ranch Hand

Joined: May 01, 2001
Posts: 82
for int (i=1;i<10;i++) doesn't compile, so this syntax is not legal, right?
Sathi Chowdhury
Ranch Hand

Joined: Mar 16, 2001
Posts: 52
sorry ,it was a typing mistake,I have changed the original message
Charlie Swanson
Ranch Hand

Joined: Jan 29, 2001
Posts: 213
What is the correct answer for this one?
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
The same.
Daniel Dunleavy
Ranch Hand

Joined: Mar 13, 2001
Posts: 276
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

Joined: May 07, 2001
Posts: 195
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

Joined: Mar 28, 2001
Posts: 27
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

Joined: Mar 16, 2001
Posts: 52
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

Joined: Sep 29, 2000
Posts: 8521
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

Joined: Mar 13, 2001
Posts: 276
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

Joined: Sep 29, 2000
Posts: 8521

All I need now is a Literary Agent . You available?
Scott Appleton
Ranch Hand

Joined: May 07, 2001
Posts: 195
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...
 
Consider Paul's rocket mass heater.
 
subject: Barry Boone's Question
 
Similar Threads
Local variable and Garbage Collection
GC
Garbage Collection
How many objects are Garbage collected.Explain
Garbage collection question from J@Whiz1.4