问题:写入固定长度的字符串【write(byte b[])】,观察ByteArrayOutputStream中buf 的大小始终比字符串 Bytes的Size大很多,很是不解
分析发现:
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = buf.length;
int newCapacity = oldCapacity << 1;
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
buf = Arrays.copyOf(buf, newCapacity);
}
buf 的长度的增长不一定是按字符bytes长度增加的,而是按原有长度右移1位后增加的!
PS:导致原先500M的数据,在写入ByteArrayOutputStream后实际内存占用到800M左右...