Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

A Java OutofMemoryError occurs if the program attempts to use more heap space than what is available. Amongst Among other causes, this error may result from

...

Code Block
bgColor#FFcccc
/** Assuming the heap size as 512 MB (calculated as 1/4th of 2 GB RAM = 512 MB)
 *  Considering long values being entered (64 bits each, the max number of elements
 *  would be 512 MB/64bits = 67108864)
 */
public class ShowHeapError {
   Vector<Long> names = new Vector<Long>(); // Accepts unknown number of records
   long newID = 0L;
   int count = 67108865;
   int i = 0;
   InputStreamReader input = new InputStreamReader(System.in);
   Scanner reader = new Scanner(input);

   public void addNames(){
     do{
       // Adding unknown number of records to a list
       // The user can enter more number of IDs than what the heap can support and thus 
       // exhaust the heap. Assume that the record ID is a 64 bit long value
      
       System.out.print("Enter recordID (To quit, enter -1): ");
       newID = reader.nextLong();
       
       names.addElement(newID);
       i++;
     }while (i < count || newID != -1);
     // Close "reader" and "input"
   }

   public static void main(String[] args) {
     ShowHeapError demo = new ShowHeapError();
     demo.addNames();
   }
}

...

Wiki Markup
The {{OutOfMemoryError}} can be avoided by ensuring that therethe areabsence noof infinite loops or, memory leaks, and no unnecessary object retention. IfWhen memory requirements are known ahead of time, the heap size can be tailored to fit the requirements using the following runtime parameters \[[Java 2006|AA. Bibliography#Java 06]\]:

...

ObjectOutputStream and ObjectInputStream can provide an application with persistent storage for graphs of objects when used with a FileOutputStream and FileInputStream respectively. ObjectInputStream is used to recover the objects previously serialized. Other uses include passing objects between hosts using a socket stream or for marshaling and unmarshaling arguments and parameters in a remote communication system.

By design, only the first time an object is written, does it get reflected in the stream. Subsequent writes write a handle to the object into the stream. A table mapping the objects written to the stream to the corresponding handle is also maintained. Because of this handle, references that may not persist during normal runs of the program are also retainedobjects are written to the stream only on their first appearance therein. For subsequent appearances, the stream writes a handle to the earlier appearance of the object. The ObjectOutputStream maintains a table that maps previously-written objects to their corresponding handles. This table remains live until the stream is closed; consequently, it prevents garbage collection of previously-written objects. This can cause an OutOfMemoryError when streams remain open for extended durations.

Code Block
bgColor#FFcccc
FileOutputStream fos = new FileOutputStream("data.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(new Date());
// ... 

Compliant Solution

If heap related issues arise, it is recommended that Make judicious use of the ObjectOutputStream.reset() method be called so that to clear references to previously written objects may and consequently permit them to be garbage collected. Note that this will cause subsequent writes of a previously-written object to be treated as though they were the initial write, leading to multiple copies of the object in the serialized data stream. This issue must be addressed to enable correct program operation.

Code Block
bgColor#ccccff
FileOutputStream fos = new FileOutputStream("data.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(new Date());
oos.reset();  // Reset the Object-Handle table to its initial state
// ... 

...