Forums Register Login

how to minimize heap space

+Pie Number of slices to send: Send
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?

Thanks
[ December 01, 2005: Message edited by: reubin yi ]
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
that's a great help to me, thanks stan.
money grubbing section goes here:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1398 times.
Similar Threads
Memory question
Interesting article about JVM performance
Memory question
Heap and Pool
confusion in heap and stack - PLEASE HELP
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 18:48:42.