| Duplicate :   | |
| Relates :   | |
| Relates :   | 
With the current implementation, the following code can trigger OOM, even though there might still be enough room to grow.
            StringBuilder sb = new StringBuilder();
            sb.ensureCapacity(Integer.MAX_VALUE / 2);
            sb.ensureCapacity(Integer.MAX_VALUE / 2 + 1);
The exception reports:
Exception in thread "main" java.lang.OutOfMemoryError: Requested array size exceeds VM limit,
 which means we tried to allocate Integer.MAX_VALUE elements array.
| 
 |