引见
memwatch是一个c++扩大,重要用来视察nodejs内存泄漏题目,基础用法以下:
const memwatch = require('@airbnb/memwatch');
function LeakingClass() {
}
memwatch.gc();
var arr = [];
var hd = new memwatch.HeapDiff();
for (var i = 0; i < 10000; i++) arr.push(new LeakingClass);
var hde = hd.end();
console.log(JSON.stringify(hde, null, 2));
完成剖析
剖析的版本为@airbnb/memwatch。首先从binding.gyp最先入手:
{
'targets': [
{
'target_name': 'memwatch',
'include_dirs': [
"<!(node -e \"require('nan')\")"
],
'sources': [
'src/heapdiff.cc',
'src/init.cc',
'src/memwatch.cc',
'src/util.cc'
]
}
]
}
这份设置示意其天生的目的是memwatch.node
,源码是src目次下的heapdiff.cc
、init.cc
、memwatch.cc
、util.cc
,在项目编译的过程当中还需要include分外的nan目次,nan目次经由过程实行node -e "require('nan')
根据node模块体系寻觅nan
依靠,<!
示意背面是一条指令。
memwatch的进口函数在init.cc
文件中,经由过程NODE_MODULE(memwatch, init);
举行声明。当实行require('@airbnb/memwatch')
的时刻会首先挪用init
函数:
void init (v8::Handle<v8::Object> target)
{
Nan::HandleScope scope;
heapdiff::HeapDiff::Initialize(target);
Nan::SetMethod(target, "upon_gc", memwatch::upon_gc);
Nan::SetMethod(target, "gc", memwatch::trigger_gc);
Nan::AddGCPrologueCallback(memwatch::before_gc);
Nan::AddGCEpilogueCallback(memwatch::after_gc);
}
init函数的进口参数v8:Handle<v8:Object> target
可以类比nodejs中的module.exports
的exports
对象。函数内部做的完成可以分为三块,初始化target、给target绑定upon_gc
和gc
两个函数、在nodejs的gc前后离别挂上对应的钩子函数。
Initialize完成
到heapdiff.cc
文件中来看heapdiff::HeapDiff::Initialize(target);
的完成。
void heapdiff::HeapDiff::Initialize ( v8::Handle<v8::Object> target )
{
Nan::HandleScope scope;
v8::Local<v8::FunctionTemplate> t = Nan::New<v8::FunctionTemplate>(New);
t->InstanceTemplate()->SetInternalFieldCount(1);
t->SetClassName(Nan::New<v8::String>("HeapDiff").ToLocalChecked());
Nan::SetPrototypeMethod(t, "end", End);
target->Set(Nan::New<v8::String>("HeapDiff").ToLocalChecked(), t->GetFunction());
}
Initialize
函数中建立一个叫做HeapDiff
的函数t
,同时在t
的原型链上绑了end
要领,使得js层面可以实行vat hp = new memwatch.HeapDiff();hp.end()
。
new memwatch.HeapDiff
完成
当js实行new memwatch.HeapDiff();
的时刻,c++层面会实行heapdiff::HeapDiff::New
函数,去掉解释和不必要的宏,New函数精简以下:
NAN_METHOD(heapdiff::HeapDiff::New)
{
if (!info.IsConstructCall()) {
return Nan::ThrowTypeError("Use the new operator to create instances of this object.");
}
Nan::HandleScope scope;
HeapDiff * self = new HeapDiff();
self->Wrap(info.This());
s_inProgress = true;
s_startTime = time(NULL);
self->before = v8::Isolate::GetCurrent()->GetHeapProfiler()->TakeHeapSnapshot(NULL);
s_inProgress = false;
info.GetReturnValue().Set(info.This());
}
可以看到用户在js层面实行var hp = new memwatch.HeapDiff();
的时刻,c++层面会挪用nodejs中的v8的api对对堆上内存打一个snapshot保留到self->before中,并将当前对象返回出去。
memwatch.HeapDiff.End
完成
当用户实行hp.end()
的时刻,会实行原型链上的end要领,也就是c++的heapdiff::HeapDiff::End
要领。一样去掉冗余的解释以及宏,End要领可以精简以下:
NAN_METHOD(heapdiff::HeapDiff::End)
{
Nan::HandleScope scope;
HeapDiff *t = Unwrap<HeapDiff>( info.This() );
if (t->ended) {
return Nan::ThrowError("attempt to end() a HeapDiff that was already ended");
}
t->ended = true;
s_inProgress = true;
t->after = v8::Isolate::GetCurrent()->GetHeapProfiler()->TakeHeapSnapshot(NULL);
s_inProgress = false;
v8::Local<Value> comparison = compare(t->before, t->after);
((HeapSnapshot *) t->before)->Delete();
t->before = NULL;
((HeapSnapshot *) t->after)->Delete();
t->after = NULL;
info.GetReturnValue().Set(comparison);
}
在End函数中,拿到当前的HeapDiff对象以后,再对当前的堆上内存再打一个snapshot,挪用compare函数对前后两个snapshot对照后获得comparison后,将前后两次snapshot对象释放掉,并将效果关照给js。
下面剖析下compare函数的细致完成:
compare函数内部会递归挪用buildIDSet函数获得终究堆快照的diff效果。
static v8::Local<Value>
compare(const v8::HeapSnapshot * before, const v8::HeapSnapshot * after)
{
Nan::EscapableHandleScope scope;
int s, diffBytes;
Local<Object> o = Nan::New<v8::Object>();
// first let's append summary information
Local<Object> b = Nan::New<v8::Object>();
b->Set(Nan::New("nodes").ToLocalChecked(), Nan::New(before->GetNodesCount()));
//b->Set(Nan::New("time"), s_startTime);
o->Set(Nan::New("before").ToLocalChecked(), b);
Local<Object> a = Nan::New<v8::Object>();
a->Set(Nan::New("nodes").ToLocalChecked(), Nan::New(after->GetNodesCount()));
//a->Set(Nan::New("time"), time(NULL));
o->Set(Nan::New("after").ToLocalChecked(), a);
// now let's get allocations by name
set<uint64_t> beforeIDs, afterIDs;
s = 0;
buildIDSet(&beforeIDs, before->GetRoot(), s);
b->Set(Nan::New("size_bytes").ToLocalChecked(), Nan::New(s));
b->Set(Nan::New("size").ToLocalChecked(), Nan::New(mw_util::niceSize(s).c_str()).ToLocalChecked());
diffBytes = s;
s = 0;
buildIDSet(&afterIDs, after->GetRoot(), s);
a->Set(Nan::New("size_bytes").ToLocalChecked(), Nan::New(s));
a->Set(Nan::New("size").ToLocalChecked(), Nan::New(mw_util::niceSize(s).c_str()).ToLocalChecked());
diffBytes = s - diffBytes;
Local<Object> c = Nan::New<v8::Object>();
c->Set(Nan::New("size_bytes").ToLocalChecked(), Nan::New(diffBytes));
c->Set(Nan::New("size").ToLocalChecked(), Nan::New(mw_util::niceSize(diffBytes).c_str()).ToLocalChecked());
o->Set(Nan::New("change").ToLocalChecked(), c);
// before - after will reveal nodes released (memory freed)
vector<uint64_t> changedIDs;
setDiff(beforeIDs, afterIDs, changedIDs);
c->Set(Nan::New("freed_nodes").ToLocalChecked(), Nan::New<v8::Number>(changedIDs.size()));
// here's where we'll collect all the summary information
changeset changes;
// for each of these nodes, let's aggregate the change information
for (unsigned long i = 0; i < changedIDs.size(); i++) {
const HeapGraphNode * n = before->GetNodeById(changedIDs[i]);
manageChange(changes, n, false);
}
changedIDs.clear();
// after - before will reveal nodes added (memory allocated)
setDiff(afterIDs, beforeIDs, changedIDs);
c->Set(Nan::New("allocated_nodes").ToLocalChecked(), Nan::New<v8::Number>(changedIDs.size()));
for (unsigned long i = 0; i < changedIDs.size(); i++) {
const HeapGraphNode * n = after->GetNodeById(changedIDs[i]);
manageChange(changes, n, true);
}
c->Set(Nan::New("details").ToLocalChecked(), changesetToObject(changes));
return scope.Escape(o);
}
该函数中组织了两个对象b(before)、a(after)用于保留前后两个快照的细致信息。用一个js对象形貌以下:
// b(before) / a(after)
{
nodes: // heap snapshot中对象节点个数
size_bytes: // heap snapshot的对象大小(bytes)
size: // heap snapshot的对象大小(kb、mb)
}
进一步对前后两次的快照举行剖析可以获得o,o中的before、after对象就是前后两次的snapshot对象的援用:
// o
{
before: { // before的堆snapshot
nodes:
size_bytes:
size:
},
after: { // after的堆snapshot
nodes:
size_bytes:
size:
},
change: {
freed_nodes: // gc掉的节点数目
allocated_nodes: // 新增节点数目
details: [ // 根据范例String、Array聚合出来的细致信息
{
Array : {
what: // 范例
size_bytes: // 字节数bytes
size: // kb、mb
+: // 新增数目
-: // gc数目
}
},
{}
]
}
}
获得两次snapshot对照的效果后将o返回出去,在End函数中经由过程info.GetReturnValue().Set(comparison);
将效果通报到js层面。
下面来细致说下compare函数中的buildIDSet、setDiff以及manageChange函数的完成。
buildIDSet的用法:buildIDSet(&beforeIDs, before->GetRoot(), s);
,该函数会从堆snapshot的根节点动身,递归的寻觅一切可以接见的子节点,加入到鸠合seen中,做DFS统计一切可达节点的同时,也会对一切节点的shallowSize(对象自身占用的内存,不包括援用的对象所占内存)举行累加,统计当前堆所占用的内存大小。其细致完成以下:
static void buildIDSet(set<uint64_t> * seen, const HeapGraphNode* cur, int & s)
{
Nan::HandleScope scope;
if (seen->find(cur->GetId()) != seen->end()) {
return;
}
if (cur->GetType() == HeapGraphNode::kObject &&
handleToStr(cur->GetName()).compare("HeapDiff") == 0)
{
return;
}
s += cur->GetShallowSize();
seen->insert(cur->GetId());
for (int i=0; i < cur->GetChildrenCount(); i++) {
buildIDSet(seen, cur->GetChild(i)->GetToNode(), s);
}
}
setDiff函数用法:setDiff(beforeIDs, afterIDs, changedIDs);
重要用来盘算鸠合差集用的,细致完成很简单,这里直接贴代码,不再赘述:
typedef set<uint64_t> idset;
// why doesn't STL work?
// XXX: improve this algorithm
void setDiff(idset a, idset b, vector<uint64_t> &c)
{
for (idset::iterator i = a.begin(); i != a.end(); i++) {
if (b.find(*i) == b.end()) c.push_back(*i);
}
}
manageChange函数用法:manageChange(changes, n, false);
,其作用在于做数据的聚合。对某个指定的set,根据set中对象的范例,聚合出每种对象建立了若干、销毁了若干,完成以下:
static void manageChange(changeset & changes, const HeapGraphNode * node, bool added)
{
std::string type;
switch(node->GetType()) {
case HeapGraphNode::kArray:
type.append("Array");
break;
case HeapGraphNode::kString:
type.append("String");
break;
case HeapGraphNode::kObject:
type.append(handleToStr(node->GetName()));
break;
case HeapGraphNode::kCode:
type.append("Code");
break;
case HeapGraphNode::kClosure:
type.append("Closure");
break;
case HeapGraphNode::kRegExp:
type.append("RegExp");
break;
case HeapGraphNode::kHeapNumber:
type.append("Number");
break;
case HeapGraphNode::kNative:
type.append("Native");
break;
case HeapGraphNode::kHidden:
default:
return;
}
if (changes.find(type) == changes.end()) {
changes[type] = change();
}
changeset::iterator i = changes.find(type);
i->second.size += node->GetShallowSize() * (added ? 1 : -1);
if (added) i->second.added++;
else i->second.released++;
return;
}
upon_gc
和gc
完成
这两个要领的在init函数中声明以下:
Nan::SetMethod(target, "upon_gc", memwatch::upon_gc);
Nan::SetMethod(target, "gc", memwatch::trigger_gc);
先看gc要领的完成,现实上对应memwatch::trigger_gc
,完成以下:
NAN_METHOD(memwatch::trigger_gc) {
Nan::HandleScope scope;
int deadline_in_ms = 500;
if (info.Length() >= 1 && info[0]->IsNumber()) {
deadline_in_ms = (int)(info[0]->Int32Value());
}
Nan::IdleNotification(deadline_in_ms);
Nan::LowMemoryNotification();
info.GetReturnValue().Set(Nan::Undefined());
}
经由过程Nan::IdleNotification
和Nan::LowMemoryNotification
触发v8的gc功用。
再来看upon_gc
要领,该要领现实上会绑定一个函数,当实行到gc要领时,就会触发该函数:
NAN_METHOD(memwatch::upon_gc) {
Nan::HandleScope scope;
if (info.Length() >= 1 && info[0]->IsFunction()) {
uponGCCallback = new UponGCCallback(info[0].As<v8::Function>());
}
info.GetReturnValue().Set(Nan::Undefined());
}
个中info[0]就是用户传入的回调函数。挪用new UponGCCallback的时刻,其对应的组织函数内部会实行:
UponGCCallback(v8::Local<v8::Function> callback_) : Nan::AsyncResource("memwatch:upon_gc") {
callback.Reset(callback_);
}
把用户传入的callback_函数设置到UponGCCallback类的成员变量callback上。upon_gc回调的触发与gc的钩子有关,细致看下一节剖析。
gc前、后钩子函数的完成
gc钩子的挂载以下:
Nan::AddGCPrologueCallback(memwatch::before_gc);
Nan::AddGCEpilogueCallback(memwatch::after_gc);
先来看memwatch::before_gc
函数的完成,内部给gc最先纪录了时候:
NAN_GC_CALLBACK(memwatch::before_gc) {
currentGCStartTime = uv_hrtime();
}
再来看memwatch::after_gc
函数的完成,内部会在gc后纪录gc的效果到GCStats构造体中:
struct GCStats {
// counts of different types of gc events
size_t gcScavengeCount; // gc 扫描次数
uint64_t gcScavengeTime; // gc 扫描事宜
size_t gcMarkSweepCompactCount; // gc标记消灭整顿的个数
uint64_t gcMarkSweepCompactTime; // gc标记消灭整顿的时候
size_t gcIncrementalMarkingCount; // gc增量标记的个数
uint64_t gcIncrementalMarkingTime; // gc增量标记的时候
size_t gcProcessWeakCallbacksCount; // gc处置惩罚weakcallback的个数
uint64_t gcProcessWeakCallbacksTime; // gc处置惩罚weakcallback的时候
};
对gc要求举行统计后,经由过程v8的api猎取堆的运用情况,终究将效果保留到barton中,barton内部保护了一个uv_work_t的变量req,req的data字段指向barton对象自身。
NAN_GC_CALLBACK(memwatch::after_gc) {
if (heapdiff::HeapDiff::InProgress()) return;
uint64_t gcEnd = uv_hrtime();
uint64_t gcTime = gcEnd - currentGCStartTime;
switch(type) {
case kGCTypeScavenge:
s_stats.gcScavengeCount++;
s_stats.gcScavengeTime += gcTime;
return;
case kGCTypeMarkSweepCompact:
case kGCTypeAll:
break;
}
if (type == kGCTypeMarkSweepCompact) {
s_stats.gcMarkSweepCompactCount++;
s_stats.gcMarkSweepCompactTime += gcTime;
Nan::HandleScope scope;
Baton * baton = new Baton;
v8::HeapStatistics hs;
Nan::GetHeapStatistics(&hs);
timeval tv;
gettimeofday(&tv, NULL);
baton->gc_ts = (tv.tv_sec * 1000000) + tv.tv_usec;
baton->total_heap_size = hs.total_heap_size();
baton->total_heap_size_executable = hs.total_heap_size_executable();
baton->req.data = (void *) baton;
uv_queue_work(uv_default_loop(), &(baton->req),
noop_work_func, (uv_after_work_cb)AsyncMemwatchAfter);
}
}
在前面事情完成的基础上,将效果丢到libuv的loop中,比及适宜的现实触发还调函数,在回调函数中可以拿到req对象,经由过程接见req.data对其做强迫范例装换可以获得barton对象,在loop的回调函数中,将barton中封装的数据顺次取出来,保留到stats对象中,并挪用uponGCCallback的Call要领,传入字面量stats
和stats对象。
static void AsyncMemwatchAfter(uv_work_t* request) {
Nan::HandleScope scope;
Baton * b = (Baton *) request->data;
// if there are any listeners, it's time to emit!
if (uponGCCallback) {
Local<Value> argv[2];
Local<Object> stats = Nan::New<v8::Object>();
stats->Set(Nan::New("gc_ts").ToLocalChecked(), javascriptNumber(b->gc_ts));
stats->Set(Nan::New("gcProcessWeakCallbacksCount").ToLocalChecked(), javascriptNumberSize(b->stats.gcProcessWeakCallbacksCount));
stats->Set(Nan::New("gcProcessWeakCallbacksTime").ToLocalChecked(), javascriptNumber(b->stats.gcProcessWeakCallbacksTime));
stats->Set(Nan::New("peak_malloced_memory").ToLocalChecked(), javascriptNumberSize(b->peak_malloced_memory));
stats->Set(Nan::New("gc_time").ToLocalChecked(), javascriptNumber(b->gc_time));
// the type of event to emit
argv[0] = Nan::New("stats").ToLocalChecked();
argv[1] = stats;
uponGCCallback->Call(2, argv);
}
delete b;
}
末了在Call函数的内部挪用js传入的callback_函数,并将字面量stats
和stats对象通报到js层面,供上层用户运用。
void Call(int argc, Local<v8::Value> argv[]) {
v8::Isolate *isolate = v8::Isolate::GetCurrent();
runInAsyncScope(isolate->GetCurrentContext()->Global(), Nan::New(callback), argc, argv);
}