A Java OutofMemoryError
occurs if the program attempts to use more heap space than is available, even after garbage collection. This Amongst others, this error can result from:
- A memory leak
- An infinite loop
- The program requires more memory than is available by default on the heapLimited amounts of default heap memory available
- Incorrect implementation of common data structures (hash tables, vectors and so on)
- Unbound deserialization
- Upon writing a large number of objects to an
ObjectOutputStream
Noncompliant Code Example
...
This noncompliant code example places no upper bounds on the memory space required to execute the program. Consequently, the program can easily exhaust the heap.
Code Block | ||
---|---|---|
| ||
public class ShowHeapError { Vector<String> names = new Vector<String>(); String newName=null; InputStreamReader input = new InputStreamReader(System.in); BufferedReader reader = new BufferedReader(input); public void addNames() throws IOException { dowhile(true) { // Adding unknown number of records to a list // the ; user can enter as much data as he wants and exhaust the heap System.out.print(" To quit, enter \"quit\"\nEnter record: "); try { String newName = reader.readLine(); if(!newName.equalsIgnoreCase("quit")) { // names are continued to be added without // bothering about the size on the heapEnter "quit" to quit the program names.addElement(newName); } } catch (IOException e) else { // forward to handler break; } } // System.out.println(newName); }while (!newName.equalsIgnoreCase("quit"));close "reader" and "input" } public static void main(String[] args) throws IOException { ShowHeapError demo = new ShowHeapError(); demo.addNames(); } } |
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 exhausted.
Noncompliant Code Example
...
Wiki Markup |
---|
InThis noncompliant thiscode example, therequires program needs more memory on the heap than is available by default. In a server-class machine running either VM (client or server) with 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]\]: |
...
- maximum heap size: smaller of 1/4th of the physical memory or 1GB
Code Block | ||
---|---|---|
| ||
public class ShowHeapError { /** 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>(67108865); // 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 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\nEnter recordID): "); newID = reader.nextLong(); //names are continued to be added without bothering about the size on the heap names.addElement(newID); System.out.println(newID); i++; }while (i<counti < count || newID != -1); } public static void main(String[] args) { ShowHeapError demo = new ShowHeapError(); demo.addNames(); } } |
Compliant Solution
...
Wiki Markup |
---|
The {{OutOfMemoryError}} can be avoided by making sure that there are no infinite loops, memory leaks or 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]\]: |
...
This setting can be changed either in using the Java Control Panel or on 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: |
...