0.1.3 • Published 12 months ago

simple-trace-malloc v0.1.3

Weekly downloads
-
License
MIT
Repository
github
Last release
12 months ago

npm version

simple-trace-malloc

simple-trace-malloc gives you a simple way to easily trace the allocated memory of a JS function.

It uses process.memoryUsage to get the metadata.

It is as simple as Python tracemalloc!

NOTE: NEEDS TO RUN WITH --expose-gc: node --expose-gc <file>.js

  • for browser environments: chrome | brave: --enable-precise-memory-info

However, you may turn it off from options with force_gc: 0.

const trace_malloc = require("simple-trace-malloc")

function make_array(size) {
  let arr = []
  for(let i = 0; i < size; i++) 
    arr.push(i)
  return arr
} 

trace_malloc( () => {
  make_array(1000000)
}, { verbose: true, unit: 'MB' })

// This will print out something like:
{
  rss: '33.593 MB',
  heapTotal: '44.382 MB',
  heapUsed: '28.986 MB', // memory actually used by your JS function
  external: '0 MB',
  arrayBuffers: '0 MB',
  time: 16 // measured time to run make_array
}

// to get this object instead of using verbose for console.log
trace_malloc( () => {
  make_array(1000000)
}, { unit: 'MB' })
  .then(memoryUsage => console.log(memoryUsage))
// trace_malloc returns a Promise so you can use await as well :)

// to trace allocated memory of a function one after another, it is important to use await to avoid weird behavior
// for example

let memoryUsage = await trace_malloc( () => {
  make_array(1000000)
}, { unit: 'MB' })
// use memoryUsage

let memoryUsage1 = await trace_malloc( () => {
  make_array(1000000)
}, { unit: 'MB' })
// use memoryUsage1

// to look at the memory actually used by your function
// look at either heapTotal, headUsed

trace_malloc(func: Function | AsyncFunction, options: Object, ...args: any[]) -> Promise

Installation

npm install simple-trace-malloc --save-dev # NodeJS
npm install simple-trace-malloc-browser --save-dev # Browser
0.1.3

12 months ago

0.1.2

12 months ago