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 callingReference
‘sclear()
method. - reference object
- A
SoftReference
,WeakReference
, orPhantomReference
object — or any object created from any new subclass of theReference
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 callingReference
‘senqueue()
method. - referent
- An object that is softly, weakly, or phantomly referenced from inside a
SoftReference
,WeakReference
, orPhantomReference
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.
Homework
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:
- Why is it beneficial that Java, rather than the developer, frees objects?
-
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).
- Write a program that demonstrates how you can run out of memory in Java. What happens when you run out of memory?
-
OM.java
below presents source code to anOM
(out of memory) application that demonstrates running out of memory. At some point, the JVM terminates the program and throws ajava.lang.OutOfMemoryError
object. (That object is a special exception. You will learn about exceptions in a future article.)OM.java
// OM.java // 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]; temp.next = first; first = temp; } } }
To run out of memory,
OM
attempts to create a chain of 10,000MemBlock
objects. Each such object has amemory
field containing 100,000 bytes. Apart from overhead, those figures indicate thatOM
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 both10000
and100000
in theOM
source code. - What is a disadvantage to a generational garbage collector? (Hint: Why does the Train algorithm exist?)
-
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.