Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

StringBuffer memory limit

Thread view: 
rounner@yahoo.com - 03 Apr 2006 06:32 GMT
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.
EJP - 03 Apr 2006 07:13 GMT
> 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?
Remon van Vliet - 03 Apr 2006 16:02 GMT
> Hello,
>
[quoted text clipped - 4 lines]
>
> 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
Oliver Wong - 03 Apr 2006 17:31 GMT
> 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
rounner@yahoo.com - 03 Apr 2006 22:35 GMT
Ben_ - 04 Apr 2006 06:38 GMT
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.
Domagoj Klepac - 07 Apr 2006 21:29 GMT
>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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.