Example for using AddMemoryAllocationCallback V8 method

33 views
Skip to first unread message

gihan karunarathne

unread,
Jul 21, 2015, 12:24:43 AM7/21/15
to v8-u...@googlegroups.com
I'm trying to use AddMemoryAllocationCallback method on Ubuntu. I want to call to that method and return that size value. I came up with following code. Seems to be it's not calling to callback method.

memoryallocation.cc file
#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)


.gyp configuration
{
 
"targets": [
   
{
     
"target_name": "memoryallocation",
     
"sources": [ "./src/memoryallocation.cc" ],
     
"include_dirs": [
       
"<!(node -e \"require('nan')\")"
     
]
   
}
 
]
}



.js file

var mem_alloc = require('bindings')('memoryallocation');
mem_alloc
.getNewSpace();

Thank you in advance!

Ben Noordhuis

unread,
Jul 21, 2015, 5:28:45 AM7/21/15
to v8-u...@googlegroups.com
The callback is called when V8 allocates memory, not when you install
the callback.

gihan karunarathne

unread,
Jul 21, 2015, 7:26:34 AM7/21/15
to v8-u...@googlegroups.com
Thank you the reply. I'm new to V8 and C++ programming. I think, I understood what you said. 
The callback is called when V8 allocates memory, not when you install
the callback.
Does that mean, my code is correct ? So, V8 have to allocate some memory in order to trigger `callback`.

How can I test this code ? Can you provide me an example? (I spent hours and hour on this matter, but I couldn't figure out away)

Thank you in advance!

gihan karunarathne

unread,
Jul 21, 2015, 1:06:50 PM7/21/15
to v8-u...@googlegroups.com
Hi devs:

Can someone help me with this issue? I want to know whether I have correctly written the code below. Specially adding a callback to `V8::AddMemoryAllocationCallback`  method.

#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)

Thank you in advance!

gihan karunarathne

unread,
Jul 21, 2015, 2:06:02 PM7/21/15
to v8-u...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages