Why is Java's String memory usage said to be high?

前端 未结 3 1072
不思量自难忘°
不思量自难忘° 2021-02-05 14:14

On this blog post, it\'s said that the minimum memory usage of a String is:

8 * (int) ((((no chars) * 2) + 45) / 8) bytes.

So for the String \"Apple

3条回答
  •  庸人自扰
    2021-02-05 14:43

    Compared to a other data types that you have, it is definitely high. The other primitives use 32 bits,64 bits,etc.

    And given that String is immutable, every time you perform any operation on it, you end up creating a new String object, consuming even more memory.

提交回复
热议问题