xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

如何定位和分析 Node.js 项目中的内存泄漏问题 All In One

如何定位和分析 Node.js 项目中的内存泄漏问题 All In One

How to locate and analyze memory leaks in Node.js projects

内存泄漏 / memory leaks

  • 内存管理
  • 垃圾回收
// GC


  1. Reference-counting garbage collection / 引用计数垃圾收集 ❌ (引用计数)
    ⚠️ 注意:现代浏览器不再使用引用计数来进行垃圾回收。

  2. Mark-and-sweep algorithm / 标记扫描算法 ✅ (标记清除)
    该算法将“不再需要对象”的定义简化为“无法访问对象”。

avaScript 引擎通常提供公开内存模型的标志。
例如,Node.js 提供了额外的选项和工具,这些选项和工具公开了用于配置和调试内存问题的底层 V8 机制
此配置在浏览器中可能不可用,对于网页(通过 HTTP 标头等)更是如此。

garbage collector / 垃圾收集器

# 使用 --max-old-space-size 标志, 增加可用堆内存的最大数量
$ node --max-old-space-size=6000 index.js

# 使用 --expose-gc --inspec 标志和 Chrome 调试器公开垃圾收集器以调试内存问题
$ node --expose-gc --inspect index.js

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_Management

https://nodejs.org/en/docs/guides/debugging-getting-started/

js 数据类型

  1. 值类型(),内存分配使用 ???
  2. 引用类型(指针),内存分配使用 (内存泄漏)

image

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures

https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop

Debugging

Most memory issues can be solved by determining how much space our specific type of objects take and what variables are preventing them from being garbage collected.
It can also help to know the allocation pattern of our program over time.

大多数内存问题可以通过确定我们特定类型的对象占用多少空间以及哪些变量阻止它们被垃圾收集来解决。
它还可以帮助了解我们的程序随时间的分配模式。

Heap Profiler / 堆分析器
Heap Snapshot / 堆快照
GC Traces / GC 跟踪

https://nodejs.org/en/docs/guides/diagnostics/memory/#debugging

(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!

refs

https://nodejs.org/en/docs/guides/diagnostics/memory/



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2023-03-13 23:11  xgqfrms  阅读(120)  评论(3编辑  收藏  举报