-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathmemory_leak_checker.js
85 lines (70 loc) · 2.06 KB
/
memory_leak_checker.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
MemoryLeakChecker = {
uniq_id: (new Date()).getTime(),
checked: 1,
is_seen: [],
checkLeaks: function(obj) {
var self = MemoryLeakChecker
if(!obj || (typeof obj == 'function') || self.checked > 20000)
return ;
if((self._isArray(obj) || self._isObject(obj))) {
if(self._isArray(obj)) {
self._logTooBig(obj, obj.length)
for(var i=0; i < obj.length; i++) {
self._checkIfNeeded(obj[i])
}
}
else if(self._isObject(obj)) {
self._logTooBig(obj, self._keys(obj).length)
for(var key in obj) {
self._checkIfNeeded(obj[key])
}
}
}
},
_checkIfNeeded: function(obj) {
if(!obj)
return ;
var self = MemoryLeakChecker;
self.checked++
if((self._isArray(obj) || self._isObject(obj))) {
if(obj.__leaks_checked == self.uniq_id)
return ;
obj.__leaks_checked = self.uniq_id
setTimeout(self._partial(self.checkLeaks, obj), 5);
}
},
_logTooBig: function(obj, limit) {
if(limit > 200) {
console.log('Object too big, memory leak? [size: ' + limit + ']')
console.log(obj)
console.log('-------')
}
},
_keys: function(obj) {
var rval = [], prop
for(prop in obj)
rval.push(prop)
return rval
},
_isArray: function(obj) {
try {
return obj instanceof Array
}
catch(e) {
return false
}
},
_isObject: function(obj) {
return (typeof obj == 'object')
},
_partial: function(fn) {
var args = Array.prototype.slice.call(arguments)
args.shift()
return function() {
var new_args = Array.prototype.slice.call(arguments)
args = args.concat(new_args)
return fn.apply(window, args)
}
}
}
MemoryLeakChecker.checkLeaks(window);