Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7183011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:53:32+00:00 2026-05-28T17:53:32+00:00

Good afternoon all, I’m using a java.lang.StringBuilder to store some characters. I have no

  • 0

Good afternoon all, I’m using a java.lang.StringBuilder to store some characters. I have no idea how many characters I’m going to store in advance, except that:

  1. 60% of the time, it is only (exactly) 7 characters
  2. 39% of the time, it is (roughly) 3500 characters
  3. 1% of the time, it is roughly 20k characters

How do we go about calculating the optimal initial buffer length that should be used?

Currently I’m using new java.lang.StringBuilder(4000) but that’s just because I was too lazy to think previously.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T17:53:33+00:00Added an answer on May 28, 2026 at 5:53 pm

    There are two factors here: time and memory consumption. The time is mostly influenced by the number of times java.lang.AbstractStringBuilder.expandCapacity() is called. Of course the cost of each call is linear to the current size of the buffer, but I am simplifying here and just counting them:

    Number of expandCapacity() (time)

    Default configuration (16 characters capacity)

    • In 60% of the cases the StringBuilder will expand 0 times
    • In 39% of the cases the StringBuilder will expand 8 times
    • In 1% of the cases the StringBuilder will expand 11 times

    The expected number of expandCapacity is 3,23.

    Initial capacity of 4096 characters

    • In 99% of the cases the StringBuilder will expand 0 times
    • In 1% of the cases the StringBuilder will expand 3 times

    The expected number of expandCapacity is 0,03.

    As you can see the second scenario seems much faster, as it very rarely has to expand the StringBuilder (three time per every 100 inputs). Note however that first expands are less significant (copying small amount of memory); also if you add strings to the builder in huge chunks, it will expand more eagerly in less iterations.

    On the other hand the memory consumption grows:

    Memory consumption

    Default configuration (16 characters capacity)

    • In 60% of the cases the StringBuilder will occupy 16 characters
    • In 39% of the cases the StringBuilder will occupy 4K characters
    • In 1% of the cases the StringBuilder will occupy 32K characters

    The expected average memory consumption is: 1935 characters.

    Initial capacity of 4096 characters

    • In 99% of the cases the StringBuilder will occupy 4K characters
    • In 1% of the cases the StringBuilder will occupy 32K characters

    The expected average memory consumption is: 4383 characters.


    TL;DR

    This makes me believe that enlarging the initial buffer to 4K will increase the memory consumption by more than two times while speeding up the program by two orders of magnitude.

    The bottom line is: try! It is not that hard to write a benchmark that will process million strings of various length with different initial capacity. But I believe a bigger buffer might be a good choice.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good afternoon to all, I have this scenario: I am using SQL Server 'BulkInsert'
Good Afternoon All, I have two tables in my SQL Server 2005 DB, Main
Good afternoon, I have a web query in Excel 2002 going against a web
Good afternoon all. I am aware that if we close() an java.io.OutputStream , it
Good afternoon all, I have a class which looks like this: public class Grapheme
Good Afternoon All, I have a wizard control that contains 20 textboxes for part
Good afternoon all. I'm going to post the stored procedure in it's entire glory.
Good Afternoon All, I have written an SSIS 2005 package that contains a conditional
afternoon all. Iv'e come across some mathematical problems that im not too good at.
Good afternoon all Here is my scenario: I have user controls within a master

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.