Two Laptop Bag
The moose likes Performance and the fly likes How to improve performance of Swing application Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Performance
Reply Bookmark "How to improve performance of Swing application" Watch "How to improve performance of Swing application" New topic
Author

How to improve performance of Swing application

Sanjay Chougule
Ranch Hand

Joined: Mar 04, 2008
Posts: 30
I have a Java audio player,developed in Swing and Sound API. Currently GUI thread are decreasing CPU performance.So please suggest me to improve performance.

Thanks in Advance.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35252
    
    7
How do you know that the GUI thread impacts performance? What is the GUI doing that would take a noticeable amount of time?


Android appsImageJ pluginsJava web charts
Sanjay Chougule
Ranch Hand

Joined: Mar 04, 2008
Posts: 30
I am using Jprofile which gives me following CPU usage report


85.6% - 2,058 ms - 1 inv. com..avp.XYZVoicePlayerFrame
5.8% - 138 ms - 6 inv. direct calls to methods of filtered classes
4.5% - 107 ms - 1 inv. java.awt.EventDispatchThread
4.0% - 96,049 �s - 1 inv. com.XYZ.avp.audio.PlayThread
0.2% - 3,763 �s - 1 inv. com..avp.audio.AudioRecorder

most CPUY consuming class is avp.XYZVoicePlayerFrame which is GUI class.

I think it is sufficient to understand issue..
thanks
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35252
    
    7
I doubt that it's Swing itself using up all that time - it's not a Swing class, it's one of your application's classes. Check the code if there's some busy waiting going on, or it includes time spent by the media player.

Also, 2 seconds isn't much to go by - I'd profile it for a longer time.
Sanjay Chougule
Ranch Hand

Joined: Mar 04, 2008
Posts: 30
yes, you are right.
Here are few threads going in wait state for long time.
Report showing that only running threads very few and waiting thread are 6 to 7 .
Then how to resolve this.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35252
    
    7
The important thing is not so much the number of threads, it's what the threads are doing. A sleeping thread doesn't use up CPU time. If a thread executes a loop over and over w/o doing anything, that's a problem. Any loop similar to this:

should be changed to something like this, using a sleep value that's appropriate for the application:

[ July 04, 2008: Message edited by: Ulf Dittmer ]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
JProfiler should also give you a report on which method in that class consumes the cpu that much.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Sanjay Chougule
Ranch Hand

Joined: Mar 04, 2008
Posts: 30
thanks for guiding.....

Currently following method is taking 19.6 % CPU useage: Please find out mistakes so that performance will improve.

As I am very fresher and also currently i am exploring Swing, your help is very valuable for me.

/**
* Sets the lF.
*
* @param LFclassname the new lF
*/
void setLF(String LFclassname) {
try {
UIManager.setLookAndFeel(LFclassname);
SwingUtilities.updateComponentTreeUI(this.avpFrame);
LookAndFeel lfcur = UIManager.getLookAndFeel();

for (int ii = 0; ii < this.lf.length; ii++) {
if (lfcur.getName() == this.lf[ii].getName()) {
this.LookAndFeelInuseIndex = ii;
this.LookAndFeelinuse = LFclassname;
}
}
} catch (ClassNotFoundException e1) {
} catch (InstantiationException e2) {
} catch (IllegalAccessException e3) {
} catch (UnsupportedLookAndFeelException e4) {
}
}

here is CPU useage Report:
Thread selection: All thread groups
Thread status: Runnable
Aggregation level: Methods

78.0% - 1,957 ms - 1 inv. com.xyz.avp.xyz.VoicePlayerFrame.main
71.0% - 1,783 ms - 1 inv. com.xyz..avp.xyz.VoicePlayerFrame.<init>
21.7% - 545 ms - 1 inv. com.xyz..avp.xyz.VoicePlayerFrame.loadPersist
20.8% - 522 ms - 1 inv. com.xyz..avp.xyz.VoicePlayerFrame.setLF
19.6% - 491 ms - 1 inv. javax.swing.SwingUtilities.updateComponentTreeUI

13.7% - 343 ms - 1 inv. com.xyz..avp.AvpMediator.setFileChoose
13.2% - 331 ms - 1 inv. javax.swing.JFrame.<init>



Thanks once again.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35252
    
    7
The code you posted does nothing that would take any appreciable amount of time, so we can't guess what might be going on.

But this looks wrong (not that it has anything to do with performance). You should never compare strings using "==" - that's what the equals method is for:


This is bad, too. You should at least print an error message to System.err so that you know something went wrong:
Sanjay Chougule
Ranch Hand

Joined: Mar 04, 2008
Posts: 30
Thanks.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
How long did the application run already, when you created the cpu usage report?
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: How to improve performance of Swing application
 
Similar Threads
Migration from Swing to Web Application
Heap size
when to use threads in swing applications?
To be static or not to be static
Does interning a String improve performance?