Achieving High Performance Application in Java Coding!

Posted: January 7th, 2010 | Author: TnT Admin | Filed under: Coding | Tags: , , , , , | No Comments »

High performance web sites and applications can start from the design coding level.  Most of the time, performance tuning we view comes at the later stage of the development life cycle, in particular at the deployment stage.  What can we do by deployment stage?  We can only tune start up parameters, add more hardware, or add more network components in the architecture.  But what if the problem is in the JVM where you diagnose it to be poor application performance due to coding?  Changing codes at this stage will be more expensive and difficult unlike when you are in the development stage!

How can we build a high performance web site through coding then?  Here are some basic tips you should note during development which should not be a big hassle for you to make changes anytime!

1. Remove any unused variables – The first thing and most simple thing you can do is to avoid creating unused variables.  Use static code analysers and look for variables that are initialized but not used.  Sometimes, in the midst of development, we leave out variables that aren’t used anymore and this can be removed to converse memory.

2. Free all resources – All created resources and variables should be released once they are not used.  This helps converse memory.  This will help optimize the memory usage and thus reduce the need for garbage collection cycle to take place.  Resources such as database connections and files are simple examples that you should close and release the resources.

3. Free all resources in the ‘finally’ block – Similar to [2], we want to free all resources.  However, this is catered to “unhappy” flow of the program code.  Free all resources that are used when exception arises if applicable.

4. Avoid calling methods in loops – This can be circumvented when the compiler optimizes the code.  However, if it doesn’t the loop condition will be calculated for each iteration during the loop

// bad practise
public class MethodInLoop {
void method (Vector vector) {
for (int i=0; int < vector.size(); i++)
{
// do something
}
}
}

// good practise
public class MethodOutOfLoop {
void method (Vector vector) {
int size = vector.size();
for (int i=0; int < size; i++)
{
// do something
}
}
}

5. Avoid methods that may cause memory leaks – Methods such as split(java.lang.String), split(java.lang.String, int), substring(int), substring(int,int), nextElement(), nextToken(), nextToken(java.lang.String), group() and group(int) are candidates of memory leak.  There are quite a fair bit of search results on the methods that causes memory leaks.  You can refer to “Strings and Memory Leaks” from The Java Explorer for some information.  Avoid such methods whenever possible.  If not, do use it with care.

More to come on coding for high performance applications!  Share with us on your performance coding tips here too! :D

Related Posts



Leave a Reply