nodejs / nodejs/node

Running a process with DevTools debugger attached leaks memory

オープン
#39,063 コメント 4 件 リアクション 13 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

inspector memory
主要言語
JavaScript
スター
122k
フォーク
37.3k
平均マージ
4日 2時間
マージ済み PR(30日)
283

説明

While the Chromium DevTools debugger is attached, RSS memory usage of a Node process keeps increasing without being collected. Once the debugger is detached, the memory usage flattens, however the memory is never released. When the debugger is not attached to the process in its runtime, memory allocation occurs as expected. It is entirely possible that this might not be related to Node (v8, perhaps) - however, this issue might be a good place to start a discussion.

Inspecting htop on a Raspberry Pi 3B machine:

  • Process after running for ~24 hours without a debugger attached: stable at around ~180MB RSS:
    no debugger

  • The same process, after attaching the debugger for ~15 minutes (with no actual user debugging active nor any logs being displayed): RSS at around ~245MB and rising:
    debugger

  • After disconnecting the debugger (removing the connection, closing and re-opening the debugger window), RSS stabilizes at around ~260MB. After ~30 minutes of further activity, the RSS usage seems stable; however, the memory is not freed:
    after

After running valgrind for various amounts of time on a different Linux machine, there seems to be an excessive amount of v8_inspector::AsyncStackTrace::capture calls. Using the Disable async stack traces option in DevTools > Settings > Debugger seems to reduce the leak somewhat, however, this wasn't reproducible across machines. Furthermore, testing the same process on Windows 10 in the same environment leads to no memory leaks.

Attached are valgrind dumps for various tested nodejs versions. Note that each dump was created with the process running for differing amounts, hence the total memory leaked will not be as relevant.

valgrind.zip

What steps will reproduce the bug?
  1. Set up a Node process, possibly with asynchronous stack activity
  2. Attach a Chrome DevTools debugger instance
  3. Observe a memory leak as long as the debugger is attached
How often does it reproduce? Is there a required condition?
  • Only seems to reproduce on Linux, not Windows
  • Might be related to async stack traces
  • At first, seemed to be related to the mariadb NPM module - but upon further testing with the module inactive, the memory leak kept occuring
What is the expected behavior?

No memory leak should occur as the debugger is being used.

What do you see instead?

Depending on the process being run, upwards of hundreds of MB of RAM will leak in a couple of minutes as the debugger is attached.

Additional information

A similar type of memory leak seems to be present in versions prior to 14.0, however, in a much smaller scale, and occuring much more slowly. Reference: nodejs/node#28787, possibly nodejs/node#28420

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、Chrome DevTools を接続した状態で、Linux 上で記載された手順に従って leak を再現し、async stack traces を有効にした場合と無効にした場合の実行を比較します。提供された Valgrind ダンプと、過剰な v8_inspector::AsyncStackTrace::capture アクティビティを調査します。完了の条件は、デバッガーの使用によって持続的な RSS の増加や解放されないメモリが発生しなくなることです。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
javascript, nodejs
領域
performance, tooling
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
静か
明瞭さ
おおむね明確
初心者へのやさしさ
45/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。