How does the JVM know when an object is eligible for garbage collection? The JVM waits patiently and monitors each object until it determines that the code no longer needs that memory. An object will remain on the heap until it is no longer reachable. An object is no longer reachable when one of two situations occurs:
Objects vs. References
Do not confuse a reference with the object that it refers to; they are two different entities. The reference is a variable that has a name and can be used to access the contents of an object. A reference can be assigned to another reference, passed to a method, or returned from a method. All references are the same size, no matter what their type is.
An object sits on the heap and does not have a name. Therefore, you have no way to access an object except through a reference. Objects come in all different shapes and sizes and consume varying amounts of memory. An object cannot be assigned to another object, and an object cannot be passed to a method or returned from a method. It is the object that gets garbage collected, not its reference.
Realizing the difference between a reference and an object goes a long way toward under-standing garbage collection, the new operator, and many other facets of the Java language. Look at this code and see whether you can figure out when each object first becomes eligible for garbage collection:
When you are asked a question about garbage collection on the exam, we recommend that you draw what’s going on. There’s a lot to keep track of in your head, and it’s easy to make a silly mistake trying to hold it all in your memory. Let’s try it together now. Really. Get a pencil and paper. We’ll wait.
Got that paper? Okay, let’s get started. On line 3, write one and two (just the words—no need for boxes or arrows since no objects have gone on the heap yet). On line 4, we have our first object. Draw a box with the string “a” in it, and draw an arrow from the word one to that box. Line 5 is similar. Draw another box with the string “b” in it this time and an arrow from the word two. At this point, your work should look like Figure 1.4.
FIGURE 1. 4 Your drawing after line 5
On line 6, the variable one changes to point to “b”. Either erase or cross out the arrow from one and draw a new arrow from one to “b”. On line 7, we have a new variable, so write the word three and draw an arrow from three to “b”. Notice that three points to what one is pointing to right now and not what it was pointing to at the beginning. This is why you are drawing pictures. It’s easy to forget something like that. At this point, your work should look like Figure 1.5.
Finally, cross out the line between one and “b” since line 8 sets this variable to null. Now, we were trying to find out when the objects were first eligible for garbage collection. On line 6, we got rid of the only arrow pointing to “a”, making that object eligible for gar-bage collection. “b” has arrows pointing to it until it goes out of scope. This means “b” doesn’t go out of scope until the end of the method on line 9.
FIGURE 1. 5 Your drawing after line 7
Code Formatting on the Exam
Not all questions will include package declarations and imports. Don’t worry about missing package statements or imports unless you are asked about them. The following are common cases where you don’t need to check the imports:
You’ll see code that doesn’t have a method. When this happens, assume any necessary plumbing code like the main() method and class definition were written correctly. You’re just being asked if the part of the code you’re shown compiles when dropped into valid sur-rounding code. Finally, remember that extra whitespace doesn’t matter in Java syntax. The exam may use varying amounts of whitespace to trick you.