{"_id":"@biiaidt/node-memwatch","_rev":"2-84feee27f62b77a0ba6d5e5ea2032b6d","name":"@biiaidt/node-memwatch","dist-tags":{"latest":"2.0.1"},"versions":{"2.0.1":{"name":"@biiaidt/node-memwatch","version":"2.0.1","author":{"name":"biiaidt"},"_id":"@biiaidt/node-memwatch@2.0.1","maintainers":[{"name":"biiaidt","email":"biiaidt@126.com"}],"contributors":[{"name":"biiaidt"}],"homepage":"https://github.com/biiaidt/node-memwatch#readme","bugs":{"url":"https://github.com/biiaidt/node-memwatch/issues"},"dist":{"shasum":"f8160e789c2a266dcc492e2d14937ae6217807dc","tarball":"https://registry.npmjs.org/@biiaidt/node-memwatch/-/node-memwatch-2.0.1.tgz","fileCount":18,"integrity":"sha512-+Wfg0Lec09Tra0YeggCJLmkN2eEnFMRRKT3leNiO5JaiHCBSgO47VOwe/+mBP5JZbfZwUlgbjsX7T6jemtmHSw==","signatures":[{"sig":"MEQCIEALiF7xJwuox0N1tXnX9fB+QU2DCBMhT2d1se/ZLRAvAiBqytuv31BY3GC5d3uty7O/u4fux+/lz+rujgDAS0MsZw==","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":34266},"main":"include.js","engines":{"node":">= 10.0"},"gitHead":"b2a63cae9ac83eeba7b88732a3dc24178b87e490","gypfile":true,"scripts":{"test":"mocha tests --reporter spec","install":"node-gyp rebuild"},"_npmUser":{"name":"biiaidt","email":"biiaidt@126.com"},"licenses":[{"type":"wtfpl"}],"repository":{"url":"git+https://github.com/biiaidt/node-memwatch.git","type":"git"},"_npmVersion":"6.9.0","description":"Keep an eye on your memory usage, and discover and isolate leaks. Support windows.","directories":{},"_nodeVersion":"12.4.0","dependencies":{"nan":"^2.14.1","bindings":"^1.5.0"},"publishConfig":{"access":"public"},"_hasShrinkwrap":false,"devDependencies":{"mocha":"^2.4.5","should":"^8.3.1"},"_npmOperationalInternal":{"tmp":"tmp/node-memwatch_2.0.1_1716884153191_0.2142122863149749","host":"s3://npm-registry-packages"}}},"time":{"created":"2024-05-28T08:15:53.070Z","modified":"2025-03-22T10:26:53.734Z","2.0.1":"2024-05-28T08:15:53.328Z"},"bugs":{"url":"https://github.com/biiaidt/node-memwatch/issues"},"author":{"name":"biiaidt"},"homepage":"https://github.com/biiaidt/node-memwatch#readme","repository":{"url":"git+https://github.com/biiaidt/node-memwatch.git","type":"git"},"description":"Keep an eye on your memory usage, and discover and isolate leaks. Support windows.","contributors":[{"name":"biiaidt"}],"maintainers":[{"email":"biiaidt@126.com","name":"biiaidt_m"}],"readme":"`node-memwatch`: Leak Detection and Heap Diffing for Node.JS\r\n============================================================\r\n\r\n`node-memwatch` is here to help you detect and find memory leaks in\r\nNode.JS code.  It provides:\r\n\r\n- A `stats` event, emitted on full MarkSweepCompact GCs giving you\r\n  data describing your heap usage and trends over time.\r\n\r\n- A `HeapDiff` class that lets you compare the state of your heap between\r\n  two points in time, telling you what has been allocated, and what\r\n  has been released.\r\n\r\n\r\nInstallation\r\n------------\r\n\r\n- `npm install @airbnb/node-memwatch`\r\n\r\n\r\nDescription\r\n-----------\r\n\r\nThere are a growing number of tools for debugging and profiling memory\r\nusage in Node.JS applications, but there is still a need for a\r\nplatform-independent native module that requires no special\r\ninstrumentation.  This module attempts to satisfy that need.\r\n\r\nTo get started, import `node-memwatch` like so:\r\n\r\n```javascript\r\nvar memwatch = require('@airbnb/node-memwatch');\r\n```\r\n\r\n### Leak Detection\r\n\r\nCurrently unsupported while we explore heuristics\r\n\r\n### Heap Usage\r\n\r\nThe best way to evaluate your memory footprint is to look at heap\r\nusage right after V8 performs garbage collection.  `memwatch` does\r\nexactly this - it checks heap usage only after GC to give you a stable\r\nbaseline of your actual memory usage.\r\n\r\nWhen V8 performs a garbage collection (technically, we're talking\r\nabout a full GC with heap compaction), `memwatch` will emit a `stats`\r\nevent.\r\n\r\n```javascript\r\nmemwatch.on('stats', function(stats) { ... });\r\n```\r\n\r\nThe `stats` data will look something like this:\r\n\r\n```javascript\r\n{\r\n  gcScavengeCount: 1,\r\n  gcScavengeTime: 1100880, // ns\r\n  gcMarkSweepCompactCount: 2,\r\n  gcMarkSweepCompactTime: 21157231, // ns\r\n  gcIncrementalMarkingCount: 0,\r\n  gcIncrementalMarkingTime: 0, //ns\r\n  gcProcessWeakCallbacksCount: 0,\r\n  gcProcessWeakCallbacksTime: 0, // ns\r\n  total_heap_size: 16097280, // bytes\r\n  total_heap_size_executable: 3670016, // bytes\r\n  total_physical_size: 10741880, // bytes\r\n  total_available_size: 1487689928, // bytes\r\n  used_heap_size: 5691584, // bytes\r\n  heap_size_limit: 1501560832, // bytes\r\n  malloced_memory: 8192,\r\n  peak_malloced_memory: 1185464,\r\n  gc_time: 4587251 // ns\r\n}\r\n```\r\n\r\nV8 has its own idea of when it's best to perform a GC, and under a\r\nheavy load, it may defer this action for some time.  To aid in\r\nspeedier debugging, `memwatch` provides a `gc()` method to force V8 to\r\ndo a full GC and heap compaction.\r\n\r\n\r\n### Heap Diffing\r\n\r\nFor leak isolation, it provides a `HeapDiff` class that takes two snapshots and\r\ncomputes a diff between them.  For example:\r\n\r\n```javascript\r\n// Take first snapshot\r\nvar hd = new memwatch.HeapDiff();\r\n\r\n// do some things ...\r\n\r\n// Take the second snapshot and compute the diff\r\nvar diff = hd.end();\r\n```\r\n\r\nThe contents of `diff` will look something like:\r\n\r\n```javascript\r\n{\r\n  \"before\": { \"nodes\": 11625, \"size_bytes\": 1869904, \"size\": \"1.78 mb\" },\r\n  \"after\":  { \"nodes\": 21435, \"size_bytes\": 2119136, \"size\": \"2.02 mb\" },\r\n  \"change\": { \"size_bytes\": 249232, \"size\": \"243.39 kb\", \"freed_nodes\": 197,\r\n    \"allocated_nodes\": 10007,\r\n    \"details\": [\r\n      { \"what\": \"String\",\r\n        \"size_bytes\": -2120,  \"size\": \"-2.07 kb\",  \"+\": 3,    \"-\": 62\r\n      },\r\n      { \"what\": \"Array\",\r\n        \"size_bytes\": 66687,  \"size\": \"65.13 kb\",  \"+\": 4,    \"-\": 78\r\n      },\r\n      { \"what\": \"LeakingClass\",\r\n        \"size_bytes\": 239952, \"size\": \"234.33 kb\", \"+\": 9998, \"-\": 0\r\n      }\r\n    ]\r\n  }\r\n}\r\n```\r\n\r\nThe diff shows that during the sample period, the total number of\r\nallocated `String` and `Array` classes decreased, but `Leaking Class`\r\ngrew by 9998 allocations.  Hmmm.\r\n\r\nYou can use `HeapDiff` in your `on('stats')` callback; even though it\r\ntakes a memory snapshot, which triggers a V8 GC, it will not trigger\r\nthe `stats` event itself.  Because that would be silly.\r\n\r\n\r\nFuture Work\r\n-----------\r\n\r\nPlease see the Issues to share suggestions and contribute!\r\n\r\n\r\nLicense\r\n-------\r\n\r\nhttp://wtfpl.net\r\n","readmeFilename":"README.md"}