Maximum length of StringBuffer object

hi friendsI wann ask u one Question.How many characters can a StringBuffer object can contain?I want to string representation of 50000 records into StringBuffer ObjectBut im getting java.lang.OutOfMemoryError.can u solve my
[300 byte] By [vijaykhambea] at [2007-11-26 19:39:05]
# 1
It can posibly hold Integer.MAX_VALUE chars, 2^31-1. You could increase the VM's max memory using the -Xmx flag.
PhHeina at 2007-7-9 22:17:50 > top of Java-index,Java Essentials,Java Programming...
# 2
OP, there might be a better way to achieve your goal. Why would you want to put so much data into a String? Are you writing it out? etc.
zadoka at 2007-7-9 22:17:51 > top of Java-index,Java Essentials,Java Programming...
# 3
hi yes , i want to store it in stringbuffer object . do u have sol for this? regards vijay
vijaykhambea at 2007-7-9 22:17:51 > top of Java-index,Java Essentials,Java Programming...
# 4

> i want to store it in stringbuffer object .

That's probably not a good idea. You're running out of memory with your current settings, which suggests that storing 50,000 records in memory isn't a feasible option. What do you plan to do with this data (presumably you don't want to store it in a StringBuffer just to leave it there)?

~

yawmarka at 2007-7-9 22:17:51 > top of Java-index,Java Essentials,Java Programming...
# 5
StringBuffer sb=new StringBuffer("abc")here the new StringBuffer sb will be off size 3 since it was intialized to "abc"so if u want to have 500initialze the string in constructor with 500 spaces like " "i guess that would help you
Ashok-Georgea at 2007-7-9 22:17:51 > top of Java-index,Java Essentials,Java Programming...
# 6
> ...> do u have sol for this?> regards> vijayProb sol?
prometheuzza at 2007-7-9 22:17:51 > top of Java-index,Java Essentials,Java Programming...
# 7
Tell us what you are trying to acheive. As others have said, it's not likely that you want to load up the StrungBuffer and leave it there.If you are working on a file then you would be better off reading in a section at a time instead of just loading the whole thing.Ted.
ted_trippina at 2007-7-9 22:17:51 > top of Java-index,Java Essentials,Java Programming...
# 8

> so if u want to have 500

> initialze the string in constructor with 500 spaces like " "

> i guess that would help you

That's extremely poor advice.

If you can't help, don't hinder.

[url=http://www.catb.org/~esr/faqs/smart-questions.html#id274601]How To Answer Questions in a Helpful Way[/url]

~

yawmarka at 2007-7-9 22:17:51 > top of Java-index,Java Essentials,Java Programming...
# 9
Go get him yawmark!
ted_trippina at 2007-7-9 22:17:51 > top of Java-index,Java Essentials,Java Programming...
# 10

hi

After reading 50000 records i want to write it in xml or excel file.

At the same time i want this object should be available in session obj so that no need fire database query again . But i unable to store it in session object becuase of out of memory error . I have increased size of JVM to 1GB but still it cannot store this heavy StringBuffer object .

I stucked at this problem.

regards

vijay

vijaykhambea at 2007-7-9 22:17:51 > top of Java-index,Java Essentials,Java Programming...
# 11

> At the same time i want this object should be available in session obj so that

> no need fire database query again.

Aha. Then given the trade-off between memory usage and database access, you chose memory usage. But in this case I think you chose wrongly. It's pretty clear that you don't have enough memory to support that choice. Given that, I would drop the idea and rerun the database query.

However several other posters have wondered why you chose a StringBuffer object to store a large amount of data. I think it's a strange choice too. Perhaps a different choice would have required less memory. Would you like to post the code that produces that StringBuffer?

DrClapa at 2007-7-9 22:17:51 > top of Java-index,Java Essentials,Java Programming...
# 12

Hi

Out of memory issue is may be because of shortage in memory allocated for JRE

You can increase run time memory by using following command

java -Xms64m -Xmx512m classname

Also I dont think there is a limit for StringBuffer. But stroing such large objects StringBuffer is not a good idea, instead of that we have Vector,ArrayList,HashMap etc...

csatheesha at 2007-7-9 22:17:51 > top of Java-index,Java Essentials,Java Programming...
# 13

> Out of memory issue is may be because of shortage

> in memory allocated for JRE

Yep, you read it here first folks. 'Out of memory' may be because of a shortage of memory. Remember that now children. It could be important.

Today must be Tautology Wednesday.

> Also I dont think there is a limit for StringBuffer.

So read the thread and discover your mistake.

> But stroing such large objects StringBuffer is not a

> good idea, instead of that we have

> Vector,ArrayList,HashMap etc...

More nonsense. They will save memory over StringBuffer how?

ejpa at 2007-7-9 22:17:51 > top of Java-index,Java Essentials,Java Programming...
# 14

Thanks ejp.

I would like to add...

> Hi

> Out of memory issue is may be because of shortage

> in memory allocated for JRE

> You can increase run time memory by using following

> command

>

> java -Xms64m -Xmx512m classname

Wow, really? U think? Why didnt any of the other replies say that!! And why if the OP said 1Gb was not enough would you suggest 0.5Gb?

>

> Also I dont think there is a limit for StringBuffer.

> But stroing such large objects StringBuffer is not a

> good idea, instead of that we have

> Vector,ArrayList,HashMap etc...

Vector? Vector? You running JDK1.1?

Ted.

ted_trippina at 2007-7-9 22:17:52 > top of Java-index,Java Essentials,Java Programming...
# 15

I'm pretty sure the OP would also save a lot of memory if he wouldn't store an XML file of some data set in a StringBuffer, but would actually use some OO to create an object model. I'm quite convinced that an object storing two int values is much smaller than an XML entity storing two int values.

Object:

20 bytes + 2 * 32 bytes = 84 bytes.

XML:

<SomeObject>

<field name="first" value="12345678"/>

<field name="second" value="0000"/>

</someObject>

= about 100 chars = about 200 bytes.

Not handling data as data is usually fairly stupid IMO.

CeciNEstPasUnProgrammeura at 2007-7-21 17:39:48 > top of Java-index,Java Essentials,Java Programming...
# 16

Another point: the OP is storing large amounts of data (be it in a StringBuffer or in some other form) in session data. Couldn't it happen that the server will dehydrate that data (is that the verb?) into its own database anyway? Shouldn''t one be careful with solutions that cache large results in session data? What's the best practice?

DrLaszloJamfa at 2007-7-21 17:39:48 > top of Java-index,Java Essentials,Java Programming...
# 17
Maybe this will help: http://vtd-xml.sourceforge.net/
uncle_alicea at 2007-7-21 17:39:48 > top of Java-index,Java Essentials,Java Programming...
# 18

> Maybe this will help:

>

> http://vtd-xml.sourceforge.net/

That's the thing about XML. Just when you think you know what's out there,

someone looks at code you've sweated blood over and they say:

the new XYZ XML technology can do this better, and in one line of code.

DrLaszloJamfa at 2007-7-21 17:39:48 > top of Java-index,Java Essentials,Java Programming...