0

We normally give the value for -Xmx and -Xms while executing a Java application and increase the heap size as per our requirement (to avoid out of memory error).

My question is how do we calculate the memory requirement? Let us take below example :

for(int i=0 ; i<1000; i++){
try{
     array.add(new ArrayList<Integer>(100000000));
  }catch(OutOfMemoryError e){
   }
}

First this program runs with -Xmx128M and later run using -Xmx256M. Will the program run in both the conditions? If not then what could be the reason and can we calculate the appropriate value of memory argument needed to run this program?

Aalekh
  • 400
  • 2
  • 7
  • 12
  • 2
    `catch(OutOfMemoryError e)` frightens me a bit. It suggests that you want to write code that will run when your JVM has no memory left. – Dawood ibn Kareem Mar 26 '15 at 01:45
  • @DavidWallace No i kept it just for displaying purpose. In real coding practice its of no use – Aalekh Mar 26 '15 at 01:48
  • possible duplicate of [What is the memory consumption of an object in Java?](http://stackoverflow.com/questions/258120/what-is-the-memory-consumption-of-an-object-in-java) – k_g Mar 26 '15 at 04:20

0 Answers0