StringBuffer memory limit

R

rounner

Hello,

What is the limitation on the StringBuffer size and what memory model
does it use (does it just use as much Java memory heap as it can)?
Is it appropriate for large (up to a few Mb of data) strings or should
I use a different object?

Thank you.
 
E

EJP

Hello,

What is the limitation on the StringBuffer size and what memory model
does it use (does it just use as much Java memory heap as it can)?
Is it appropriate for large (up to a few Mb of data) strings or should
I use a different object?

What exactly are the contents of a large (up to a few Mb of data)
string? I've never seen a *real* string this long, but I've seen a lot
of programming where data was concatenated into strings that wasn't
really string data at all, and 90% of the time it should have been
processed immediately, not just concatenated. Surely you should be using
some other technique?
 
R

Remon van Vliet

Hello,

What is the limitation on the StringBuffer size and what memory model
does it use (does it just use as much Java memory heap as it can)?
Is it appropriate for large (up to a few Mb of data) strings or should
I use a different object?

Thank you.

It can be as large as you want within the constraints of your heap and no
larger than Integer.MAX_VALUE. That said, i cant think of a single practical
application involving Strings where this should become an issue.

Remon van Vliet
 
O

Oliver Wong

What is the limitation on the StringBuffer size and what memory model
does it use (does it just use as much Java memory heap as it can)?
Is it appropriate for large (up to a few Mb of data) strings or should
I use a different object?

I wouldn't worry about a few megabytes. When you start getting near a
gigabyte, then I'd start to worry. But also note EJB's comment on using the
appropriate data structure for the problem you're trying to solve. Also note
that if something isn't specified in the API documentation, it's subject to
change from one implementation to the next.

- Oliver
 
B

Ben_

Unlike file allocation on a harddisk, an object uses contiguous heap. So,
even if the sum of individual heap spaces might be sufficient, you could run
int OutOfMemoryError due to fragmented heap.

I've seen OoME due to allocation of a 2-3 MB String in a JVM with 256 MB
heap where total heap free was nearly 50% but fragmented.

As to the memory model of StringBuffer, you'll want to download the source
of the JDK to see the details. I don't know to what extends implementation
varies from vendor to vendor, but I suppose it could.
 
D

Domagoj Klepac

What is the limitation on the StringBuffer size and what memory model
does it use (does it just use as much Java memory heap as it can)?

From my testing, it seems that it does. Though it breaks far before
the heap limit - I imagine that inserting a String in a middle of
StringBuffer involves copying both StringBuffer and String into new
char array etc.
Is it appropriate for large (up to a few Mb of data) strings or should
I use a different object?

Use streams if possible.

Domchi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,165
Latest member
JavierBrak
Top