...
Noncompliant Code Example 1
This example uses an unlimited amount of memory, places no upper bound on the memory space required due to which the program can easily exhaust the heap.
A heap error will be generated if the heap continues to be accessed populated even if there is no memory left in the heapspace available.
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{ //addingAdding 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 diskIf the objects or data structures are large enough to potentially cause heap exhaustion, the programmer must consider using databases instead, to ensure that records are written to the disk in a timely fashion. Hence, this structure will never outgrow the heap.
In the above example, the user can reuse a single long
variable where to store the input gets stored and write that value into a simple database containing a table User
with a field userID
and along with any other required fields. This will prevent the heap from getting exhausted.
...
Wiki Markup |
---|
In this example, the program needs more memory on the heap than is available theby default. In a server-class machine running either VM (client or server) with a parallel garbage collector, the default initial and maximum heap sizes are as follows for J2SE 5.0 \[1\]: |
...
- 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 2
Wiki Markup |
---|
This exceptionThe {{OutOfMemoryError}} can be avoided by making sure that there are no infinite loops or, memory leaks. Ifor theunnecessary programmerobject knowsretention. thatIf thememory applicationrequirements wouldare requireknown a lotahead of memory, he can increasetime, the heap size in Java can be tailored using the following runtime 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 to 128MB and the maximum heap size as 512Mbto 512MB.
This setting can be done either in the Java Control Panel or on the command line. This setting It cannot be controlled in adjusted through the application itself.
Risk Assessment
It is difficult identifying to identify code that can lead to a heap exhaustion , since static analysis tools would not be able to pinpoint anything and the heap size could be different for are currently unable to pinpoint violations. The heap size may also differ in different machines.
In the case of the heap size being increased through the command line, then the risk assessment would be as follows:
...
In the case of the database solution being used, the cost would increase to high due to the coding usage of a disk-based solution.
...