Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: shortened NCE, I can even eliminate the loop but then the NCE wouldn't seem like a genuine coding mistake.

A Java OutofMemoryError occurs if the program attempts to use more heap space than what is available, even after garbage collection. Amongst othersother causes, this error can may result from:

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

...

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.

Code Block
bgColor#FFcccc
public class ShowHeapError {
  Vector<String> names = new Vector<String>();
  InputStreamReader input = new InputStreamReader(System.in);
  BufferedReader reader = new BufferedReader(input);

  public void addNames() throws IOException {
     while(true)String newName;
    do {     
      // Adding unknown number of records to a list; user can exhaust the heap
      String newName = reader.readLine();
      if(!newName.equalsIgnoreCase("quit")) { // Enter "quit" to quit the program
        names.addElement(newName);
      } else {
    	break;
      }
    }} while(newName.equalsIgnoreCase("quit") == false); // Enter "quit" to quit the program
    // Close "reader" and "input"
  }

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

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

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 example to read input using a loop.

Compliant Solution

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 getting being exhausted.

Noncompliant Code Example

...

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

...