后端应用程序会消耗所有RAM,但没有发现内存泄漏

发布于 2025-02-05 01:03:21 字数 1133 浏览 1 评论 0原文

我最近部署了Nestjs Backend应用程序来渲染,我提到所有可用的RAM在我的服务后一段时间都消失了。

它可以在相当长的一段时间内正常工作,消耗不超过500MB的RAM,这是我期望看到的,但是随后发生了一些事情,内存开始泄漏。 top说我的节点过程使用1.4g内存:

    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND                                                              
     60 render    20   0 2883236   1.4g  56492 S   0.3   2.3  17:27.66 node

但是后来我使用 flag启动了 flag以制作堆快照,令人惊讶的是显示了堆尺寸只有65兆字节,而top表示1.4G

“在此处输入图像说明”

我也在VSCODE调试器扩展中看到:

我认为木偶可能是一个问题,所以我从顶部输出中杀死了每个铬的过程,它仅释放了大约80MB的RAM。这里有什么问题,为什么我的RAM使用率如此之高?

I recently deployed my NestJS backend application to Render and I mentioned that all available RAM is gone after some time for my service.

enter image description here

It can work fine for quite some time, consuming no more than 500mb of RAM which is what I expect to see, but then something happens and memory starts to leak. top says my node process is using 1.4g of memory:

    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND                                                              
     60 render    20   0 2883236   1.4g  56492 S   0.3   2.3  17:27.66 node

But then I started my application with --inspect flag to make a heap snapshot, and surprisingly it showed that heap size was only 65 megabytes while top says 1.4g:

enter image description here

That I also see in VSCode debugger extension:
enter image description here

I thought that puppeteer might be an issue, so I killed every single chrome processes by PID from top output, and it only freed around 80mb of RAM. What could be the issue here and why my RAM usage is so high?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执着的年纪 2025-02-12 01:03:21

我不知道您正在开发哪种类型的项目,但是最近我在上传文件的系统中也遇到了内存泄漏问题。

首先,要找出导致内存泄漏的原因,您将需要监视您的API中调用哪些请求,该请求引起了此问题,并调试并分析项目代码。

我注意到您正在使用nodejs - Inspect标志进行分析,尝试通过保留大小列进行降序,然后开始分析每个数组中的内容,可能有些数组或对象可以指向您项目中安装的某些库,这可能会导致内存泄漏供您执行调试。

我遇到的问题是在mongoose库中,在我的代码中的某个点上,有必要打开与数据库的连接,并且没有被关闭。也可能导致内存泄漏的是blob文件的,在我的情况下,我必须删除buffer还结束了使用blob的流的读/写操作。

我将无法告诉您您的项目中可能有什么问题,但是进行调试和代码分析确实是必要的。

I don't know what type of project you are developing, but recently I also had a memory leak problem in a system that uploads files.

First, to find out what is causing the memory leak, you will need to monitor which request is being called in your API that is causing this problem and debug and analyze your project's code.

I noticed that you are using the NodeJs --inspect flag to analyze, try to do a descending sort by the Retained Size column and start analyzing what is inside each array, probably there are arrays or objects that could point to some library installed in your project that may be causing a memory leak for you to perform debugging.

The problem I was having was with the Mongoose library, at one point in my code it was necessary to open the connection to the database and it was not being closed. What can also cause a memory leak is the Blob and Buffer of files, in my case I had to delete the Buffer when finishing an upload and also ending the read/write operation of a stream that used Blob.

I won't be able to tell you what your problem might be in your project, but it will really be necessary to carry out debugging and code analysis.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文