简体   繁体   中英

can a java EE app use more than MAX heap memory

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 :

-Xmx256m

While this parameter set the start value :

-Xms128m

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 :

export CATALINA_OPTS=-Xms16m -Xmx256m;

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.

When you run your application you must also specify the max heap size.

For example if your class name is HelloWorld and if you want to set 512mb the heap size then you have to launch te following command:

java -Xmx512m HelloWorld

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM