MaxCapacity of StringBuilder

后端 未结 2 1263
陌清茗
陌清茗 2021-02-10 20:23

why does this code throw an OutOfMemoryException on i = 690864192?

StringBuilder sb = new StringBuilder();
                for (int i = 0; i < Int32.MaxValue;         


        
2条回答
  •  有刺的猬
    2021-02-10 21:09

    Each time the StringBuilder allocates a new char[] (by pointing to a previous chunk instance.. which is another StringBuilder).. you're putting an insane amount of pressure on the garbage collector.

    There are memory restrictions on the size of objects. Your object will likely max out your assigned memory for your applications process before reaching the end... hence OutOfMemoryException in either case.

    The last StringBuilder instance you have points to the last instance before it maxed out.. that one also points to the previous instance before it maxed out... etc. You have a gigantic graph of roots for the GC that never get cleaned up.

提交回复
热议问题