v8.queryObjects(ctor[, options])
¥Stability: 1.1 - Active development
-
ctor
<Function> 该构造函数可用于在原型链上进行搜索,以过滤堆中的目标对象。¥
ctor
<Function> The constructor that can be used to search on the prototype chain in order to filter target objects in the heap. -
options
<undefined> | <Object> -
返回:{number|Array
} ¥Returns: {number|Array
}
这与 Chromium DevTools 控制台提供的 queryObjects()
控制台 API 类似。它可用于在完全垃圾回收后在堆中搜索其原型链上具有匹配构造函数的对象,这对于内存泄漏回归测试非常有用。为了避免出现意外的结果,用户应避免在其实现无法控制的构造函数上或可以由应用中其他方调用的构造函数上使用此 API。
¥This is similar to the queryObjects()
console API provided by the
Chromium DevTools console. It can be used to search for objects that
have the matching constructor on its prototype chain in the heap after
a full garbage collection, which can be useful for memory leak
regression tests. To avoid surprising results, users should avoid using
this API on constructors whose implementation they don't control, or on
constructors that can be invoked by other parties in the application.
为了避免意外泄漏,此 API 不会返回对找到的对象的原始引用。默认情况下,它返回找到的对象的计数。如果 options.format
是 'summary'
,则它返回一个包含每个对象的简短字符串表示形式的数组。该 API 提供的可见性类似于堆快照提供的可见性,同时用户可以节省序列化和解析的成本,并在搜索过程中直接过滤目标对象。
¥To avoid accidental leaks, this API does not return raw references to
the objects found. By default, it returns the count of the objects
found. If options.format
is 'summary'
, it returns an array
containing brief string representations for each object. The visibility
provided in this API is similar to what the heap snapshot provides,
while users can save the cost of serialization and parsing and directly
filter the target objects during the search.
结果中仅包含在当前执行上下文中创建的对象。
¥Only objects created in the current execution context are included in the results.
const { queryObjects } = require('node:v8');
class A { foo = 'bar'; }
console.log(queryObjects(A)); // 0
const a = new A();
console.log(queryObjects(A)); // 1
// [ "A { foo: 'bar' }" ]
console.log(queryObjects(A, { format: 'summary' }));
class B extends A { bar = 'qux'; }
const b = new B();
console.log(queryObjects(B)); // 1
// [ "B { foo: 'bar', bar: 'qux' }" ]
console.log(queryObjects(B, { format: 'summary' }));
// Note that, when there are child classes inheriting from a constructor,
// the constructor also shows up in the prototype chain of the child
// classes's prototype, so the child classes's prototype would also be
// included in the result.
console.log(queryObjects(A)); // 3
// [ "B { foo: 'bar', bar: 'qux' }", 'A {}', "A { foo: 'bar' }" ]
console.log(queryObjects(A, { format: 'summary' }));
import { queryObjects } from 'node:v8';
class A { foo = 'bar'; }
console.log(queryObjects(A)); // 0
const a = new A();
console.log(queryObjects(A)); // 1
// [ "A { foo: 'bar' }" ]
console.log(queryObjects(A, { format: 'summary' }));
class B extends A { bar = 'qux'; }
const b = new B();
console.log(queryObjects(B)); // 1
// [ "B { foo: 'bar', bar: 'qux' }" ]
console.log(queryObjects(B, { format: 'summary' }));
// Note that, when there are child classes inheriting from a constructor,
// the constructor also shows up in the prototype chain of the child
// classes's prototype, so the child classes's prototype would also be
// included in the result.
console.log(queryObjects(A)); // 3
// [ "B { foo: 'bar', bar: 'qux' }", 'A {}', "A { foo: 'bar' }" ]
console.log(queryObjects(A, { format: 'summary' }));