内存

¥Memory

在本文档中,你可以了解如何调试内存相关问题。

¥In this document you can learn about how to debug memory related issues.

我的进程内存不足

¥My process runs out of memory

Node.js(JavaScript)是一种垃圾收集语言,因此通过保留器可能会出现内存泄漏。由于 Node.js 应用通常是多租户、业务关键且长期运行的,因此提供一种可访问且有效的查找内存泄漏的方法至关重要。

¥Node.js (JavaScript) is a garbage collected language, so having memory leaks is possible through retainers. As Node.js applications are usually multi-tenant, business critical, and long-running, providing an accessible and efficient way of finding a memory leak is essential.

症状

¥Symptoms

用户观察到内存使用量不断增加(可能很快或很慢,持续数天甚至数周),然后看到进程崩溃并由进程管理器重新启动。该过程可能比以前运行得慢,重新启动会导致某些请求失败(负载平衡器响应 502)。

¥The user observes continuously increasing memory usage (can be fast or slow, over days or even weeks) then sees the process crashing and restarting by the process manager. The process is maybe running slower than before and the restarts cause some requests to fail (load balancer responds with 502).

副作用

¥Side Effects

  • 由于内存耗尽,进程重新启动,请求被丢弃

    ¥Process restarts due to the memory exhaustion and requests are dropped on the floor

  • GC 活动增加会导致 CPU 使用率上升和响应时间变慢

    ¥Increased GC activity leads to higher CPU usage and slower response time

    • GC 阻塞事件循环导致速度缓慢

      ¥GC blocking the Event Loop causing slowness

  • 内存交换增加会减慢进程(GC 活动)

    ¥Increased memory swapping slows down the process (GC activity)

  • 可能没有足够的可用内存来获取堆快照

    ¥May not have enough available memory to get a Heap Snapshot

我的进程内存利用效率低下

¥My process utilizes memory inefficiently

症状

¥Symptoms

应用使用了意外的内存量和/或我们观察到垃圾收集器活动增加。

¥The application uses an unexpected amount of memory and/or we observe elevated garbage collector activity.

副作用

¥Side Effects

  • 页面错误数量增加

    ¥An elevated number of page faults

  • 更高的 GC 活动和 CPU 使用率

    ¥Higher GC activity and CPU usage

调试

¥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.