#include <nan.h>
using namespace std;
using namespace v8;
int x;
void callback(ObjectSpace space, AllocationAction action, int size) {
x = size;
}
NAN_METHOD(GetNewSpace) {
V8::AddMemoryAllocationCallback(callback, kObjectSpaceNewSpace, kAllocationActionAllocate);
NanReturnVAlue(NanNew<Number>(x));
}
void Init(Handle<Object> exports) {
exports->Set(NanNew<String>("getNewSpace"), NanNew<FunctionTemplate>(GetNewSpace)->GetFunction());
}
NODE_MODULE(memoryallocation, Init)
{
"targets": [
{
"target_name": "memoryallocation",
"sources": [ "./src/memoryallocation.cc" ],
"include_dirs": [
"<!(node -e \"require('nan')\")"
]
}
]
}
var mem_alloc = require('bindings')('memoryallocation');
mem_alloc.getNewSpace();
The callback is called when V8 allocates memory, not when you install
the callback.
#include <nan.h>
using namespace std;
using namespace v8;
int x;
// Is this function need to be static?
void callback(ObjectSpace space, AllocationAction action, int size) {
x = size;
}
NAN_METHOD(GetNewSpace) {
V8::AddMemoryAllocationCallback(callback, kObjectSpaceNewSpace, kAllocationActionAllocate);
//Or V8::AddMemoryAllocationCallback(&callback, kObjectSpaceNewSpace, kAllocationActionAllocate);
NanReturnVAlue(NanNew<Number>(x));
}
void Init(Handle<Object> exports) {
exports->Set(NanNew<String>("getNewSpace"), NanNew<FunctionTemplate>(GetNewSpace)->GetFunction());
}
NODE_MODULE(memoryallocation, Init)