[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Out of memory error with XML and Java ResultSet


stringbuffer out of memory
Brown, Jason B. wrote:

>One row of XML text is approximately 230 characters, including tag name
>and attribute specifiers. 
>  
>
So you have about 230 characters x 300,000 rows. 70,000,000 characters. 
In Java, a character
takes two bytes, so that is 140,000,000 bytes. If you are appending to a 
StringBuffer, it
doubles the size of the underlying array whenever it needs more; at 
worse case, if your
StringBuffer had a full array with 70,000,000 characters and you 
appended one more character,
then StringBuffer would allocate a new underlying array of 140,000,000 
characters.  So that
takes up 280,000,000 bytes.   *Plus* not forgetting that the previous 
array was still needed at the
same time as the new one, so at the time of expanding the capacity of 
the underlying array,
you needed 420,000,000 bytes!

And that apart from anything else going on in your program.

I suggest you:--
  1) Allocate the StringBuffer with the correct amount of size (e.g. 70 
million characters) 
to prevent the worst-case blowout and reallocation requirements; If you 
regularly have
to handle large StringBuffers, you should add it to your QA checklist to 
check that
all large StringBuffers that are appended to should have preallocated 
capacities on
initialization;
  2) Make sure your JVM has been allocated with enough heap (-Xms);
  3) Double check your code so that you make sure the StringBuffer is 
explicitly set to null
as soon as it is not needed (which may help garbage collection), and 
that any external
references to the StringBuffer free it as soon as needed.

Cheers
Rick Jelliffe





PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
 

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.


Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.