[ Pobierz całość w formacie PDF ]

collector when it senses that memory is running low. Experience indicates that when
your Java program makes a request for garbage collection, the JVM will usually grant
your request in short order, but there are no guarantees. Just when you think you can
count on it, the JVM will decide to ignore your request.
How Does the Garbage Collector Work?
You just can t be sure. You might hear that the garbage collector uses a mark and
sweep algorithm, and for any given Java implementation that might be true, but the
Java specification doesn t guarantee any particular implementation. You might hear
that the garbage collector uses reference counting; once again maybe yes maybe no.
The important concept to understand for the exam is when does an object become
eligible for garbage collection. To answer this question fully we have to jump ahead a
little bit and talk about threads. (See Chapter 9 for the real scoop on threads.) In a
nutshell, every Java program has from one to many threads. Each thread has its own
little execution stack. Normally, you (the programmer), cause at least one thread to
run in a Java program, the one with themain()method at the bottom of the stack.
However, as you ll learn in excruciating detail in Chapter 9, there are many really
cool reasons to launch additional threads from your initial thread. In addition to
having its own little execution stack, each thread has its own lifecycle. For now, all
we need to know is that threads can be alive or dead. With this background
information we can now say with stunning clarity and resolve that, an object is
eligible for garbage collection when no live thread can access it.
Based on that definition, the garbage collector does some magical, unknown
operations, and when it discovers an object that can t be reached by any live thread
it will consider that object as eligible for deletion, and it might even delete it at some
point. (You guessed it, it also might not ever delete it.) When we talk about reaching
an object, we re really talking about having a reachable reference variable that refers
to the object in question. If our Java program has a reference variable that refers to
an object, and that reference variable is available to a live thread, then that object is
considered reachable. We ll talk more about how objects can become unreachable in
the following section.
Color profile: Generic CMYK printer profile
CertPrs8(SUN) / Sun Certified Programmer & Developer for Java 2 Study Guide / Sierra / 222684-6 / Chapter 7
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)
29
Can a Java application run out of memory? Yes. The garbage collection system
attempts to remove objects from memory when they are not used. However,
if you maintain too many live objects (objects referenced from other live objects),
the system can run out of memory. Garbage collection cannot ensure that
there is enough memory, only that the memory that is available will be managed
as efficiently as possible.
Writing Code That Explicitly
Makes Objects Eligible for Collection
In the previous section, we learned the theories behind Java garbage collection. In
this section, we show how to make objects eligible for garbage collection using actual
code. We also discuss how to attempt to force garbage collection if it is necessary,
and how you can perform additional cleanup on objects before they are removed
from memory.
Nulling a Reference
As we discussed earlier, an object becomes eligible for garbage collection when there
are no more reachable references to it. Obviously, if there are no reachable references,
it doesn t matter what happens to the object. For our purposes it is just floating in
space, unused, inaccessible, and no longer needed.
The first way to remove a reference to an object is to set the reference variable
that refers to the object tonull. Examine the following code:
1. public class GarbageTruck {
2. public static void main(String [] args) {
3. StringBuffer sb = new StringBuffer("hello");
4. System.out.println(sb);
5. // The StringBuffer object is not eligible for collection
6. sb = null;
7. // Now the StringBuffer object is eligible for collection
8. }
9. }
The StringBuffer object with the valuehellois assigned the reference variable
sb in the third line. Although the rest of the code does not use the StringBuffer
object, it is not yet eligible for garbage collection. To make it eligible, we set the
reference variable sb tonull, which removes the single reference that existed to [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • szkla.opx.pl
  •