Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Buffer allocation in node.js

I want to use a buffer to store a sparse index. If I allocate a buffer of, say, 1024 bytes, and store effectively 128 bytes in it, how many bytes will be allocated in memory?

like image 471
Gaël Barbin Avatar asked Sep 24 '26 18:09

Gaël Barbin


1 Answers

According to the constructor doc:

new Buffer(size)#

size Number Allocates a new buffer of size bytes. size must be less than 1,073,741,824 bytes (1 GB) on 32 bits architectures or 2,147,483,648 bytes (2 GB) on 64 bits architectures, otherwise a RangeError is thrown.

Unlike ArrayBuffers, the underlying memory for buffers is not initialized. So the contents of a newly created Buffer are unknown and could contain sensitive data. Use buf.fill(0) to initialize a buffer to zeroes.

It seem that the whole memory is allocated, but not zeroified.

like image 162
Remy Grandin Avatar answered Sep 27 '26 12:09

Remy Grandin