-2

I'm new to backend with nodejs. most recently on a project I chose to unzip the zip file received from API in memory to work with it. But now i think about whether this is resource-saving. I would therefore like to ask the experienced nodejs developers, how long does the zip file stored in memory actually stay in memory? As long as the server is running or until the function ends?

I used the Zip-Tool adm-zip.

code-snippet

function getJsonFiles () {
    const zipReportsFile = await fetch(this.generateApiUrl({action:"get",project:project.name,compression:"zip",date:myDate}));
    
    const zipReportsFileBuffer = Buffer.from(await zipReportsFile.arrayBuffer());
    
    //reading zip archive in memory
    const zipContent = new AdmZip(zipReportsFileBuffer);
                
    const jsonFiles = zipContent.getEntries();

    return jsonFiles;
}

every answer is appreciate.

DWORD
  • 17
  • 4
  • When the function ends, there is no variable pointing to the zip file any more, therefore it can be removed by the next garbage collection process (which is triggered when more Javascript memory is needed). – Heiko Theißen Aug 14 '22 at 14:47

1 Answers1

1

The lifetime of a variable in memory depends of its scope. And the lifetime of a data in memory depends of the number of references to it being used in an "alive" scope (meaning that it has not reached end yet). It doesn't matter what you put in your variable, zip file data or anything else.

function getJsonFiles () {
    const zipReportsFile = await fetch(this.generateApiUrl({action:"get",project:project.name,compression:"zip",date:myDate}));

zipReportsFile : This variable has block scope, so it will be thrown away at the end of this function, unless you reference it elsewhere

    const zipReportsFileBuffer = Buffer.from(await zipReportsFile.arrayBuffer());

zipReportsFileBuffer : This variable will also be ended at the end of the function. A reference of the first variable is used in a promise closure, to memory will be freed when the promise is resolved. If for some reason the promise status cannot be resolved, there will be a memory leak, the memory will not be freed but your variable zipReportsFile will still be unreachable after the end of this function

    const zipContent = new AdmZip(zipReportsFileBuffer);

zipContent : This variable will also be ended at the end of the function along with the reference to zipReportsFileBuffer it holds

    const jsonFiles = zipContent.getEntries();

    return jsonFiles;
}

jsonFiles : This is the data you return so the lifetime depends what you do with the data, where you call the function. Also be careful, if the getEntries() (I don't know what this returns exactly) holds a reference to zipContent, and then maybe also a reference to zipReportsFileBuffer, then you will be returning all those references alive, and they will keep being accessible in memory through the returned object.

Peterrabbit
  • 2,166
  • 1
  • 3
  • 19