Glossary of terms
- phantom reference
- A reference to a referent from inside a
PhantomReferenceobject. The garbage collector does not clear phantom references. The program must do so, by callingReference'sclear()method. - reference object
- A
SoftReference,WeakReference, orPhantomReferenceobject -- or any object created from any new subclass of theReferenceclass. - reference queue
- A "first-in-first-out" data structure that holds references to
Referencesubclass 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, orPhantomReferenceobject, respectively. - soft reference
- A reference to a referent from inside a
SoftReferenceobject. 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
WeakReferenceobject. 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.javabelow 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.OutOfMemoryErrorobject. (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,
OMattempts to create a chain of 10,000MemBlockobjects. Each such object has amemoryfield containing 100,000 bytes. Apart from overhead, those figures indicate thatOMrequires 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 both10000and100000in theOMsource 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.


![JavaWorld > Android Studio for Beginners [movile java package]](https://images.idgesg.net/images/article/2019/02/jw_android_studio_for_beginners_3x2_1200x800_pkg_idg_google_darkovujic_gettyimages-100788152-small.3x2.jpg)
![JavaWorld > Persistence [series] > data blocks / data center / database / server traffic routing](https://images.idgesg.net/images/article/2019/04/jw_java_persistence_series_3x2_2400x1600_3_data_center_database_digital_information_blocks_server_traffic_routing_by_ramcreativ_gettyimages-100792562-small.3x2.jpg)