Versions Compared

Key

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

...

  • a memory leak
  • an infinite loop
  • limited amounts of default heap memory available
  • incorrect implementation of common data structures (hash tables, vectors and so on)
  • unbound deserialization
  • writing a large number of objects to an ObjectOutputStream

Noncompliant Code Example (readLine())

This noncompliant code example places no upper bounds on the memory space required to execute the program. Consequently, the program can easily exhaust the available heap space.

...

Wiki Markup
According to the Java API \[[API 06|AA. Java References#API 06]\], {{BufferedReader.readLine()}} method documentation:

Wiki Markup
\[{{readLine()}}\] Reads a line of text. A line is considered to be terminated by any one of a line feed ('{{\n}}'), a carriage return ('{{\r}}'), or a carriage return followed immediately by a linefeed. 

Any code that uses this method is susceptible to abuse because the user can enter a string of any length. This does not require the noncompliant code example to read input using a loop.

Compliant Solution (use databases)

If the objects or data structures are large enough to potentially cause heap exhaustion, the programmer must consider using databases instead.

To remedy the noncompliant code example, the user can reuse a single long variable to store the input and write that value into a database containing a table User, with a field userID along with any other required fields. This prevents the heap from being exhausted.

Noncompliant Code Example

Wiki Markup
This noncompliant code example requires more memory on the heap than is available by default. In a server-class machine using a parallel garbage collector, the default initial and maximum heap sizes are as follows for J2SE 6.0 \[[Sun 06|AA. Java References#Sun 06]\]:

  • initial heap size: larger of 1/64th of the machine's physical memory on the machine or some reasonable minimum
  • maximum heap size: smaller of 1/4th of the physical memory or 1GB

This noncompliant code example requires more memory on the heap than is available by default.

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 
       // 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();
   }
}

Compliant Solution

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

...

This setting can be changed either using the Java Control Panel or from the command line. It cannot be adjusted through the application itself.

Noncompliant Code Example

Wiki Markup
According to the Java API \[[API 06|AA. Java References#API 06]\], Class {{ObjectInputStream}} documentation:

...

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 the ObjectOutputStream.reset() method be called so that references to previously written objects may be garbage collected.

...