Stability: 1 - Experimental

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

    • 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 measure 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.

    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.

    1. const vm = require('vm');
    2. // Measure the memory used by the main context.
    3. vm.measureMemory({ mode: 'summary' })
    4. // This is the same as vm.measureMemory()
    5. .then((result) => {
    6. // The current format is:
    7. // {
    8. // total: {
    9. // jsMemoryEstimate: 2418479, jsMemoryRange: [ 2418479, 2745799 ]
    10. // }
    11. // }
    12. console.log(result);
    13. });
    14. const context = vm.createContext({ a: 1 });
    15. vm.measureMemory({ mode: 'detailed', execution: 'eager' })
    16. .then((result) => {
    17. // Reference the context here so that it won't be GC'ed
    18. // until the measurement is complete.
    19. console.log(context.a);
    20. // {
    21. // total: {
    22. // jsMemoryEstimate: 2574732,
    23. // jsMemoryRange: [ 2574732, 2904372 ]
    24. // },
    25. // current: {
    26. // jsMemoryEstimate: 2438996,
    27. // jsMemoryRange: [ 2438996, 2768636 ]
    28. // },
    29. // other: [
    30. // {
    31. // jsMemoryEstimate: 135736,
    32. // jsMemoryRange: [ 135736, 465376 ]
    33. // }
    34. // ]
    35. // }
    36. console.log(result);
    37. });