Java EE – Performance Tuning Tips

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

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:

  1. Don’t cache data unless it is known how and when to invalidate the cached entries.
  2. The caching components should work in a clustered environment.
  3. Should have option to measure the hits/miss counts to fine tune the caching.
  4. Use suitable algorithm to clear the cache periodically like LRU (Least Recently Used) and LFU (Less Frequently Used).
  5. Don’t cache too much data that consumes more heap size.

High Availability

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:

  1. 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.
  2. 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.
  3. Adjust JVM heap size to best suit the server RAM.
  4. Fine tune the JVM garbage settings.

JDBC Optimization

Database interaction is the performance bottleneck for many J2EE applications. Extreme caution should be exercised while using JDBC driver’s features extensively.

  1. Choose the right JDBC driver
  2. Use connection pools.
  3. Avoid excessive rollback.
  4. Choose optimal transaction isolation level.
  5. Use batch updates and batch retrieval.
  6. Optimize Fetch Size to reduce the network round trips between application and database server. Read More here.
  7. Use prepared statements at necessary places.
  8. Avoid frequent BLOB/CLOB updates.

Connection/Thread Pooling

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:

  1. In database driven application the thread and connection maximum count has to be defined based on the database capacity.
  2. Determine the optimal value of maximum thread and connection pool from performance tests. Read more here.
  3. Set the value of initial capacity equivalent to maximum capacity.
  4. 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

SQL tuning is another way to improve the database retrieval performance.

  1. Measure all the SQL queries performance – optimize top 10 frequently used queries and top 10 queries that take longer time.
  2. Create optimal indexes wherever required.
  3. Make sure database server and application server are connected through high bandwidth Ethernet card.
  4. Use tools like IronTrackSQL and JDbMonitor to track the query performance from Java applications.

Refactoring

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:

  1. Authentication technique – the amount of time taken to authenticate a user should be minimal (may be less than one millisecond)
  2. Optimize static resource access like images and javascript – cache frequently accessed static resources or move static resources to other web servers like apache.
  3. Session persistent – measure the session persistent time and fine tune the code.

http://en.wikipedia.org/wiki/Code_refactoring

Remote Calls

In recent years distributed computing has become a popular solution for scalable applications. Make sure that the following points are addressed:

  1. Choose remote calls with careful decision. Avoid remote calls if possible.
  2. Measure all the remote calls’ response time and fine tune or cache frequently accessed data.
  3. Measure all synchronized remote calls like FileSystem, Database, Email, WebService and EJB.
  4. 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:

  1. Disable development environment like auto reloading JSP and Servlets
  2. Pre-compile jsp files
  3. Tune your logging to optimal level like INFO.
  4. Disable unused services like mail client service, JMS Server and JMXConsole.

Data Transfer

Data transfer is another bottleneck area where application performance can be improved. Address the following points:

  1. Minimize the amount of data transfer between browser and web server. For example, don’t keep too many form variables.
  2. Use AJAX calls to fetch minimal/required data from web server.

Logging

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.

Code Optimization

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:

  1. Minimize object creation, use object pooling.
  2. Use correct collection type like ArrayList, LinkedList, etc.
  3. Avoid using System.out.
  4. Avoid repeatedly evaluating invariant expressions in a loop.

You can find lot more information from http://java.sun.com

Conclusion

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.

Advertisements

8 Responses to Java EE – Performance Tuning Tips

  1. Anonymous says:

    Nice blog entry – but it would be nicer if you can reveal the source of information so that we can GET to the source :)

    somehow – when i read it I felt I am reading Rod’s book….but then two architects can definitely think alike :)

  2. Venkat says:

    I too have read Rod’s book. I am sure this article has less influence on Rod’s book. The information in this entry is not a rocket science; most of the points are common across the Java industry. Source of the information is Google search; you can find all these points in many sites.

  3. Anonymous says:

    Hi again !

    What do you mean by saying ‘I am sure this article has less influence on Rod’s book’ ???

    Sorry didn’t quite get it – but I am hoping that it is not what it seems it is !

    By the way when you do write a blog you need to publish references (serious blogger would do that), IMHO, it becomes even more important if your blog is inpired by someone elses experiences :)

    For instance your blog is a collection of important points collected from other websites or cut and paste from some good books. It is a good thing but it needs to be told to the outside world that – it stands for that. It should not stand for or try to give an impression that it is your findings – when all you are trying to do is collocate.

    Also – blogging again stands for personal experiences so it becomes even more important to give it your touch, it needs the Venkat touch :) as your blog entry ‘Java EE – Performance Tuning Tips December 28, 2008 — Venkat ‘ has your name on it.

    What has Venkat done – collected some facts from others and published here, well Venkat can do much better . . . .

    Few more example of what made me spend some time here –
    under code optimization in your blog you have pointed the users to sun.com , now that’s way too much !

    If the stuff here is common(as you said ‘most of the points are common across the Java industry’) – why should I visit your blog then? Probably not . . . . .

    Harsh truths but I am hoping that you will take it in good spirit, Venkat.

    Take care and rethink about serious blogging – mainly blogging needs experience, lets not just follow the fad !

  4. Venkat says:

    On previous comments when you said “looks like Rod’s book” then I realized that it is one of my close friend trying to pull me. Anyhow I will respond all your questions/comments. I like criticism.

    References: Your point is valid that it has to have references. If you see the above article has java.sun.com as reference under code optimization section where my touch is not there. You can find wikipedia references in many my articles.

    Copy/Paste: I am not a stupid blogger where I copy/paste contents from different website and publish as mine. If you have really time to read all my blog entry you will come to know about my experience on each topic. I am sure you are one of the reader goes through just title and come to conclusion that is same as written by others.

    Personal Experience: I do write only if I experience such a scenario personally. For example there are so many performance tuning tips; this article doesn’t cover it all; it contains only the points I believe and experienced in my work.

    I have cleared referred Sun site under Code optimization topic. Didn’t you look into that?

    Your criticism are most well come, not just what you think; it all about what world thinks.

    Again I would like to re-emphasize that all my blog entries are personal experience and touch is that I write the way I understand.

    Also remember that a person’s thinking/writing style will not change between each article. Definitely it will have his fundamental thoughts and style repeated for each article.

    FYI. I wrote this article about two years.

    “Criticism may not be agreeable, but it is necessary. It fulfils the same function as pain in the human body. It calls attention to an unhealthy state of things.“
    – Winston Churchill

  5. Anonymous says:

    Seriously I wish I had more time, but this is my last post on this subject as I know this is not going anywhere and I kind of can anticipate your reply :)

    Anyway below is some food for thought for you –

    Your entry in the blog above-
    Mostly design determines an application performance unless the code is written badly.
    From Rods Book-
    Since design largely determines performance, unless application code is particularly badly written…..

    mmmm how close is that for comfort ?

    Ok another one below –

    Your entry –
    Minimize object creation, use object pooling.
    From Rods Book –
    Minimize object creation, through techniques such as object pooling

    Of course your not stupid !

    You need to fix the unhealthy part brother…..sooner the better.

    chill.

    PS: there are loads of other stuff which can be pointed out, time is a problem but I am done here, yes sir……

  6. Venkat says:

    Thanks for your serious advice/criticism.

  7. Anand says:

    A very well written article.

    Anonymous – I agree all the information above is available on the internet or in Rod’s book but this is nicely summarized and well written. Why would you need Wikipedia when all of the information is available through Google searches ? Same here. It is all in one place and concise.

    I don’t know who you are or what you do but you need to seriously take some lessons on internet behavior !

  8. Pingback: Event Training Australia » Blog Archive » Java EE – Performance Tuning Tips

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: