clarification on retained size from v8 context

65 views
Skip to first unread message

madana gopal

unread,
Nov 29, 2017, 7:12:31 AM11/29/17
to v8-users
Hi Team,

We are trying to embed v8 within c++ application. Through the application, v8 context is loaded for every new javascript. And, context is destroyed , on the javascript exit by the user. In this way new context is created and destroyed for every javascript usage. In this way, it looks heap memory created by a context is getting retained and not garbage collected, even after the context is destroyed. Please find the below code, to get clear idea, on what is observed.

//code executed during javascript start
Local<Context>  localContext = node::makeContext(isolate, sandbox);
persistentContext.Reset(isolate, localContext);
printf("Size of newly created context is [%ld] \n",persistentContext.EstimatedSize());
fflush(stdout);

//code executed during javascript exit
persistentContext.Reset();

Here, EstimatedSize() is increasing 1.5 MB for every context creation (i.e estimated size coming for second context created is 1.5 MB more than the value came for first context). Please clarify the way to debug the case. How EstimatedSize() increased value can be identified?. Please provide suggestion.

Thanks.

Regards,
Madan 

Ben Noordhuis

unread,
Nov 29, 2017, 7:35:43 AM11/29/17
to v8-users
As long as there are references to the context or things from the
context (objects, functions), it won't be reclaimed. In your example,
it's not clear what the lifetime of the Local<Context> is, but it will
keep the context alive until its HandleScope is closed.

Also, make sure you are using an up-to-date version of V8. There have
been bugs in the past where contexts were retained longer than
necessary due to weak references embedded in code objects (i.e., JS
functions.)

And finally, note that Context::EstimatedSize() is a no-op that always
returns zero since V8 5.9 and may not have been accurate before that.

madana gopal

unread,
Nov 29, 2017, 7:54:57 AM11/29/17
to v8-users
Thanks for the details. 

1, We are using node version 6.9.0
2, Local<Context> is assigned to Persistent context , mentioned in below code. 
3, I couldn't get the last point. Do you mean, checking with EstimatedSize() is not right approach?. 
4, From heap snapshot, we are seeing many closure leaks. But when I try to debug it with retainer in chrome dev tools, tree is bigger with many frames telling native_context. since it is big, we are stuck in how to hit the proper place , causing the leak reference. 

Please let us know, if you have any suggestions to debug this.
 
Thanks.

Regards,
Madan

 

Ben Noordhuis

unread,
Nov 29, 2017, 8:18:16 AM11/29/17
to v8-users
On Wed, Nov 29, 2017 at 1:54 PM, madana gopal <madanag...@gmail.com> wrote:
> 2, Local<Context> is assigned to Persistent context , mentioned in below
> code.

You call Persistent<Context>::Reset() but it's not clear from the
snippet you posted if the original Local<Context> is still alive. It
remains alive until its HandleScope is destroyed.

> 3, I couldn't get the last point. Do you mean, checking with EstimatedSize()
> is not right approach?.

It may have bugs that affect its accuracy, yes.

> 4, From heap snapshot, we are seeing many closure leaks. But when I try to
> debug it with retainer in chrome dev tools, tree is bigger with many frames
> telling native_context. since it is big, we are stuck in how to hit the
> proper place , causing the leak reference.

It's not clear to me what you mean. Can you post some screenshots?

madana gopal

unread,
Nov 29, 2017, 8:34:24 AM11/29/17
to v8-users
Please find the image of snapshot attached. Here it is showing something called context in heap snapshot retainers in the bottom tree?. What it means and some object like Array it is telling. How we can find the root cause of it and locate it?

Please clarify..

Thanks.

Regards,
Madan 
heap_snapshot_1.png

Ben Noordhuis

unread,
Nov 29, 2017, 11:33:27 AM11/29/17
to v8-users
On Wed, Nov 29, 2017 at 2:34 PM, madana gopal <madanag...@gmail.com> wrote:
> Please find the image of snapshot attached. Here it is showing something
> called context in heap snapshot retainers in the bottom tree?. What it means
> and some object like Array it is telling. How we can find the root cause of
> it and locate it?

"Context" in that, ah, context means "closure context" a.k.a. captured
variables. Consider this example:

var x = 42;
function add(y) { return x + y; }

Here, `x` ends up in the context for `add()`.

madana gopal

unread,
Nov 29, 2017, 8:38:57 PM11/29/17
to v8-users
ok. Thanks. I will go more in snapshot and let you know, if I need more clarifications.

Regards,
Madan
Reply all
Reply to author
Forward
0 new messages