Java memory tuning
From Oxxus Wiki
Visit our java hosting page for professional java hosting solutions and top quality support from java experts.
Contents |
Introduction
Here are several tips regarding tuning up Java services which can be achieved in 5, below described, steps.
Step 1 – Increase JVM heap memory
One of the most common ram related errors with Tomcat services is “Permgen Space” error, which means “OutOfMemoryError”. This error mostly occurs in the production environment. It's triggered because Tomcat has very small memory for the running process. Error can be resolved by changing ram features in the Tomcat configuration file named catalina.sh. The change to be made is to increase the JVM heap memory. That is,the JVM does not invoke the garbage collector often, so the server can focus more in serving web requests and the requests are completed faster.
JAVA_OPTS="-Djava.awt.headless=true -Dfile.encoding=UTF-8 / -server -Xms1024m -Xmx1024m / -XX:NewSize=512m -XX:MaxNewSize=512m -XX:PermSize=512m / -XX:MaxPermSize=512m -XX:+DisableExplicitGC" /
-Xms – initial heap memory -Xmx – maximum heap memory
To apply changes just restart the Tomcat Server.
Step 2 – Resolve JRE memory leaks
One of main reasons for the performance lack is memory leak. It's the best to always use the latest Tomcat server to get better performance and scalability. This error can be resolved if you use the latest Tomcat server version 6.0.26 and above. Since those versions, a listener to handle the JRE and permgen memory leak, is available and here it is
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
Above listener class configuration is in the server.xml file which resides in “tomcat project folder/conf/server.xml”.
Step 3 – Thread pool setting
The number of web request loads that come in, is defined with threads number. This part should be handled carefully in order to get the better performance. This can be accomplished by tuning the connector attribute “maxThreads”. The value of the maxThreads should be based on the volume of the traffic. Setting value too low won't enable enough threads to handle all of the requests, while setting it too high means then the Tomcat startup time will take longer.
<Connector port="8080" address="localhost" / maxThreads="250" maxHttpHeaderSize="8192"/ emptySessionPath="true" protocol="HTTP/1.1"/ enableLookups="false" redirectPort="8181" acceptCount="100"/ connectionTimeout="20000" disableUploadTimeout="true" />
Above, maxThreads value is given as “250. Any further simultaneous requests will receive “connection refused” errors until another request gets freed. The error looks like the below,
org.apache.tomcat.util.threads.ThreadPool logFull SEVERE: All threads (250) are currently busy, waiting. Increase maxThreads (250) or check the servlet status
If the application results in the above error, always investigate whether the above error is caused by a single request that takes too long. The reason is that sometimes if the database connection is not released, it will not allow for processing additional requests.
Note: If the number of request exceeds 750 means, instead of setting the value 750 in the maxThreads attribute, it is better to go for the "Tomcat Clustering” with multiple Tomcat instances. In the case of 1000 request, set maxThreads=500 for the two instances of Tomcat instead of a single Tomcat with maxThreads=1000.
Step 4 – Compression
Tomcat has an option to compress the mime-types by doing some configuration in the server.xml file. Compression feature should be done in the connector like the below
<Connector port="8080" protocol="HTTP/1.1" \ connectionTimeout="20000" \ redirectPort="8181" compression="500" \ compressableMimeType="text/html,text/xml,text/plain,application/octet-stream" />
In the preceding configuration, the files will be compressed when the number of bytes is >= 500. If the files are not to be compressed by the size means, set the attribute compression=”on”. Otherwise the default setting in Tomcat is “off”.
Step 5- Database performance tuning
Tomcat's performance drops down while waiting for the database queries to get executed. Most of the application uses Relational databases, which may contains “NamedQueries”. By default, the Tomcat will load the named queries initially, this may increase the performance. Always ensure that all the database connections are closed properly. Setting the correct value in the database connection pool is also very important. The values in maxIdle, maxActive, and maxWait attributes of the Resource element. You can find the correct values by invoking the performance testing for the database.