aspose file tools*
The moose likes Java in General and the fly likes how to minimize heap space Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to minimize heap space" Watch "how to minimize heap space" New topic

how to minimize heap space

reubin haz
Ranch Hand

Joined: May 12, 2005
Posts: 287
I built a program and its size is increasingly becoming large. In order to avoid running out of heap memory later, I want to carefully define any variables inside my classes.

But before this, I want to clarfy one thing first, which is, local variables are stored on the stack; instance variables are stored on the heap. -- someone please check this statement's correctness.

If what I wrote above is right, is it a good way to define more local variables and less instance variables would make my program consume less memory? Is it a good way to define more StringBuffer variables instead of String variables so that there are less number of objects are created by JVM, which results in less memory consuming? What else technique could we do in order to minimize heap space?

[ December 01, 2005: Message edited by: reubin yi ]

Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Can you prove that heap usage is a problem? In JDK 5 you can use the JConsole to monitor your app and see what's going on. Even if you're approaching current heap limits, you can probably raise the heap size when you start Java.

If you're convinced the heap is a problem you may need some analysis tools to look into the JVM. I'll look to others for recommendations on tools here.

We've had several kinds of problems lately.

One is keeping data too long. We have an application cache that is supposed to throw out anything that hasn't been used in an hour. When the cleanup wasn't working, it could get big enough to cause problems. It might take all day or all week. Some things have to stay in memory for a long time, but make sure it's only the right things.

Another was a spike in memory use in a data transformation. In this case from an XML DOM to HTML as a String. This data made it back from the database, converted to a data transfer object, converted into XML and then killed the XML to HTML processor. We bought that bit without source code so we had to throttle our own data.

Another had an endless loop with a partner system. We asked for data and they sent us a chunk of data with a flag that said there was more. We asked for the next chunk and they mistakenly sent us the same chunk again. We kept asking, they kept sending and we filled up a collection of some kind and crashed.

All of these were pretty major design problems or defects, not something as simple as "use more local variables and fewer instance variables." Be aware of the scope and lifespan of your variables and don't keep them beyond what the logic requires. Locals go away when the method exits, instance variables go away when the object is garbage collected, statics maybe never go away. Any of these scopes can be promoted up if you give some other object a reference to them, say an object that puts everything it touches into a static List.

Any of that help?

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
reubin haz
Ranch Hand

Joined: May 12, 2005
Posts: 287
that's a great help to me, thanks stan.
jQuery in Action, 2nd edition
subject: how to minimize heap space