Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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
bgColor#FFcccc
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();
    }
}

...

maximum heap size:  Smaller of 1/4th of the physical memory or 1GB

Code Block
bgColor#FFcccc
 publicpublic 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();
    }
}

...