File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes What is the maximum size that can hold by a String buffer object? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "What is the maximum size that can hold by a String buffer object?" Watch "What is the maximum size that can hold by a String buffer object?" New topic
Author

What is the maximum size that can hold by a String buffer object?

Nagaraj Shivaklara
Ranch Hand

Joined: Dec 16, 2008
Posts: 78
Hi,

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))
at org.apache.log4j.helpers.PatternConverter.format(PatternConverter.java:79)
at org.apache.log4j.PatternLayout.format(PatternLayout.java:495)
............ etc

Can you please help me with this. Actually Data size is about 9MB (9234KB).

Thanks in advance
Nagaraj



Thanks n Regards,
Nagaraj S K
Joshua Barrett
Greenhorn

Joined: Sep 08, 2010
Posts: 27
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
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

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).


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Nagaraj Shivaklara
Ranch Hand

Joined: Dec 16, 2008
Posts: 78
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;

Nagaraj Shivaklara
Ranch Hand

Joined: Dec 16, 2008
Posts: 78
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


Hi,

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..
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11402
    
  16

Nagaraj Shivaklara wrote:You mean to say a stringbuffer can hold upto 4GB of data? i am using java 1.4.2;

There is no such thing as a 'stringBuffer' - unless it's a class YOU created. The class provided is called 'StringBuffer' - is that what you are referring to?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Nagaraj Shivaklara
Ranch Hand

Joined: Dec 16, 2008
Posts: 78
fred rosenberger wrote:
Nagaraj Shivaklara wrote:You mean to say a stringbuffer can hold upto 4GB of data? i am using java 1.4.2;

There is no such thing as a 'stringBuffer' - unless it's a class YOU created. The class provided is called 'StringBuffer' - is that what you are referring to?


Hi,

I am referring to a single StringBuffer Object!!
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

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.
 
jQuery in Action, 2nd edition
 
subject: What is the maximum size that can hold by a String buffer object?