-
Notifications
You must be signed in to change notification settings - Fork 0
/
app2.js
85 lines (71 loc) · 2.06 KB
/
app2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// a trivial process that does nothing except
// trigger GC and output the present base memory
// usage every second. this example is intended to
// demonstrate that memwatch itself does not leak.
var http = require('http');
var heapdump = require('heapdump');
var _ = require('underscore');
var start = new Date();
function msFromStart() {
return new Date() - start;
}
var leak = [];
// every second, this program "leaks" a little bit
setInterval(function() {
if (global && global.gc){
global.gc()
}
for (var i = 0; i < 10; i++) {
//var str = i.toString() + " on a stick, short and stout!";
//var str = new Array(1000).fill("hey").join(" ");
// var str = _.reduce(_.range(100000),(acc,i) => { return _.extend(acc, {[i]:i})},{});
var str = _.range(100000);
var fn = makeFn(str);
leak.push(fn);
}
}, 1000);
function makeFn(what){
return function(){
return what;
}
}
// meantime, the program is busy, doing *lots* of http requests
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
function doHTTPRequest() {
var options = {
host: '127.0.0.1',
port: 1337,
path: '/index.html'
};
http.get(options, function(res) {
setTimeout(doHTTPRequest, 300);
}).on('error', function(e) {
console.info(e)
setTimeout(doHTTPRequest, 300);
});
}
doHTTPRequest();
doHTTPRequest();
var memwatch = require('memwatch-next');;
// report to console postgc heap size
memwatch.on('stats', function(d) {
console.log("postgc:", msFromStart(), d.current_base);
});
// memwatch.on('leak', function(d) {
// console.log("LEAK:", d);
// });
memwatch.on('leak', (info) => {
console.error('Memory leak detected:\n', info);
heapdump.writeSnapshot((err, filename) => {
if (err) console.error(err);
else console.error('Wrote snapshot: ' + filename);
})
});
// also report periodic heap size (every 10s)
setInterval(function() {
console.log("naive:", msFromStart(), process.memoryUsage().heapUsed);
}, 5000);