Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Windows opening and closing memory...  RSS feed

 
Tad Dicks
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm in the midst of writing a fairly complicated desktop app using swing (I'm sure everyone is sick of my questions as this is the first non-trivial swing app I've written). Anyways on to my problem, I've begun to run into memory issues, specifically problems with the "java heap". I've found a short term solution (using -Xmx1024m when running the app). Thats way more memory than I need at the moment, but I'm running test data thats probably on the order of 10-100 times smaller than what it eventually will be used for (and I'm not even done with all the functionality yet).

My biggest concern is whenever I open an internal frame (really an instance of my own class that extends internalFrame) the memory usage jumps from 500k to 10,000k but when closing it none of this memory seems to get released. In fact any time I open any window (frame, internalframe, dialog etc I can see the memory useage increase, and it never goes down).

Are these objects not being garbage collected? Is there something I should be doing code-wise to ensure garbage collection for these things? I've always assumed when an object is out of scope it gets collected. I'm not positive how this works with windows (particularly windows that get opened in new threads ala foxtrot/swingworker etc).

The end application shouldn't be as complex as a full featured IDE but I would expect its memory useage and interface to be almost that "intensive".
Any help is greatly appreciated. Anyone know of any books that deal with application design (using swing?). I think a book detailing the design (and to a lesser extent coding) of a complicated/large desktop app in java would be nice something like a "case study".

-Tad
 
Manuel Moons
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java has garbage collecting, this is true. But you still have to be careful when programming applications. An object can only be collected when no more references exist to it. Maybe you have listeners that are still active when you close your window. Remove those.

Do you use images? If you do not need those images in other screens, call the flush method. All resources used by them will get released. If you have large images that can used time and time again. Use the same image objects do not create new image objects every time.

When closing window you might consider a cleanup method that removes all listeners, cleans up images, ...
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also make sure that when you are closing windows you are calling the dispose() method on the window and not just setVisible(false). You can do this on the windowClosing event or you can do it with setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE).
 
Tad Dicks
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been sure to call dispose on all the windows, though the wording in the api about dispose for internal frames vs all other windows made me wonder if it really gc's the window.

I hadn't given any thought to removing listeners. More Often than not I have the class I'm using implementing the listener (or an internal class is implementing the listner) for the components in that object. So I would assume when the object with the listener, goes away I don't need to remove hte listener then. However I can see where when closing the internalFrames I need to remove the listener thats part of the desktop pane who's life cycle is as long as the app is running. That should definitely help.

-Tad
 
Jared Cope
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

This is an interesting topic as I suffer from the above problem a little bit too. My swing app is reasonably complex and after startup the memory usage is around 30MB (going by the process list in windows).

However, if I minimize the window and then maximize again this memory usage drops to around 15-20MB. It seems like GC kicks in when the window is minimized, but if the user never does this, then the application is destined to keep the full 30MB forever.

This doens't really help, but just wanted to share my observations.

Cheers, Jared.
 
Tyler Jordan
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any help is greatly appreciated. Anyone know of any books that deal with application design (using swing?). I think a book detailing the design (and to a lesser extent coding) of a complicated/large desktop app in java would be nice something like a "case study".



A great book that discusses design patterns is ("Head First" Design Patterns) by O'Reilly. This book has much less talk about code and more about what program design to use when creating different types of applications. Unfortunately, the book doesn't delve into Swing specifically, but some of the design patterns can be used to help implement Swing.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!