I don't think there is a direct way to do this.

There is v8::internal::HeapObject::Size(), but it only gives you a form of
"shallow" size. Under the hood, most (JavaScript-level) objects are
composed of several (heap) objects; e.g. for an Array, all the elements are
stored in a separate backing store, which would not be included in the
result of a ->Size() call on the array itself.

You could try to implement a crawl of the object graph that sums up all the
nested objects, but it's not obvious how you'd deal with shared objects
(canonical example: typically, many objects share the same hidden class.
How do you count the bytes of the hidden class metadata?). DevTools gives
one answer via the "retained size" of each object in a heap snapshot. Does
that serve your needs?

Another way to approximate it is to wrap your function in a pair of gc()
calls, run with --expose-gc --trace-gc, and compare the tracing output
before/after. The limitation is that you only get 0.1 MB resolution; you
can amplify the signal with a loop. Example:

7 ms: Mark-sweep 0.6 (3.7) -> 0.5 (4.7) MB, 1.1 / 0.0 ms
9 ms: Mark-sweep 1.3 (4.7) -> 0.5 (4.7) MB, 1.1 / 0.0 ms

1.3 - 0.5 = 0.8 MB consumed, in this case for 1,000 invocations of function
my_function() { new Array(100); }. The expected value (from knowing what
such an array looks like in memory) is 0.816 MB, so the approximated result
is reasonably accurate.

On Thu, Jul 19, 2018 at 11:43 PM czczcheng <zhecheng2...@yeah.net> wrote:

> Is it possible to get the memory for one of v8::Object ?
> I want to count how much memory was used  during a single call of a
> function.
>
> --
> --
> v8-users mailing list
> v8-users@googlegroups.com
> http://groups.google.com/group/v8-users
> ---
> You received this message because you are subscribed to the Google Groups
> "v8-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to v8-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
-- 
v8-users mailing list
v8-users@googlegroups.com
http://groups.google.com/group/v8-users
--- 
You received this message because you are subscribed to the Google Groups 
"v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to v8-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to