• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).

 
Nagaraj Shivaklara
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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..
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Nagaraj Shivaklara
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!!
 
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic