On this page

    M

    vm.measureMemory

    History
    vm.measureMemory(options?): Promise
    Stability: 1Experimental

    Measure the memory known to V8 and used by all contexts known to the current V8 isolate, or the main context.

    Attributes
    options:Object
    Optional.
    mode?:string
    Either 'summary' or 'detailed'. In summary mode, only the memory measured for the main context will be returned. In detailed mode, the memory measured for all contexts known to the current V8 isolate will be returned. Default: 'summary'
    execution?:string
    Either 'default' or 'eager'. With default execution, the promise will not resolve until after the next scheduled garbage collection starts, which may take a while (or never if the program exits before the next GC). With eager execution, the GC will be started right away to measure the memory. Default: 'default'
    Returns:Promise
    If the memory is successfully measured, the promise will resolve with an object containing information about the memory usage. Otherwise it will be rejected with an ERR_CONTEXT_NOT_INITIALIZED error.

    The format of the object that the returned Promise may resolve with is specific to the V8 engine and may change from one version of V8 to the next.

    The returned result is different from the statistics returned by v8.getHeapSpaceStatistics() in that vm.measureMemory() measure the memory reachable by each V8 specific contexts in the current instance of the V8 engine, while the result of v8.getHeapSpaceStatistics() measure the memory occupied by each heap space in the current V8 instance.

    import { createContext, measureMemory } from 'node:vm';
    // Measure the memory used by the main context.
    measureMemory({ mode: 'summary' })
      // This is the same as vm.measureMemory()
      .then((result) => {
        // The current format is:
        // {
        //   total: { jsMemoryEstimate: 1601828, jsMemoryRange: [1601828, 5275288] },
        //   WebAssembly: { code: 0, metadata: 33962 },
        // }
        console.log(result);
      });
    
    const context = createContext({ a: 1 });
    measureMemory({ mode: 'detailed', execution: 'eager' }).then((result) => {
      // Reference the context here so that it won't be GC'ed
      // until the measurement is complete.
      console.log('Context:', context.a);
      // {
      //   total: { jsMemoryEstimate: 1767100, jsMemoryRange: [1767100, 5440560] },
      //   WebAssembly: { code: 0, metadata: 33962 },
      //   current: { jsMemoryEstimate: 1601828, jsMemoryRange: [1601828, 5275288] },
      //   other: [{ jsMemoryEstimate: 165272, jsMemoryRange: [Array] }],
      // }
      console.log(result);
    });
    const { createContext, measureMemory } = require('node:vm');
    // Measure the memory used by the main context.
    measureMemory({ mode: 'summary' })
      // This is the same as vm.measureMemory()
      .then((result) => {
        // The current format is:
        // {
        //   total: { jsMemoryEstimate: 1601828, jsMemoryRange: [1601828, 5275288] },
        //   WebAssembly: { code: 0, metadata: 33962 },
        // }
        console.log(result);
      });
    
    const context = createContext({ a: 1 });
    measureMemory({ mode: 'detailed', execution: 'eager' }).then((result) => {
      // Reference the context here so that it won't be GC'ed
      // until the measurement is complete.
      console.log('Context:', context.a);
      // {
      //   total: { jsMemoryEstimate: 1767100, jsMemoryRange: [1767100, 5440560] },
      //   WebAssembly: { code: 0, metadata: 33962 },
      //   current: { jsMemoryEstimate: 1601828, jsMemoryRange: [1601828, 5275288] },
      //   other: [{ jsMemoryEstimate: 165272, jsMemoryRange: [Array] }],
      // }
      console.log(result);
    });