express.js api应用程序中的内存泄漏 [英] Memory leak in express.js api application

查看:178
本文介绍了express.js api应用程序中的内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一个express.js应用程序,该应用程序用作REST api。一个终结点启动puppeteer并通过几个过程测试我的网站。

I am running an express.js application, which is used as a REST api. One endpoint starts puppeteer and test my website with several procedures.

启动应用程序并不断使用终结点后,当您使用Docker时,我的Docker容器每小时都会耗尽内存可以在下面看到。

After starting the application and the continuous consumption of the endpoint, my docker container runs out of memory every hour as you can see below.

首先,我以为我的木偶/无头铬中有内存泄漏,但是随后我从进程中监视了内存使用情况,没有,可见的内存泄漏如下所示:

First, I thought I have a memory leak in my puppeteer / headless chrome, but I then I monitored the memory usage from the processes, there isn't and memory leak visible as you can see here:

     0.00 Mb COMMAND 
   384.67 Mb /var/express/node_modules/puppeteer/.local
   157.41 Mb node /var/express/bin/www 
   101.76 Mb node /usr/local/bin/pm2
     4.34 Mb /var/express/node_modules/puppeteer/.local
     1.06 Mb ps 
     0.65 Mb bash 
     0.65 Mb bash 
     0.31 Mb cut 
     0.31 Mb cut 
     0.13 Mb dumb

现在,我的想法不完整了。有谁知道RAM消耗的来源?

Now, I ran out of ideas what the problem could be. Has anyone an idea where the RAM consumption could came from?

推荐答案

更多地分析问题



您需要实时监视活动。

Analyse the problem more

You need to monitor the activity real time.

我们没有代码,因此我们甚至不知道发生了什么。但是,您可以使用更高级的工具,例如 htop gtop netdata 和除 top ps

We do not have the code, thus we cannot even know what is going on. However, you can use more advanced tool like htop, gtop, netdata and others than top or ps.

pm2 上的日志可能还会告诉您更多有关事情的信息。在这种情况下,日志将比流程管理器拥有更多的数据。确保彻底调查日志以查看脚本是否负责,并且是否抛出错误,

The log on pm2 might also tell you more about things. On such situation, the logs will have more data than the process manager. Make sure to thoroughly investigate the logs to see if scripts are responsible, and throwing errors or not,

pm2 logs



每次api调用都会使您花费



提前计算费用并进行相应的准备,

Each api call will cost you

Calculate the cost early and prepare accordingly,


  • 如果您有1个通话,则准备每次拥有100Mb-1GB或更多。就像浏览器选项卡一样,它将花费您。只要选项卡处于打开状态,成本就会存在。

  • 如果目标网站很繁重,那么它将花费更多。某些网站,例如Youtube,显然会花费更多。

  • 在浏览器选项卡中运行的任何脚本都将占用cpu和内存使用。

  • 说每个进程都是造成300MB的内存,如果您没有正确关闭进程并开始进行API调用,则只有10个API调用将能够轻松使用3GB的RAM。

  • If you have 1 call, then be prepared to have 100Mb-1GB or more each time. It will cost you just like a browser tab. The cost will be there as long as the tab is open.
  • If the target website is heavy, then it will cost more. Some websites like Youtube will obviously cost you more.
  • Any script running inside the browser tab will cost cpu and memory usage.
  • Say each process is causing 300MB ram, If you don't close the process properly and start making API calls, then only 10 API call will be able to use 3GB ram pretty easily. It can add up pretty quickly.

自动化任务是否成功,请确保正确使用 browser.close()以确保其使用的资源免费。

Whether the automation task is successful or not, make sure to properly use browser.close() to ensure the resource it is using gets free. Most of time we forget about such small things and it costs us.

类似于 dumb-init tini 可以使用,如果您有一个生成新进程的进程,并且您没有实现良好的信号处理程序来捕获子信号并阻止您的孩子,过程应停止,等等。

Something like dumb-init or tini can be used if you have a process that spawns new processes and you doesn't have good signal handlers implemented to catch child signals and stop your child if your process should be stopped etc.

有关此 SO答案

这篇关于express.js api应用程序中的内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆