Nodejs | Chrome内存调试

发布于 2025-01-10 18:36:32 字数 1436 浏览 0 评论 0 原文

上下文:

我有一个 Node.js 应用程序,其内存似乎非常高,我不知道这是否是内存泄漏,因为它在一定时间后会减少,但有时在重负载下它会持续增加,需要花费很多时间更长的时间才能减少。

因此,浏览文章和几个视频后,我认为我必须堆快照并分析导致内存泄漏的原因。

步骤:

  1. 到目前为止,我已经在本地拍摄了 4 张快照来重现内存泄漏。

快照 1:800MB 快照2:1400MB 快照 3:1600MB 快照 4:2000+MB

  1. 当我将堆转储文件上传到 chrome 开发工具时,我看到了很多信息,但我不知道如何从那里继续。

请检查下面的屏幕截图,它说有构造函数 [array],其浅层大小为 687206列中的保留大小为 721414,因此当展开该构造函数时,我可以看到创建了 4097716 个构造函数(请参阅下面附加的第二个屏幕截图)。

snapshot4-截图2

snapshot4-screenshot1

问题

  1. 内部数组[]是什么意思?为什么会创建 4097716 ?

  2. 如何过滤掉我的应用程序创建的构造函数并向我显示该构造函数而不是某些系统/v8 引擎构造函数?

  3. 在同一张屏幕截图中,构造函数之一使用名为 tenantRequire 函数的全局变量,这是自定义全局函数,在某些地方内部使用,而不是普通的 Node.js require,我看到所有构造函数中的变量,如“Array”、“Object”。这是全局tenantRequire代码供参考。它只是用 trycatch 修补了 require 函数。这是否会导致内存泄漏?

  4. 请参阅屏幕截图 3,[string] 构造函数,其浅层大小为 270303848。当我展开时,它显示 Node.js 加载的模块。问题为什么要这么大? &为什么我的 lodash 模块在该字符串构造函数中重复?

输入图片此处描述

Context:

I have an Node.js application which memory seems to very high, I don't know if that is memory leak or not, because it is reduced after certain period time, but some times on heavy load it keeps on increasing takes much longer to get reduced.

So going through the articles and couple of videos, i figured that i have to heap snapshot and analyse what is causing the memory leak.

Steps:

  1. I have taken 4 snap shots as of now in my local to reproduce the memory leak.

Snapshot 1: 800MB
Snapshot 2: 1400MB
Snapshot 3: 1600MB
Snapshot 4: 2000+MB

  1. When i uploaded the heapdump files to chrome dev tools I see there a lot of information but i don't know how to proceed from there.

Please check below screenshot, it says there is constructor [array] which has 687206 as shallow Size & Retained Size is 721414 in the columns, so when expanded that constructor i can see there are 4097716 constructors created ( refer the second screenshot attached below ).

snapshot4-screenshot2

snapshot4- screenshot1

Question

  1. What does internal array [] means ? Why is there 4097716 created ?

  2. How can a filter out the constructor which created by my app and showing me that instead of some system/v8 engine constructor ?

  3. In the same screenshot one of the constructor uses global variable called tenantRequire function, this is custom global function which is being used internally in some places instead of normal Node.js require, I see the variable across all the constructor like "Array", "Object". This is that global tenantRequire code for reference. It is just patched require function with trycatch. Is this causing the memory leak somehow ?

  4. Refer screenshot 3, [string] constructor it has 270303848 as shallow size. When i expanded it shows modules loaded by Node.js. Question why is this taking that much size ? & Why is my lodash modules are repeated in that string constructor ?

enter image description here

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

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

发布评论

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

评论(1

っ左 2025-01-17 18:36:32

如果不太了解您的应用程序以及导致高内存使用率的操作,则很难判断可能出现的问题。您使用哪个工具来记录堆快照?您录制快照时的操作顺序是怎样的?您能否将此信息添加到您的问题中?

您用 node.js标记

了问题并显示了 Chrome DevTools。没关系。您完全可以拍摄 Node.js 应用程序的堆快照并在 Chrome DevTools 中对其进行分析。但由于 Node.js 和 Chrome 使用相同的 JS 引擎 (V8) 和相同的垃圾收集器 (Orinoco),因此对于阅读该问题的人来说可能会有点困惑。只是为了确保我理解正确:问题出在 Node.js 应用程序中,而不是浏览器应用程序中。您使用 Chrome 只是为了分析堆快照。正确的?

另外,您还写道,您拍摄快照是为了重现内存泄漏。这是不正确的。您执行了一些您认为会导致高内存使用率的操作,记录了堆快照,然后在 Chrome DevTools 中加载该快照以观察假定的内存泄漏。

首先跟踪,其次分析

每当您怀疑性能问题时,您应该首先使用跟踪来了解应用程序中的哪些功能有问题(即速度慢、创建大量必须被垃圾收集的对象) , ETC)。
然后,当您知道要关注哪些功能时,您可以分析它们。

尝试这些可视化工具

有一些工具可以帮助您跟踪/分析您的应用程序。看看 FlameScope (网络应用程序)和 node-clinic (一套工具)。还有 Perfetto,但我认为它适用于 Chrome 应用程序,而不是 Node.js 应用程序。

我还强烈推荐 V8 博客

Without knowing much about your app and the actions that cause the high memory usage, it's hard to tell what could be the issue. Which tool did you use to record the heap snapshot? What is the sequence of operations you did when you recorded the snapshot? Could you add this information to your question?

A couple of remarks

You tagged the question with node.js and showed Chrome DevTools. That's ok. You can totally take a heap snapshot of a Node.js application and analyze it in Chrome DevTools. But since both Node.js and Chrome use the same JS engine (V8) and the same garbage collector (Orinoco), it might be a bit confusing for someone who reads the question. Just to make sure I understand it correctly: the issue is in a Node.js app, not in a browser app. And you are using Chrome just to analyze the heap snapshot. Right?

Also, you wrote that you took the snapshots to reproduce the memory leak. That's not correct. You performed some action which you thought would cause a high memory usage, recorded a heap snapshot, and later loaded the snapshot in Chrome DevTools to observe the supposed memory leak.

Trace first, profile second

Every time you suspect a performance issue, you should first use tracing to understand which functions in your applications are problematic (i.e. slow, create a lot of objects that have to be garbage-collected, etc).
Then, when you know which functions to focus on, you can profile them.

Try these visual tools

There are a few tools that can help you with tracing/profiling your app. Have a look a FlameScope (a web app) and node-clinic (a suite of tools). There is also Perfetto, but I think it's for Chrome apps, not Node.js apps.

I also highly recommend the V8 blog.

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