typescript – Why does buffers in Node.js only take memory when they are filled not when they are allocated
Sorry if this question my seem stupid , started learning about buffers and wanted to experiment with them in Node.js , Basically my question is , why doesnt the process doesnt take 5gb when i allocate it without filling since it will be all zeroed out to “0000 0000” , but when i fill the buffer with any value , it pumps up the RAM usage for the process to 5GB
import { Buffer } from "buffer";
// Allocating a buffer with size of 5GB
const buf = Buffer.alloc(5_000_000_000);
setInterval(() => {
// Just to wait to see whats happening in memory
// so we can monitor in task manager
// This code doesn't allocate the memory for the process
}, 5000);
import { Buffer } from "buffer";
// Allocating a buffer with size of 5GB
const buf = Buffer.alloc(5_000_000_000);
setInterval(() => {
// Just to wait to see whats happening in memory
// so we can monitor in task manager
// Filling Buffer
buf.fill(0x22);
// This code takes 5GB
}, 5000);
shouldnt the first one and the second one takes the same memory space since only the values inside the buffer change ? or am i missing something
Read more here: Source link