Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Swing performance  RSS feed

 
Payam Fard
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 3014
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!