This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
A am reading a question on when an object is available for garbage collection and it has two answers that are very similar to me. Can someone explain the difference? A. When an object becomes unreachable by any code. B. When an object goes out of scope. Thanks... Erik
The first one likely refers to the case where an object no longer is referenced by your program. This is the simplest example of an object which is no longer referenced:
obj use to point to an object, but after setting it to null that object is not referenced by any other object or a variable by your program so it is "eligible" for garabage collection. The other case just refers to objects created within methods (maybe someone can offer up other examples)
Once the method terminates so goes all the references to the object we created, and thus it is "eligible" for garabage collection.
Joined: Jun 13, 2003
What if in the second example your method returned a n Object o to the caller? I assume that would make o "still in scope" and "not available for gc".
�reachable� is a Very important word in the context of garbage collection. It is worth getting the best explanation you can find. 1A. The Java virtual machine uses garbage collection to ensure that any referenced object will remain in memory, and to free up memory by deallocating objects that are no longer reachable from references in executing code. This is a strong guarantee--an object will not be collected if it can be reached by following a chain of references starting with a *root* reference, that is, a reference that is directly accessible from executing code. (That�s a lot of words. They are all important. Read this several times.) 1B. An object is �no longer reachable� when no reference to the object exists in any variable of any currently executing method, nor can you find a reference to the object starting from such variables and then following each field or array element, and so on. (Same author repeating himself in the next paragraph. The only thing missing is you can also start from references in static variables.) 2A. An object enters an *unreachable* state when no more strong references to it exist. When an object is unreachable, it is a *candidate* for collection. 2B. It is important to note that not just any strong reference will hold an object in memory. These must be references that chain from a garbage collection root. GC roots are a special class of variable that includes temporary variables on the stack (of any thread), static variables (from any class) and special references from JNI native code. (Try to imagine the garbage collection roots and the chain of references.) My references are The Java Programming Language 12.1 and The Truth About Garbage Collection The Truth [ June 17, 2003: Message edited by: Marlene Miller ]
Joined: Mar 05, 2003
The scope of a local variable declaration in a block is the rest of the block in which the declaration appears. The scope of a local variable declared in a for statement is the rest of the for statement.
When the code is outside of the scope of the variable declaration, the code cannot access the variable. But the variable on the thread stack might still hold a reference to the object. [ June 17, 2003: Message edited by: Marlene Miller ]
In my opinion, it is an issue with wording of question. My understanding is that objects are created on the heap in Java. They by themselves do NOT come into scope or go out of scope. You access these objects in your program via 'handles' more commonly know as references. These references come into or go out of scope (existance). More over they have defined visibility. For any given object, there could be none, one or many references at any point IN TIME (not any line in code because of multithreading). If none of the reference are in scope (alive) for an object at a given point in time, that object is eligible for collection. Based on this understanding, I believe wording is poor. A. When an object becomes unreachable by any code. B. When an object goes out of scope.