A Java OutofMemoryError occurs if infinite heap space is assumed, making the program crash. This error can be generated due to the following possible reasons:
- A memory leak
- An infinite loop
- The program requires more memory than is present by default in the heap
- Incorrect implementation of common data structures (such as hash tables, vectors etc)
Non Compliant Code Example 1
This example uses unlimited amount of memory due to which the program can easily exhaust the heap.
A heap error will be generated if the heap is continued to be accessed even if there is no memory left in 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(){ do{ //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 { newName = reader.readLine(); if(!newName.equalsIgnoreCase("quit")){ //names are continued to be added without bothering about the size on the heap names.addElement(newName); } } catch (IOException e) { } System.out.println(newName); } while (!newName.equalsIgnoreCase("quit")); } public static void main(String[] args) { ShowHeapError demo = new ShowHeapError(); demo.addNames(); } } |
Compliant solution 1
To handle this problem, where the data structure size is so big that the heap gets exhausted, the user should consider using databases, where the record will get written on to the disk. Hence, this structure will never outgrow the heap.
In the above example, the user can re-use a single Long variable where the input gets stored and write that value into a simple database, containing a table User with a field userID and any other required fields. This will prevent the heap to get exhausted.
Non Compliant Code Example 2
Wiki Markup |
---|
In this example, the program needs more memory on the heap than the default. In a server-class machine running either VM (client or server) with parallel garbage collector, the default initial and maximum heap sizes are for J2SE 5.0 \[1\]: |
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
Code Block | ||
---|---|---|
| ||
public class ShowHeapError { /*assuming the heap size as 512mB (calculated as 1/4th of 2 GB RAM = 512mB) * Considering long values being entered (64 bits each, the max number of elements * would be 5122mB/64bits = 67108864) */ Vector<Long> names = new Vector<Long>(67108865); 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(" 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<count || newID!=-1); } public static void main(String[] args) { ShowHeapError demo = new ShowHeapError(); demo.addNames(); } } |
Compliant solution
Wiki Markup |
---|
This exception can be avoided by either making sure that there are no infinite loops or memory leaks. If the programmer knows that the application would require a lot of memory then, he can increase the heap size in Java using the following run time parameters \[2\]: |
java -Xms<initial heap size> -Xmx<maximum heap size>
for example:
java -Xms128m -Xmx512m ShowHeapError
Here we have set the initial heap size as 128Mb and the maximum heap size as 512Mb.
This setting can be done either in the Java Control Panel or on the command line. This setting cannot be controlled in the application itself.
Risk Assessment
It is difficult identifying a heap exhaustion, since static analysis tools would not be able to pin point anything and the heap size could be different for different machines.
In case the heap size is increased through the command line, then the assessment would be as follows:
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
FIO37-J | low | probable | medium | P4 | L3 |
In case the database solution is used, the cost would increase to high due to the coding of a disk-based solution.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
FIO37-J | low | probable | high | P2 | L3 |
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website
References
[1] Garbage Collection Ergonomics Default values for the Initial and Maximum heap size
[2] Non Standard Options for java: The Java application launcher Syntax for increasing the heap size
FIO06-J. Validate user input 07. Input Output (FIO) FIO31-J. Create a copy of mutable inputs