メモリ リークの調査

メモリ リークを調査している場合に、オブジェクトがガベージコレクションされない理由を知りたいときは、%DebugTrackRetainingPath(object)を使用して、各 GC でオブジェクトの実際の保持パスを印刷できます。

これには、--allow-natives-syntax --track-retaining-pathランタイム フラグが必要で、リリース モードとデバッグ モードの両方で機能します。詳細については、CL の説明を参照してください。

次のtest.jsを考えてください。

function foo() {
const x = { bar: 'bar' };
%DebugTrackRetainingPath(x);
return () => { return x; }
}
const closure = foo();
gc();

例(デバッグ モードを使用するか、v8_enable_object_print = trueですべての詳しい情報を表示する)

$ out/x64.release/d8 --allow-natives-syntax --track-retaining-path --expose-gc test.js
#################################################
Retaining path for 0x245c59f0c1a1:

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Distance from root 6: 0x245c59f0c1a1 <Object map = 0x2d919f0d729>

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Distance from root 5: 0x245c59f0c169 <FixedArray[5]>

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Distance from root 4: 0x245c59f0c219 <JSFunction (sfi = 0x1fbb02e2d7f1)>

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Distance from root 3: 0x1fbb02e2d679 <FixedArray[5]>

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Distance from root 2: 0x245c59f0c139 <FixedArray[4]>

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Distance from root 1: 0x1fbb02e03d91 <FixedArray[279]>

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Root: (Isolate)
-------------------------------------------------

デバッガー サポート #

デバッガー セッション(例: gdb/lldb)中に、前述のフラグがプロセスに渡されている場合(つまり、--allow-natives-syntax --track-retaining-path)、print isolate->heap()->PrintRetainingPath(HeapObject*)を対象のオブジェクトに示すことができます。