Java Performance Tips

Try, catch block should not be in loop (unless specifically required).


All Collection API Classes should be specifically reinitialized to null (since JVM has to specifically do it & GC spends much time in doing these functions).


Release JDBC resources when done with tasks.


Use inner join instead of multiple queries.


Optimize SQL queries.


Always use Dynamic SQL (Prepare Statement) rather than the static SQL like Statement.


Try to avoid repeated DB calls to retrieve the same data. Instead of that retrieve the data from the table and keep the data in Java objects and use that in the subsequent call. (make Model class for that and use getter, setter methods)


Avoid Select * instead give the names of fields to be selected in query (e.g. Select id,name from Student).

Using getXX(int ColumnIndex) instead of getXX(String columnName): Depending upon the no of columns in your resultSet, a getXX field operation using column Index is two times as fast as than using Column Name.


Do not use Vectors, instead of that use Collection classes like ArrayList.


Avoid using method call as loop termination test. E.g. Don’t use list.size(), instead use store that in a temporary variable and then use the temporary variable for testing loop termination test.


Avoid throwing Exception in normal flow of execution since try-catch-finally block is costly.


Build proper WHERE clause in SQL SELECT for database indexing. (In left of Join select table with less no of records).


Try using java Collection classes instead of array.


String Concatenation: Check that For String concatenation use StringBuilder (JDK 1.5 has StringBuilder over StringBuffer) instead of String ‘+’ operator.


Debugging Statements: Do not use System.out.println or printstackTrace(). Use log4j.debug() for debugging. In catch blocks add log4.error() wherever applicable.


Prepare Statement: Use prepared statement for retrieving data when we have parameterized queries(insert, update, delete) else use statements.


Avoid object instantiation inside Loop. If possible create object outside loop and reuse the same object inside the loop.


Minimize object lifetimes. Keep the lifetimes of your data as short as possible. Place the declaration in the most nested block that first uses the data. If operator new is used, do the new just before the first use, and the set the reference to null just after the last use.


Minimum use of Session variables in the code instead set variables in Request. Set session variables as null after use.


The constants are not static we can make it final for better performance(e.g. if we have fix value for variable than make it final private void testMethod(final int id){}).


Apply Transactions wherever necessary.


Apply Query Caching if is required as per functionality.


Minimize data conversions or Castings


Try to use primitive java objects instead Wrapper classes as like Wrapper classes (i.e. Integer) allocate large data amounts in memory.


Replace a long if-else-if chain by a switch if possible; this is much faster.


Declaring a method as private, final, or static makes calls to it faster. Of course, you should only do this when it makes sense in the application.


Do not add JavaScript directly in the JSP Code; instead include JS File and include in Jsp (e.g <script src=”include/common.js”></script>).


Use STRUTS, JSTL specific UI Tag Library in JSP pages.


Exceptions are taken care in the code and highest levels of Exceptions are routed to Error Page; others are properly logged using logger.