To avoid getting java.lang.OutOfMemoryErrors, while running web application, we should increase heap size allocated by the JVM by using command line options.
-Xms<size> set initial Java heap size
-Xmx<size> set maximum Java heap size
-Xss<size> set java thread stack size
To do this, follow these steps:
1. Open the Server Configuration in Eclipse by double-clicking on the Server instance.
2. Click on “Open launch configuration” link
3. Click on the Arguments tab and add the following command at the end of VM arguments: -Xms128m –Xmx1024m.
These values may vary, depending on requirement/available memory
If you are not using any server, you can type the following on the command line before you run your program:
java -Xms64m -Xmx1024m MyProgram
- Do not set -Xmx to too small value
- Set -Xms to a small value
Setting -Xmx to small value mostly leads to OutOfMemoryErrors, because this is the maximum amount of memory you are allocating for Java and it cannot utilize memory beyond the set value.
Also If you set -Xms to higher value you might run out of memory. So try to keep it to a small value like -Xms16m.