Maximum number of characters stringbuilder can accommodate

前端 未结 3 999
[愿得一人]
[愿得一人] 2021-02-20 17:54

I need to write 10,000 x 30,000 characters. will a single stringbuilder be able to acomodate all characters or should I think of an array of stringbuilders? I do not have access

相关标签:
3条回答
  • 2021-02-20 18:17

    The length is an int so it should hold up to 2GChar (4GB) assuming you have the memory. You are going to use "only" 600MB (300 million @ 2 bytes per character). Just be careful how many copies you end up making... i.e. toString().

    0 讨论(0)
  • 2021-02-20 18:26

    As long as the length of the character sequence contained in the string builder does not exceed the capacity, it is not necessary to allocate a new internal buffer. If the internal buffer overflows, it is automatically made larger. And it gives you the max number of chars this StringBuilder instance memory can hold at this time.

    0 讨论(0)
  • 2021-02-20 18:37

    What you need to worry about is the max heap size. It is not going to make any difference whether you use single or multiple StringBuilder objects.

    0 讨论(0)
提交回复
热议问题