Speed up Swing GUI Java Apps
I was writing a small database application using Java and Apache Derby in Netbeans. Soon, felt that the speed should be improved as it was slow compared to a c++ or python app.
There are a few tweaks which could give significant improvement in the speed of a swing gui application.
1. Multithreading – Proper use of threads can significantly improve the applications speed and also its “perceived speed” or responsiveness.
Time consuming tasks should not be done inside an event handler since this will effect the appearance of the gui component whose event handler this is. If a lengthy task for example is done inside the event handler of a menu then the menu may stay until the task is complete or if its a button then if may freeze i.e. stay pressed until the task is complete. This results into reduced usability.
Various solutions include SwingWorker , Threads , invokeLater. E.g.Time-consuming tasks like I/O operatings , Database operations or creation of some kind of object or child window should be put in a thread. Lengthy tasks inside an event handler should be moved into a SwingWorker Thread or invokeLater so that the handler can return immediately keeping the GUI component responsive.
A Thread :
new Thread () {
public void run () {
//Lengthy Code
}
}.start ();
A SwingWorker :
invokeLater :
Multithreaded applications when properly designed give even better results on multicore processors. In any case threads will definitely increase the responsiveness or perceived speed of the gui in particular.
2. JVM Switches – jvm switches can be used to modify parameters like minimum and maximum memory that a java app can use. For a complete list of jvm options look here.
Example :
java -jar -Xverify:none -Xms32m -XX:PermSize=32m -Dsun.java2d.noddraw=true application.jar
3. Creating objects before they are actually needed – Object creation is a process that takes time. For example creation of a window frame object . So a window object can be created beforehand and kept hidden. When needed it can be simply displayed by calling the show() method. This increases the speed of the application.
4. Reusing Objects – Reusing objects prevents creating them again and again and slowing down the app. This totally depends on the logic of the application how objects can be kept in memory and reused when necessary.
5. StringBuffer versus String – Strings are immutable means they cannot be changed once they are created. Hence any operation like concatenation done on a string results in creation of more string objects.
StringBuffer on the other hand is an object which can be changed and hence any operation like concatenation results into the same object being changed instead of creation of a new one.
Hence StringBuffer is faster than String. If the application uses too many Strings and string operations then StringBuffer should be used to gain speed.
Apart from these other techniques for speed gain include using native functions , AOT (Ahead of Time compilers) , JIT (Just in Time compilers) etc.
If hardware acceleration is enabled then using opengl can improve GUI speed and can be enabled using the switch :
-Dsun.java2d.opengl=true
Popularity: 6% [?]
















Hi there,
I stumbled upon your website while searching for a solution to my problem, which was exactly what this article about – my app’s GUI freezes when pressing a button which queries a database through a server. However, it seems like the Swingworker and invokelater examples that I’m particularly interested about, are missing. Can you do seomthing about this matter please?
Thanks in advance,
Ben Dash