简体   繁体   English

Node.js搜索内存泄漏

[英]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. 我的脚本正在杀死服务器RAM,我无法弄清楚我的方法有什么问题。

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. 基本上它从API获取消息,然后应该对它做一些事情。 It will execute itself as long as there is more data to fetch. 只要有更多数据要提取,它就会自行执行。 (as long as nextPageToken exists in response). (只要nextPageToken存在于响应中)。

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. 随着脚本的运行,buff / cache不断增加。

  • What is the buff/cache thing actually and how is it related to my Node script? 实际上什么是buff / cache,它与我的Node脚本有什么关系?
  • 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) (我甚至在脚本的末尾尝试了process.exit
  • How do I debug and monitor RAM usage from my Node.js script? 如何从Node.js脚本调试和监控RAM使用情况?

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); gmail.users.messages返回响应, resp.nextPageToken存在(我想),然后你调用getPages(params); again. 再次。 Can you put a console.log just before the getPages(params); 你可以在getPages(params);之前放一个console.log 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? 另外,为什么要将消息设置为null进入迭代? There is a redefinition of the variable. 有一个重新定义的变量。

You can use N|Solid (its free for development), you'll launch your app inside its wrapper. 你可以使用N | Solid(它可以免费开发),你可以在它的包装器中启动你的应用程序。 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) (我甚至在脚本的末尾尝试了process.exit)

There has been misunderstanding: 一直存在误解:

http://www.linuxatemyram.com/ http://www.linuxatemyram.com/

Don't Panic! 不要惊慌! Your ram is fine! 你的公羊很好!

What's going on? 这是怎么回事? Linux is borrowing unused memory for disk caching. Linux正在借用未使用的内存来进行磁盘缓存。 This makes it looks like you are low on memory, but you are not! 这使得你看起来内存不足,但你不是! Everything is fine! 一切顺利!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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