This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I need to create a customized xls report from DB data, after putting all the data in string buffer and ran the application, i am getting below exception:
JVMST109: Insufficient space in Javaheap to satisfy allocation request
Exception in thread "main" java.lang.OutOfMemoryError at java.lang.StringBuffer.append(StringBuffer.java(Compiled Code))
Can you please help me with this. Actually Data size is about 9MB (9234KB).
StringBuffers have a capacity, expressed as an int, which represents the number of characters in the buffer. So the max capacity should be Integer.MAX_VALUE (or 2 ^31 -1) . Each character will be a 16-bit unicode character, so 2 bytes per character will require a heap size of (2 ^31 -1) * 2 bytes which is about a 4 GB heap (excluding any other object in the heap).
Paul Sturrock wrote:StringBuffers have a capacity, expressed as an int, which represents the number of characters in the buffer. So the max capacity should be Integer.MAX_VALUE (or 2 ^31 -1) . Each character will be a 16-bit unicode character, so 2 bytes per character will require a heap size of (2 ^31 -1) * 2 bytes which is about a 4 GB heap (excluding any other object in the heap).
You mean to say a stringbuffer can hold upto 4GB of data? i am using java 1.4.2;
Joined: Dec 16, 2008
Joshua Barrett wrote:Hello Nagaraj
What API are you using to create the .xls report?
I really dont think storing all the data for a report within a StringBuffer is the most optimal way.
They way I do it is parse a result set into a LinkedList of objects that each represent a single row. This allows you to retrieve the desired information as well as perform calculations etc.
Hope this helps
I am using POI 1.1 ...
Actually i am using java 1.4.2 and the structure of excel is like it has 4 tabs and each tab contains different data. What i have done is created 4 stringbufferes and adding all the buffer objeccts to single buffer as xml type and then converting xml to xls..
In almost every case, when I see someone worrying about the maximum size of a StringBuffer (or array or List etc etc etc) because they are trying to hold the whole of a file in memory I know that they have a design problem. I could be wrong but this certainly looks to be the case here.
In the OP's boots I would re-factor the design so that very little needed to be held in memory at a time. This normally means having sequential data creation and sequential writing of this data.
Retired horse trader.
Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.