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