Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
PhantomReference object. The garbage collector does not clear phantom references. The program must do so, by calling Reference's clear() method.
SoftReference, WeakReference, or PhantomReference object -- or any object created from any new subclass of the Reference class.
Reference subclass objects. The garbage collector places those references on the queue by calling Reference's enqueue() method.
SoftReference, WeakReference, or PhantomReference object, respectively.
SoftReference object. The garbage collector has the option of clearing soft references when heap memory is low.
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?
Last month, I presented three questions. Here are those questions and their answers:
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).
OM.java 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.)
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,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.
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.
Read more about Core Java in JavaWorld's Core Java section.