Kunok Kunok - 1 year ago 59
Node.js Question

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;

params.pageToken = resp.nextPageToken;
} else {
// resolve end here...


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
    at the end of the script)

  • How do I debug and monitor RAM usage from my Node.js script?

Answer Source

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)

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download