One of the biggest concerns with using swing is that over time, the application takes more and more memory. I've noticed this with a lot of swing applications. I know some of it is due to the internal design of the swing components, but I'm guessing there are certain rules we can follow to minimize this. Is there a way to keep the memory consumption down in a swing app over time? How would I regulate it? For example, is there a way to find out at runtime how the memory consumption of the Java App is? Could this be used to discover areas that maybe add the most un-reclaimed memory?
Sounds like the job for an optimizer. Using a tool like OptimizeIt, you can find out how many objects are being allocated and where it's happening. I've worked with an optimizer several times and have always been able to significantly increase the speed of the application and reduce its memory footprint.
Hello, OptimizeIt is a borland product. Do you have to use it with JBuilder or is it possible to use it alone?
By constantly trying one ends up succeeding. Thus: the more one fails the more one has a chance to succeed.
posted 17 years ago
Thanks for the reply. Yes, and no. I know that swing programs in particular have some areas where you have to be extra careful. It's not exactly the same as performance. The program may be performing quite well, but many of the gui elements could be building and building in memory consumption to where it will cause a problem later in the running.
OptimizeIt can be run as a stand-alone program, you don't have to have JBuilder.
Memory consumption is one of many aspects of performance. Here's a good breakdown of the section on Profiling to Locate Memory Leaks in the Java Platform Performance book that should help you understand what needs to be done:
1.) Determine that there really is a memory leak. If memory usage increases and drops, it is normal execution since the garbage collector only runs every so often. If memory usage keeps climbing up and up and never goes down, then there is a memory leak.
2.) Determine what objects are responsible. This is where OptimizeIt (or another profiler) comes in. Start your program. Get the program into some state you can return to later. Run the gc option to garbage collect all unused objects. Work with your program a bit, then return to the original state again. Run gc again. See what class has increased instances. One or more classes that use this class are causing your problem.
3.) Trace references. Figure out what classes are keeping unneccessary references. This should be easy with OptimizIt because it provides references for all objects right in the GUI. Otherwise you're going to have to dig through code.
Swing itself shouldn't have memory leaks in it... enough people are running profiling tools on their apps to know if it was. It's something in the code you are using to interface to it...
BTW, Java Platform Performance is by Steve Wilson and Jeff Kesselman and is published by Addison-Wesley. I'd highly recommend it, it has lots of great pointers in increasing performance (speed, memory, algorithms, etc) in many different kinds of apps.
Write once, run anywhere, because there's nowhere to hide! - /. A.C.