Java EE – Performance Tuning Tips
December 28, 2008 8 Comments
Every application goes through some performance issues whether it is an intranet or internet product. As development progresses, more concentration goes to resolving requirement and technical complexities than performance issues. It is essential to introduce a phase in your development life cycle to tune the product performance before it goes to production.
Performance tuning is an art; it requires lots of experience and out of box thinking. Though it is very difficult to provide common solutions to resolve performance issues in a J2EE application, there are a few common tips to resolve recurring performance issues. Every application has unique performance issues, though most of the applications have common performance requirements.
Static Content Serving
Most content in web pages by volume and by count is static; take example of www.amazon.com or www.cnn.com. So build a static content acceleration server to make the site faster, it means deploy the static contents in a separate web server and transparently route the static content request to the static content acceleration server. You can consider reverse proxy technique to do this.
Caching is one of the important techniques to improve performance. Many java architects tend to choose custom cache solution for their implementation because of the comfort level on coding and debugging. It is better to choose one of the best open source (EhCahce, OSCache, etc.) caching components to do caching. This will reduce development time, bugs in the code and more. Make sure the points given below are addressed while implementing cache:
- Don’t cache data unless it is known how and when to invalidate the cached entries.
- The caching components should work in a clustered environment.
- Should have option to measure the hits/miss counts to fine tune the caching.
- Use suitable algorithm to clear the cache periodically like LRU (Least Recently Used) and LFU (Less Frequently Used).
- Don’t cache too much data that consumes more heap size.
High Availability is another key attribute for a good product service. To make applications highly available, you have to ensure that both the Application Server and the Database server are up for a longer time. High availability cannot be achieved with only better software design it also involves network and hardware settings. However the software should run for longer duration. Few companies restart their application servers once in a week to avoid out of memory exception. On the application front the main bottle neck is memory leakage. Root cause of memory leak issues can be identified using the techniques given below:
- Profile the application using profiler to find the unused objects and time taken to execute each method. If possible go for jdk1.6 which has in-built profiler otherwise use one of the commercial profiler tools like YourKit, JProbe, JRockIt.
- Find the performance and correct code to remove unused objects after running application for a longer time on the test environment, i.e., more than two weeks.
- Adjust JVM heap size to best suit the server RAM.
- Fine tune the JVM garbage settings.
Database interaction is the performance bottleneck for many J2EE applications. Extreme caution should be exercised while using JDBC driver’s features extensively.
- Choose the right JDBC driver
- Use connection pools.
- Avoid excessive rollback.
- Choose optimal transaction isolation level.
- Use batch updates and batch retrieval.
- Optimize Fetch Size to reduce the network round trips between application and database server. Read More here.
- Use prepared statements at necessary places.
- Avoid frequent BLOB/CLOB updates.
Connection and thread pooling is also one of the important strategy to tune the performance. Some points to be considered while configuring connection and thread pool are listed below:
- In database driven application the thread and connection maximum count has to be defined based on the database capacity.
- Determine the optimal value of maximum thread and connection pool from performance tests. Read more here.
- Set the value of initial capacity equivalent to maximum capacity.
- Remember that there is no general thumb rule to define the maximum and minimum thread/connection pool size. These figures have to be derived based on the hardware capability and load testing.
SQL tuning is another way to improve the database retrieval performance.
- Measure all the SQL queries performance – optimize top 10 frequently used queries and top 10 queries that take longer time.
- Create optimal indexes wherever required.
- Make sure database server and application server are connected through high bandwidth Ethernet card.
- Use tools like IronTrackSQL and JDbMonitor to track the query performance from Java applications.
Many architects are afraid to make design changes. Refactoring is a very important engineering practice to keep code maintainable and continuously tune the application performance. Change the application design to improve performance. Make sure the following designs are good, if not refactor:
- Authentication technique – the amount of time taken to authenticate a user should be minimal (may be less than one millisecond)
- Session persistent – measure the session persistent time and fine tune the code.
In recent years distributed computing has become a popular solution for scalable applications. Make sure that the following points are addressed:
- Choose remote calls with careful decision. Avoid remote calls if possible.
- Measure all the remote calls’ response time and fine tune or cache frequently accessed data.
- Measure all synchronized remote calls like FileSystem, Database, Email, WebService and EJB.
- Asynchronous messaging is a proven communication model for developing large-scale, distributed enterprise integration solutions. Messaging provides more scalability because senders and receivers of messages are decoupled and are no longer required to execute in lockstep.
WebServer/ AppServer Tuning
Server tuning is another important area in performance tuning. Server tuning tips vary between different application servers. Below are a few common tips:
- Disable development environment like auto reloading JSP and Servlets
- Pre-compile jsp files
- Tune your logging to optimal level like INFO.
- Disable unused services like mail client service, JMS Server and JMXConsole.
Data transfer is another bottleneck area where application performance can be improved. Address the following points:
- Minimize the amount of data transfer between browser and web server. For example, don’t keep too many form variables.
- Use AJAX calls to fetch minimal/required data from web server.
Logging is essential for server centric applications. Excessive logging will cause performance issues. When in doubt disable all logging and check the application’s performance.
Mostly design determines an application performance unless the code is written badly. Avoid code optimization that reduces the maintainability. If you to specific on code optimization here are a few tips:
- Minimize object creation, use object pooling.
- Use correct collection type like ArrayList, LinkedList, etc.
- Avoid using System.out.
- Avoid repeatedly evaluating invariant expressions in a loop.
You can find lot more information from
Performance tuning is an art. There is no standard set of tips to fine-tune an application. Before starting the tuning process of an application, measure the application performance using load test and identify the bottleneck based on hard evidence such as profiler result. Performance tuning is an iterative process. Tuning does not end with deployment.