简体   繁体   中英

Node.js search for memory leak

I am trying to get rid of memory leak but my understanding of things is pretty low in this area and I have nobody to ask for help expect you guys. My script is killing server RAM and I can't figure out what is wrong with my approach.

I have this function:

function getPages(params){
  gmail.users.messages.list(params, (err, resp)=>{

    for (var message of resp.messages) {
      message['ownerEmail'] = currentUser;
      getMessage(message); // this does something with it later
      var message = null;
    }

    if(resp.nextPageToken){
      params.pageToken = resp.nextPageToken;
      getPages(params);
    } else {
      // resolve end here...
    }

  })//gmail.users.messages.list
}//fetchPages
getPages(params);

Basically it gets messages from the API and should do something with it afterwards. It will execute itself as long as there is more data to fetch. (as long as nextPageToken exists in response).

Now I ran this command:

$ free -lm

              total        used        free      shared  buff/cache   available
Mem:          11935        1808        7643         401        2483        9368
Low:          11935        4291        7643
High:             0           0           0
Swap:          6062           0        6062

As script is running buff/cache is constantly increasing.

  • What is the buff/cache thing actually and how is it related to my Node script?
  • How do I manage what is buffered/cached and how do I kill/clear such stuff?
  • How do I optimize function above to forget everything that is already processed?
  • How do I make sure that script takes absolutely zero resources once it is finished? (I even tried process.exit at the end of the script)
  • How do I debug and monitor RAM usage from my Node.js script?

I don't think that there is a memory leak. I think that you are in an infinite loop with the recursion. The gmail.users.messages returns the response with the resp.nextPageToken being present (I suppose) and then you are calling the getPages(params); again. Can you put a console.log just before the getPages(params); function call? Something like that:

if (resp.nextPageToken) {
  params.pageToken = resp.nextPageToken;
  console.log('token', params.pageToken)
  getPages(params);
}

and check how many times do you print this and if you ever get out of the recursion. Also, why do you set the message to null into the iteration? There is a redefinition of the variable.

You can use N|Solid (its free for development), you'll launch your app inside its wrapper. Its quite easy to use and it allows you to make full profile where leak occurs. You can also do it manually with built in debugger, check memory consumption at each step.

Just to answer one of questions within the post:

How do I make sure that script takes absolutely zero resources once it is finished? (I even tried process.exit at the end of the script)

There has been misunderstanding:

http://www.linuxatemyram.com/

Don't Panic! Your ram is fine!

What's going on? Linux is borrowing unused memory for disk caching. This makes it looks like you are low on memory, but you are not! Everything is fine!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM