I checked with following code in my servlet:
int mb = 1024 * 1024;
Runtime runtime = Runtime.getRuntime();
out.write("Used Memory:" + (runtime.totalMemory() - runtime.freeMemory()) / mb);
out.write("Free Memory:" + runtime.freeMemory() / mb);
out.write("Total Memory:" + runtime.totalMemory() / mb);
out.write("Max Memory:" + runtime.maxMemory() / mb);
the output is :
Used Memory:10
Free Memory:46
Total Memory:57
Max Memory:57
I want my app not to use more than 64 MB heap? I want to know – is there any way my app can use more then 64 MB heap…(Max Memory:57) ? ..will my app throw an OutOfMemoryException after 57MB?
Default Max Heap size is 64Mo, the only way to get more is to set the max size with this parameter :
While this parameter set the start value :
Don’t forget the “m” at the end which means Megabytes, don’t give more start memory than max neither.
Furthermore, there is no setMaxHeapSize() function if it’s what you are looking for.
If you are runnning your servlet inside a tomcat you should try with this :
in startup.bat
If your servlet need more than JVM can allow, you will get weird errors when reaching the limit and most of the time a OutOfMemoryException, the Garbage Collector will try to avoid that though so it will crawl a bit before throwing it.