I try extract data in chunks in order to reduce memory usage.The original file size is 2.24MBThe data size that I need to resolve is 0.2mbBut no matter what I do it seems like measuring the concatenated data is almost 2.2MB.
Here my code
function foo(url,sectionToWatchStart,sectionToWatchEnd ) { let isDataSectionFound = false; return new Promise((resolve, reject) => { let buffer= ""; https .get(url, function (response) { response.on("data", function (chunk) { if (chunk.includes(sectionToWatchStart)) isDataSectionFound = true; if (isDataSectionFound) { buffer += chunk; if (chunk.includes(sectionToWatchEnd)) { isDataSectionFound = false; } } }); response.on("end", function () { resolve(buffer); // the measure size of buffer variable is just 0.19MB }); }) .on("error", function (error) { console.error(error); reject(); }); });}
// Util Func to measure the size:const mesurMemory = async () => { const initialMemoryUsage = process.memoryUsage().heapUsed; const res = await foo(); const finalMemoryUsage = process.memoryUsage().heapUsed; const memoryUsageForCode = finalMemoryUsage - initialMemoryUsage; console.log(`Memory usage for code: ${memoryUsageForCode / (1024 * 1024)}MB`);};
So as I said , the file size is 2.24MB and the buffer size is just 0.19MB , why the measure func show usage os 2.2MB