I’m writing some browser code with a lot of large ArrayBuffers. I noticed the memory consumption is unexpectedly large and I’m not sure why that is. I’ve never worked with ArrayBuffers before and I don’t understand it too well. But my test code is short so I’ll include it here:
const data = new Uint8Array(
await get_new_data(output_data_size)
);
async function get_new_data(output_data_size) {
const output_byte_count = Math.ceil(1024*1024*output_data_size);
let results_buffer = new ArrayBuffer(output_byte_count);
return results_buffer;
}
It’s supposed to create an ArrayBuffer of size output_data_size megabytes, all elements initialised as zeroes, and that’s it. When running the code with output_data_size=128
it should create a 128 MB ArrayBuffer, but the web browser memory consumption increases by 5 GB each time I’ve tried. I can’t see any obvious places where memory leaks could happen. Maybe there is some maximum size for ArrayBuffers?