Luca Reghellin
Luca Reghellin

Reputation: 8113

Does node's readFile() use a single Buffer with all the file size allocated?

I'm quite new to Node and filesystem streams concerns. I wanted to now if the readFile function maybe reads the file stats, get the size and create a single Buffer with all the file size allocated. Or in other words: I know it loads the entire file, ok. But does it do it by internally splitting the file in more buffers or does it use only a single big Buffer? Depending on the method used, it has different memory usage/leaks implications.

Upvotes: 1

Views: 362

Answers (1)

Luca Reghellin
Luca Reghellin

Reputation: 8113

Found the answer here on chapter 9.3:

http://book.mixu.net/node/ch9.html

As expected, readFile uses 1 full Buffer. From the link above, this is the execution of readFile:

// Fully buffered access [100 Mb file] -> 1. [allocate 100 Mb buffer] -> 2. [read and return 100 Mb buffer]

So if you use readFile() your app wiil need exactly the memory for all the file size at once.

To break that memory into chunks, use read() or createReadStream()

Upvotes: 1

Related Questions