I am dealing with a huge array,
It contains ~200,000 elements. Basically its a array of strings. Each string being ~50 characters in length. After looking around I found it would take 2 bytes for 1 character, i.e 100 bytes for 1 element.
therefore, the total memory allocation should add up to 200,000 * 100 = ~20 MB
object-sizeof, js-sizeof, sizeof seems to implement same logic.
But consider this snippet,
process.memoryUsage();
const paths = getAllFilePaths();
process.memoryUsage();
Output before getting array,
external:25080
heapTotal:31178752
heapUsed:10427896 //10 MB
rss:51761152
Output after getting array,
external:16888
heapTotal:173539328
heapUsed:134720896 //134 MB
rss:204070912
This is ~124MB addition to heapUsed.
Implementation of getAllFilePaths():
const getAllFilePaths = function (_path, paths = []) {
fs.readdirSync(_path).forEach(name => {
const stat = fs.lstatSync(joinPath(_path, name))
if (stat.isDirectory()) {
getAllFilePaths(joinPath(_path, name), paths);
return;
}
paths.push(joinPath(_path, name));
});
return paths;
};
Why is so much memory being used ? Is this the desired behaviour or somehow getAllFilePaths() function could possibly be leaking memory ?
getArray()?getArray()is a recursive function that traverses a directory path and returns all the nested filepaths.