[ Pobierz całość w formacie PDF ]
object is doomed, eligible for garbage collection.
Color profile: Generic CMYK printer profile
CertPrs8(SUN) / Sun Certified Programmer & Developer for Java 2 Study Guide / Sierra / 222684-6 / Chapter 7
Composite Default screen
Chapter 7: Objects and Collections
30
Reassigning a Reference Variable
We can also decouple a reference variable from an object by setting the reference
variable to refer to another object. Examine the following code:
class GarbageTruck {
public static void main(String [] args) {
StringBuffer s1 = new StringBuffer("hello");
StringBuffer s2 = new StringBuffer("goodbye");
System.out.println(s1);
// At this point the StringBuffer "hello" is not eligible
s1 = s2; // Redirects s1 to refer to the "goodbye" object
// Now the StringBuffer "hello" is eligible for collection
}
}
Objects that are created in a method also need to be considered. When a method
is invoked, any local variables created exist only for the duration of the method. Once
the method has returned, the objects created in the method are eligible for garbage
collection. There is an obvious exception, however. If an object is returned from the
method, its reference might be assigned to a reference variable in the method that
called it; hence, it will not be eligible for collection. Examine the following code:
import java.util.Date;
public class GarbageFactory {
public static void main(String [] args) {
Date d = getDate()
doComplicatedStuff();
System.out.println("d = " + d);
}
public static Date getDate() {
Date d2 = new Date();
String now = d2.toString();
System.out.println(now);
return d2;
}
}
In the preceding example, we created a method calledgetDate()that returns
a Date object. This method creates two objects: a Date and a String containing the
date information. Since the method returns the Date object, it will not be eligible for
collection even after the method has completed. The String object, though, will be
eligible, even though we did not explicitly set the now variable tonull.
Color profile: Generic CMYK printer profile
CertPrs8(SUN) / Sun Certified Programmer & Developer for Java 2 Study Guide / Sierra / 222684-6 / Chapter 7
Composite Default screen
Garbage Collection (Exam Objectives 3.1, 3.2, 3.3)
31
Isolating a Reference
There is another way in which objects can become eligible for garbage collection,
even if they still have valid references! We think of this scenario as islands of isolation.
A simple example is a class that has an instance variable that is a reference variable
to another instance of the same class. Now imagine that two such instances exist and
that they refer to each other. If all other references to these two objects are removed,
then even though each object still has a valid reference, there will be no way for any
live thread to access either object. When the garbage collector runs, it will discover
any such islands of objects and will remove them. As you can imagine, such islands
can become quite large, theoretically containing hundreds of objects. Examine the
following code:
public class Island {
Island i;
public static void main(String [] args) {
Island i2 = new Island();
Island i3 = new Island();
Island i4 = new Island();
i2.i = i3; // i2 refers to i3
i3.i = i4; // i3 refers to i4
i4.i = i2; // i4 refers to i2
i2 = null;
i3 = null;
i4 = null;
// do complicated, memory intensive stuff
}
}
When the code reaches// do complicated,the three Island objects
(previously known as i2, i3, and i4) have instance variables so that they refer to
each other, but their links to the outside world (i2, i3, and i4) have been nulled.
These three objects are eligible for garbage collection.
This covers everything you will need to know about making objects eligible for
garbage collection. Study Figure 7-5 to reinforce the concepts of objects without
references and islands of isolation.
Color profile: Generic CMYK printer profile
CertPrs8(SUN) / Sun Certified Programmer & Developer for Java 2 Study Guide / Sierra / 222684-6 / Chapter 7
Composite Default screen
Chapter 7: Objects and Collections
32
FIGURE 7-5 Objects eligible for garbage collection
Forcing Garbage Collection
The first thing that should be mentioned here is, contrary to this section s title,
garbage collection cannot be forced. However, Java provides some methods that allow
you to request that the JVM perform garbage collection. For example, if you are about
to perform some time-sensitive operations, you probably want to minimize the chances
of a delay caused by garbage collection. But you must remember that the methods
that Java provides are requests, and not demands; the virtual machine will do its best
[ Pobierz całość w formacie PDF ]