Skip to content


First step in Java application optimization

We are always trying to optimize our applications, make them run faster, use less resources, be more stable. Unfortunately this part of the development process is often left as a finishing part and sometimes is completely skipped.

And yet it is so simple to do those improvements while developing applications. One of them is not to use import package.*; and only include the packages you really need. This may sound so simple but I doubt many people think about this when developing applications. I have often heard (and yes i have done that mistake myself recently) people say stuff like “I cant be bothered with every single include, i rather just include everything” and “It doesnt really matter”.

And to be all honest – it is true when developing small applicationsm that do not require to be run for a very long time without destroying the VM (a.k.a restarting the application). But in the long run – the more classes you are importing, the more PermGen memory is used — and that is a waste when you do not need so many classes.

So here is a first and really basic optimization suggestion:

Try avoiding importing * packages as much as possible. I do realize that it may not always be easy to do and your code may become a few hundered lines larger — but in the long run you will not regret it!

Now a fast numbers to prove my point

Unoptimized application PermGen usage:14.5Mb With the following * imports:

import java.util.*;
import java.io.*;
import java.sql.*;

Optimized application PermGen usage: 10.2Mb When replaced * imports with in somewhat 30 classes:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Iterator;
import java.util.Vector;

As you can see numbers speak for themselves – that is quite an optimization

Posted in Java, Programming, Work. Tagged with , , .

0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

Some HTML is OK

(required)

(required, but never shared)

or, reply to this post via trackback.