Optimal JVM parameters
boky March 29th, 2010
Since most of our software runs on JVM, we’ve had our share of experience with crashing JVMs (Out Of Any Kind Of Memory), tuning JVM and optimizing its performance. Some are on 32-bit JVMs, others on 64-bits. Some run many small applications, others run bigger applications. Some run resin, others JBoss yet there are some running Tomcat. Most are on Linux, some Windows.
But they all have some things in common:
- they must run 24/7
- they all run server VMs
- they are all web applications
While there is no real way to tell which options will make your JVM work best, we’ve found the following to be helpful. I thought it would be nice to share:
-Xms1024m -Xmx1024m
Setting both Xmx and Xms to the same value will help eliminate memory fragmentation. Your application should run a bit faster, as the VM will not start at minimum and allocate additional memory as the application server / your application starts up.
-XX:MaxPermSize=512M
You will most certainly want to increase your permanent generation size, as the default is often too small. Here we’ve set it to half of assigned memory and works quite fine.
-Xmn128m
Change the new generation size. If your application is creating and releasing a lot of objects in short time (usually correlated with number of connections, usage of your website), you may find yourself with a JVM crashing when the “eden / new generation” has been exhausted. This will show up as “100% eden” in the crash log. Increasing the new generation size might help.
-Xss2m
Each thread in the JVM will get its own stack. The default stack is quite small and setting it a bit higher is usually a good idea. 2048k should be enough usually appropriate for most usage scenarios, but your mileage may vary. Increased when you have really long stacks (quite common if using a servlet container + several frameworks). Your first sign of having a too short stack will be a StackOverflowError.
-Dsun.rmi.dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000
These two parameters increate garbage collection interval for RMI connections. Mostly a JBoss issue, but other app servers run just fine with this settings. Should also speed things up a bit.
-Dsun.net.inetaddr.ttl=30
Sun’s Oracle’s JVM has a “feature” which cashes all DNS lookups. Forever. This might be an issue if the some of the external services changes IP and you don’t restart your server very often (reCaptha is one such example). Set this parameter and have JVM forget lookups after a certain time.
-XX:+UseConcMarkSweepGC
Change the garbage collector. We’ve had better experiences using UseConcMarkSweepGC, especially on multi-processor machines. Although, it seems “G1” will be the best after it’s finished.
-XX:+CMSPermGenSweepingEnabled -XX:+CMSClassUnloadingEnabled
Support class unloading. Usefull even in production, as it allows you to completely undeploy the previous application if you’re doing hot-deployment. One option name is old (CMSClassUnloadingEnabled) the other new (CMSPermGenSweepingEnabled) but both still work though.
-Dfile.encoding=utf-8
Since we don’t speak US-ASCII only, we moved everything to UTF-8 several years ago. Even all our JSP pages are defined as @page encoding=”UTF-8” contentType=”..; charset=UTF-8” You would be amazed how are i18n problems just vanished. UTF-8 rules.
-Denvironment.type=prod
We “mark” our servers as “prod”, “test” and “dev”. In ideal world this would not be necessary, but sometimes the code needs to know in which environment it runs. This helps avoid confusion and have that same setting in several different places and each person inventing his own way of determening the enviornment.
-Dorg.apache.catalina.STRICT_SERVLET_COMPLIANCE=false
Tomcat goodie. Being too much servlet-compliant is not always a good thing(TM). This relaxes some strange constrains that makes applications — especially ones ported from other servlet containers — run better. Sometimes, applications will not run without this parameter at all. If you’re concerned about cross-container compatibility, leave this parameter out and develop exclusively on Tomcat. If you’re running applications developed by different teams and/or vendors, by all means, put it in.
-XX:+UseCompressedOops
Using compressed oopts will decrease your memory usage when running 64-bit JVMs. In a nutshell, it tells JVM to use 32-bit pointers around the code (where possible).
-ea
For development/staging only. Enable assertions. If you’re a prudent developer, you’re code will have a lot of sanity checks. This will enable them. This way it’s easier to spot errors before going live.
-Dcom.sun.xml.ws.transport.http.client.HttpTransportPipe.dump=true -Dcom.sun.xml.ws.transport.http.HttpAdapter.dump=true -Dcom.sun.xml.ws.util.pipe.StandaloneTubeAssembler.dump=true
For development/staging only. Enable debugging of JAX-WS /WSIT. All communication (XMLs, HTTP trafic) of webservices will be dumped to your STDOUT. If you’re having problems with webservices and don’t know what’s wrong, sometimes this is the only way to go.
If you have any other useful options, I’d be more than happy to add them to the list.
![[del.icio.us]](http://www.boky.cc/wp-content/plugins/bookmarkify/delicious.png)
![[Digg]](http://www.boky.cc/wp-content/plugins/bookmarkify/digg.png)
![[Google]](http://www.boky.cc/wp-content/plugins/bookmarkify/google.png)
![[LinkedIn]](http://www.boky.cc/wp-content/plugins/bookmarkify/linkedin.png)
![[StumbleUpon]](http://www.boky.cc/wp-content/plugins/bookmarkify/stumbleupon.png)
![[Windows Live]](http://www.boky.cc/wp-content/plugins/bookmarkify/windowslive.png)
![[Yahoo!]](http://www.boky.cc/wp-content/plugins/bookmarkify/yahoo.png)
![[Email]](http://www.boky.cc/wp-content/plugins/bookmarkify/email.png)










