Study guide: Trash talk, Part 2

Brush up on Java terms, review homework assignments, and read Jeff's answers to student questions

Glossary of terms

phantom reference
A reference to a referent from inside a PhantomReference object. The garbage collector does not clear phantom references. The program must do so, by calling Reference's clear() method.
reference object
A SoftReference, WeakReference, or PhantomReference object -- or any object created from any new subclass of the Reference class.
reference queue
A "first-in-first-out" data structure that holds references to Reference subclass objects. The garbage collector places those references on the queue by calling Reference's enqueue() method.
An object that is softly, weakly, or phantomly referenced from inside a SoftReference, WeakReference, or PhantomReference object, respectively.
soft reference
A reference to a referent from inside a SoftReference object. The garbage collector has the option of clearing soft references when heap memory is low.
weak reference
A reference to a referent from inside a WeakReference object. The garbage collector always clears weak references.


Rewrite WeakReferenceDemo to use the WeakHashMap class. How does the resulting program differ from what appears in the article?

Answers to last month's homework

Last month, I presented three questions. Here are those questions and their answers:

  1. Why is it beneficial that Java, rather than the developer, frees objects?
  2. Java, not the developer, should free objects because the developer might either forget to free objects (which can cause a memory leak) or attempt to free objects that have already been freed (which might crash the program).

  3. Write a program that demonstrates how you can run out of memory in Java. What happens when you run out of memory?
  4. below presents source code to an OM (out of memory) application that demonstrates running out of memory. At some point, the JVM terminates the program and throws a java.lang.OutOfMemoryError object. (That object is a special exception. You will learn about exceptions in a future article.)

    // A demonstration of a program running out of memory.
    class MemBlock
       char [] memory;
       MemBlock next;
    class OM
       public static void main (String [] args)
          MemBlock first = null;
          for (int i = 0; i < 10000; i++)
               MemBlock temp = new MemBlock ();
               temp.memory = new char [100000];
      = first;
               first = temp;

    To run out of memory, OM attempts to create a chain of 10,000 MemBlock objects. Each such object has a memory field containing 100,000 bytes. Apart from overhead, those figures indicate that OM requires 10,000 objects x 100,000 bytes x 2 bytes (two bytes per character) -- for each object -- or 2 billion bytes of heap memory. Most likely, your computer doesn't have that much heap memory to play with. If it does, try increasing either or both 10000 and 100000 in the OM source code.

  5. What is a disadvantage to a generational garbage collector? (Hint: Why does the Train algorithm exist?)
  6. When garbage collectors, including most generational garbage collectors, run, a program must stop execution until the garbage collector completes. That leads to unpredictable delays in a program's execution. The Train algorithm serves as an extension to a generational garbage collector and can remove those delays from the garbage collection process.