IntelliJ Java IDE
The moose likes Performance and the fly likes Swing performance Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Performance
Reply Bookmark "Swing performance" Watch "Swing performance" New topic
Author

Swing performance

Payam Fard
Ranch Hand

Joined: Jan 31, 2003
Posts: 65
Hi all,
In a fairly large Swing applications, lots of Swing objects gets created and destroyed. Let's assume there is a form in an application that has about 300 widgets (including JTabbedPane, JTextField, JLabel, JButton, ...). Each time this form gets created in the application, we have to create all these widgets. Although when we dispose the JFrame holding these widgets, theoretically they become available for the garbage collector to clean up, we want to make sure they get cleaned up as fast as possible (as this could harm the performance of our application if they stay around too much). If I am right, we can call the System.gc(), but even this call does not force the garbage collector to run at that time. Is this correct?
Any thoughts on how to improve performance in this scenario?
Thank you,
Payam.
Greg Charles
Bartender

Joined: Oct 01, 2001
Posts: 1855

If possible, keep the form around but not visible. Each time it is displayed, repopulate it with relevant data, and make it visible again.
You're right, calling System.gc() does not absolutely guarantee the garbage collector will run. It's just a suggestion, which the JVM can ignore if it chooses.
Wouter Zelle
Ranch Hand

Joined: Apr 12, 2002
Posts: 30
It's a good idea to use the approach mentioned above for the most used forms, but you might not want to use it for rarely used ones. An option is to call System.gc() when you are already showing a progress indicator onscreen. That way the user will not notice the slowdown, it will just seem that the operation is a bit slower.
Michael Zalewski
Ranch Hand

Joined: Apr 23, 2002
Posts: 168
300 objects is not so much.
I would not be attempting to manipulate the VM's garbage collection to dispose of 300 objects.
If there is a JTabbedPane, you might consider delaying the creation of each pane until the user activates the pane. In other words, group each tab page into a JPanel (or some component) and don't populate the child components until the ChangeListener tells you that the page has been activated.
 
 
subject: Swing performance
 
Threads others viewed
Reducing window reduces memory use - why?
garbage collection
Garbage collection problems in java?
Explicit Garbage Collection invocation
Finalize and System.gc()
IntelliJ Java IDE