0

I read somewhere that the Java StringBuilder uses around 1 mb for 500 characters. Is this true and if so, isn't that a bit extreme? Is the StringBuilder doing some incredible things with this amount of memory? What is the reason and does this mean I should not make too much use of this class?

asked Nov 10, 2009 at 11:38
2
  • 3
    Where did you read this? Commented Nov 10, 2009 at 11:40
  • Somewhere on the Internet, of course. Commented Nov 10, 2009 at 14:34

5 Answers 5

14

No, that's complete rubbish - unless you create a StringBuilder with a mammoth capacity, of course.

Java in general uses 2 bytes per char. There's a little bit of overhead in String and StringBuilder for the length and the array itself, but not a lot.

Now 1K for 500 characters is about right... I suspect that was the cause of confusion. (Either you misheard, or the person talking to you was repeating something they'd misheard.)

answered Nov 10, 2009 at 11:41
Sign up to request clarification or add additional context in comments.

Comments

2

I have seen two cases where StringBuilder's tend to use large amounts of memory:

  • When the StringBuilder is created with an insane initial-capacity.
  • StringBuilder's who were "cached" to "save" object-allocation time.

So in the second case a StringBuilder might consume 1Mb of memory if some code, which used the SB earlier, stored a very big string in it. That's because it will only grow but not shrink its internal char-array.

Both cases can (and should) easy be avoided.

answered Nov 10, 2009 at 12:26

Comments

0

This information is erroneous, do you remember what the source of this information was? If so you should correct it. Java normally uses 2 bytes per character.

answered Nov 10, 2009 at 11:46

Comments

0

Because of the doubling reallocation 2K for 500 characters would also be right, but not more. Here is a similar question.

answered Nov 10, 2009 at 11:48

Comments

0

I think StringBuilder is the best choice to use. It is faster and safer too. It depends on the scenario. If you have String literal that doesn't change frequently then I would say String is a better choice because it is immutable else StringBuilder is right there. Now for the space that you are talking about I haven't heard that any where.

answered May 5, 2017 at 17:42

2 Comments

Faster than what? Safer? Be aware that StringBuilder is not thread safe, in contrast to StringBuffer. BTW this question is almost 8 years old...
Faster than StringBuffer.Anythng else you need to understand.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.