├── .gitignore ├── index.js ├── binding.gyp ├── .travis.yml ├── src ├── gc-watch.js └── gc-watch.cc ├── README.md ├── package.json ├── test └── gc-watch.js └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | build 2 | 3 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = require('./src/gc-watch'); 2 | -------------------------------------------------------------------------------- /binding.gyp: -------------------------------------------------------------------------------- 1 | { 2 | "targets": [ 3 | { 4 | "target_name": "gc-watch-impl", 5 | "sources": [ "src/gc-watch.cc" ], 6 | "include_dirs": [ 7 | "", 6 | "repository": { 7 | "type": "git", 8 | "url": "https://github.com/sankha93/node-gc-watch.git" 9 | }, 10 | "main": "index.js", 11 | "license": "MIT", 12 | "bugs": "https://github.com/sankhs93/node-gc-memwatch.git", 13 | "dependencies": { 14 | "bindings": "^1.2.1", 15 | "nan": "^2.2.1" 16 | }, 17 | "gypfile": true, 18 | "scripts": { 19 | "install": "node-gyp rebuild", 20 | "test": "node_modules/.bin/mocha --expose-gc" 21 | }, 22 | "keywords": [ 23 | "gc", 24 | "garbage", 25 | "performance", 26 | "memory", 27 | "profile" 28 | ], 29 | "devDependencies": { 30 | "mocha": "^2.4.5", 31 | "should": "^8.3.1" 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /test/gc-watch.js: -------------------------------------------------------------------------------- 1 | var should = require('should'); 2 | var gcWatch = require('../src/gc-watch'); 3 | 4 | describe('the library', function() { 5 | it('should export a couple of functions', function() { 6 | should.exist(gcWatch.on); 7 | should.exist(gcWatch.once); 8 | should.exist(gcWatch.removeAllListeners); 9 | }); 10 | }); 11 | 12 | describe('setting event handlers', function() { 13 | it('raises beforeGC event', function() { 14 | var beforeGCTime; 15 | gcWatch.once('beforeGC', function() { 16 | beforeGCTime = Date.now(); 17 | }); 18 | var beforeGCFired = Date.now(); 19 | global.gc(); 20 | Date.now().should.be.above(beforeGCTime); 21 | beforeGCTime.should.be.aboveOrEqual(beforeGCFired); 22 | }); 23 | 24 | it('raises afterGC event', function() { 25 | var afterGCTime; 26 | gcWatch.once('afterGC', function() { 27 | afterGCTime = Date.now(); 28 | }); 29 | var beforeGCFired = Date.now(); 30 | global.gc(); 31 | Date.now().should.be.aboveOrEqual(afterGCTime); 32 | afterGCTime.should.be.above(beforeGCFired); 33 | }); 34 | }); 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Sankha Narayan Guria 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/gc-watch.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace v8; 4 | 5 | Nan::Persistent beforeGCCallback; 6 | Nan::Persistent afterGCCallback; 7 | 8 | NAN_GC_CALLBACK(beforeGC) { 9 | Nan::HandleScope scope; 10 | Nan::MakeCallback(Nan::GetCurrentContext()->Global(), Nan::New(beforeGCCallback), 0, NULL); 11 | } 12 | 13 | NAN_GC_CALLBACK(afterGC) { 14 | Nan::HandleScope scope; 15 | Nan::MakeCallback(Nan::GetCurrentContext()->Global(), Nan::New(afterGCCallback), 0, NULL); 16 | } 17 | 18 | NAN_METHOD(SetBeforeGCCallback) { 19 | if (info.Length() == 1 && info[0]->IsFunction()) { 20 | beforeGCCallback.Reset(info[0].As()); 21 | Nan::AddGCPrologueCallback(beforeGC, kGCTypeMarkSweepCompact); 22 | } 23 | } 24 | 25 | NAN_METHOD(SetAfterGCCallback) { 26 | if (info.Length() == 1 && info[0]->IsFunction()) { 27 | afterGCCallback.Reset(info[0].As()); 28 | Nan::AddGCEpilogueCallback(afterGC, kGCTypeMarkSweepCompact); 29 | } 30 | } 31 | 32 | NAN_MODULE_INIT(Init) { 33 | Local cb_beforeGC = Nan::GetFunction(Nan::New(SetBeforeGCCallback)).ToLocalChecked(); 34 | Nan::Set(target, Nan::New("beforeGC").ToLocalChecked(), cb_beforeGC); 35 | 36 | Local cb_afterGC = Nan::GetFunction(Nan::New(SetAfterGCCallback)).ToLocalChecked(); 37 | Nan::Set(target, Nan::New("afterGC").ToLocalChecked(), cb_afterGC); 38 | } 39 | 40 | NODE_MODULE(gc_watch_impl, Init) 41 | --------------------------------------------------------------------------------