diff --git a/src/base_object.cc b/src/base_object.cc index e93ca47341d..abef21b35b7 100644 --- a/src/base_object.cc +++ b/src/base_object.cc @@ -174,9 +174,15 @@ void BaseObjectList::Cleanup() { } } -void BaseObjectList::MemoryInfo(node::MemoryTracker* tracker) const { +void BaseObjectList::MemoryInfo(MemoryTracker* tracker) const { for (auto bo : *this) { - if (bo->IsDoneInitializing()) tracker->Track(bo); + if (bo->IsDoneInitializing()) { + // TODO(addaleax): Add weak edges instead of no edges once + // https://github.com/v8/v8/commit/e37cadf1143a8c5bbe44c0408186b5a26cc23863 + // is available for us + tracker->Track( + bo, bo->persistent().IsWeak() ? MemoryTracker::kWeakEdge : nullptr); + } } } diff --git a/src/cppgc_helpers.cc b/src/cppgc_helpers.cc index 4e3ffb678c2..7c557a822d2 100644 --- a/src/cppgc_helpers.cc +++ b/src/cppgc_helpers.cc @@ -16,7 +16,10 @@ void CppgcWrapperList::MemoryInfo(MemoryTracker* tracker) const { for (auto node : *this) { CppgcMixin* ptr = node->persistent.Get(); if (ptr != nullptr) { - tracker->Track(ptr); + // TODO(addaleax): Add weak edges instead of no edges once + // https://github.com/v8/v8/commit/e37cadf1143a8c5bbe44c0408186b5a26cc23863 + // is available for us + tracker->Track(ptr, MemoryTracker::kWeakEdge); } } } diff --git a/test/common/heap.js b/test/common/heap.js index ac7e090326c..6c83ca99782 100644 --- a/test/common/heap.js +++ b/test/common/heap.js @@ -336,6 +336,19 @@ function validateByRetainingPath(...args) { return validateByRetainingPathFromNodes(nodes, ...args); } +function getRetainingNodes(startingNode, filter) { + const seen = new Set(); + function listNodes(node) { + if (!filter(node) || seen.has(node)) return; + seen.add(node); + for (const edge of node.incomingEdges) { + listNodes(edge.from); + } + } + listNodes(startingNode); + return [...seen]; +} + module.exports = { recordState, validateSnapshotNodes, @@ -343,4 +356,5 @@ module.exports = { validateByRetainingPathFromNodes, getHeapSnapshotOptionTests, createJSHeapSnapshot, + getRetainingNodes, }; diff --git a/test/pummel/test-heapdump-zlib.js b/test/pummel/test-heapdump-zlib.js index 9f11fbcadad..f6ca5ceaab8 100644 --- a/test/pummel/test-heapdump-zlib.js +++ b/test/pummel/test-heapdump-zlib.js @@ -3,7 +3,7 @@ const common = require('../common'); const assert = require('assert'); -const { validateByRetainingPath, validateByRetainingPathFromNodes } = require('../common/heap'); +const { validateByRetainingPath, validateByRetainingPathFromNodes, getRetainingNodes } = require('../common/heap'); const zlib = require('zlib'); // Before zlib stream is created, no ZlibStream should be created. @@ -27,6 +27,18 @@ const gzip = zlib.createGzip(); assert.strictEqual(withMemory.length, 0); } +{ + // Assert that the `ZlibStream` has no unexpected connections + // to other `Node / ...` nodes. + const contexts = validateByRetainingPath('Node / ZlibContext', []); + assert.deepStrictEqual( + getRetainingNodes(contexts[0], (node) => node.name?.startsWith('Node /')) + .map((node) => node.name).sort(), [ + 'Node / ZlibContext', + 'Node / ZlibStream', + ]); +} + // After zlib stream is written, zlib_memory should be created. gzip.write('hello world', common.mustCall(() => { validateByRetainingPath('Node / ZlibStream', [