Skip to content

Instantly share code, notes, and snippets.

@azylman
Forked from rgarcia/run.md
Last active August 29, 2015 13:57
Show Gist options
  • Save azylman/9378618 to your computer and use it in GitHub Desktop.
Save azylman/9378618 to your computer and use it in GitHub Desktop.
$ node server.js

output in terminal shows that req.foo is no longer sitting in the heap:

got request with a foo of length 10000000
hello world
got request with a foo of length 0
hello world
{ before:
   { nodes: 87105,
     time: Wed Mar 05 2014 15:04:07 GMT-0800 (PST),
     size_bytes: 7986496,
     size: '7.62 mb' },
  after:
   { nodes: 68490,
     time: Wed Mar 05 2014 15:04:13 GMT-0800 (PST),
     size_bytes: 7569112,
     size: '7.22 mb' },
  change:
   { size_bytes: -417384,
     size: '-407.6 kb',
     freed_nodes: 22053,
     allocated_nodes: 3438,
     details:
      [ [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object] ] } }
var _ = require('underscore');
var assert = require('assert');
var express = require('express');
var memwatch = require('memwatch');
var quest = require('quest');
var app = express()
var checkFoo = function (req) {
console.log('got request with a foo of length', (req.foo || []).length);
};
app.get("/foo", function(req, res, next) {
req.foo = _.range(10000000); // allocate a ton of space
checkFoo(req);
res.write("hello world");
res.end();
});
app.get("/no_foo", function(req, res, next) {
checkFoo(req);
res.write("hello world");
res.end();
});
var server = app.listen(5001);
var snapshot = new memwatch.HeapDiff();
var checkHeap = function() {
memwatch.gc();
var diff = snapshot.end();
console.log(diff);
};
quest('http://localhost:5001/foo', function(err, resp, body) {
assert.ifError(err);
console.log(body);
quest('http://localhost:5001/no_foo', function(err, resp, body) {
assert.ifError(err);
console.log(body);
setTimeout(checkHeap, 5000);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment