File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Performance and the fly likes Reducing window reduces memory use - why? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Performance
Bookmark "Reducing window reduces memory use - why?" Watch "Reducing window reduces memory use - why?" New topic

Reducing window reduces memory use - why?

David Porter

Joined: May 19, 2003
Posts: 1
I'm working on a Java application and have been trying to reduce the overall memory use. According to the Windows Task Manager, when my application first starts up it uses about 45,000 kb. If I reduce the program window (by clicking on the icon in the upper right-hand corner), and then restore it, the memory usage drops to about 25,000 kb. I've got two questions about this:
1) Why is this happening?
2) Is there any way I can make this happen automatically when the program starts up, so that it uses the smaller amount of memory from the outset, without having to reduce it manually?
Thanks for any suggestions!
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Welcome to JavaRanch, David Porter!
I'm moving this to the Performance forum...

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
I'm not too sure, but I'd guess that the building of the window and application startup leave a lot of garbage in memory that was only necessary to initialize stuff. I'm no GUI mechanics expert, but I'm sure there are all sorts of temporary objects created to format and diplay a GUI.
Just a thought: Start the program up with the java runtime option -verbose:gc to see if the garbage collector runs at all before you minimize the window. If not (or if the garbage collector runs so early in the process that it becomes ineffective), it will proably help to call System.gc() in your code shortly after opening the window.
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

What David said.

Plus, in Swing everything is double-buffered. This is done so that all the GUI components are redrawn smoothly and you can't see any components in a half-drawn state. This is done basically by using a buffer image in memory that everything is drawn to, and when it is done drawing, this buffer image is painted to the real screen image. Since the buffer image is the same size as the real frame, the amount of memory taken up by the buffer increases as the frame gets larger.
[ May 20, 2003: Message edited by: Nathan Pruett ]

Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
Start the program up with the java runtime option -verbose:gc
One thing to be aware of here is that the memory use reported by -verbose:gc refers to the allocated heap memory. The memory use reported by Task Manager refers to total memory allocated to a process - which is generally larger. In particular, any time your java classes invoke a native method (directly or indirectly) they may end up consuming memory somewhere outside the heap, not directly managed by the JVM. It's even possible that in some situations the memory may not appear in Task Manager as allocated to java.exe, but rather to some other process. (Dunno a good example of this; I think I've seen this sort of thing in Unix once or twice, not Windows - I'm just saying it could happen.) So pay attention to both the -verbose:gc output and the Task Manager display to get a complete picture.

"I'm not back." - Bill Harding, Twister
I agree. Here's the link:
subject: Reducing window reduces memory use - why?
It's not a secret anymore!