GithubHelp home page GithubHelp logo

justjavac / v8-javascript-memory Goto Github PK

View Code? Open in Web Editor NEW
49.0 4.0 1.0 282 KB

V8 JavaScript 内存占用分析

Home Page: https://justjavac.com/v8-javascript-memory/

HTML 72.47% JavaScript 27.53%
v8 v8-javascript-engine v8-profiler javascript chrome-devtools

v8-javascript-memory's Introduction

v8-javascript-memory

V8 JavaScript 内存占用分析。

回答知乎的问题:v8 中 Symbol() 、Object.create(null) 和 {} 的内存占用分别是多少?

步骤

  1. 使用 Chrome 浏览器访问 https://justjavac.com/v8-javascript-memory/

  2. 打开 Dev Tools,如图:

  1. 选择 Memory 标签页
  2. 点击 take heap snapshots
  3. 在过滤框中输入 ho 快速过滤出 holder

名词解释

Shallow Size:对象自身占用内存的大小,不包括它引用的对象。JavaScript 对象会将一些内存用于自身的说明和保存中间值。通常,只有数组和字符串会有明显的浅层大小。

Retained Size:这是将对象本身连同其无法从 GC root 到达的相关对象一起删除后释放的内存大小。

单位是字节(Byte)。

分析

从截图中可以看到,Symbol() 的内存占用是 16。

Object.create(null) 自身占用 12,总占用 88。

{} 自身占用 28,总占用 28。

继续展开你会看到其他信息:

  1. __proto__ 是原型链。
  2. map 就是很多文章都在介绍的 V8 对象的黑魔法 Hidden Class。

使用 V8 进行调试

V8 的 %DebugPrint() 函数可以打印出对象的调试信息。这需要手动使用 --is_debug=true 参数来编译 V8。

代码:

let o = {};
%DebugPrint(o);

运行:d8 --allow_natives_syntax heap.js

输出:

DebugPrint: 0x2604080c60e9: [JS_OBJECT_TYPE]
 - map: 0x2604082802d9 <Map(HOLEY_ELEMENTS)> [FastProperties]
 - prototype: 0x2604082413c9 <Object map = 0x2604082801c1>
 - elements: 0x2604080406e9 <FixedArray[0]> [HOLEY_ELEMENTS]
 - properties: 0x2604080406e9 <FixedArray[0]> {}
0x2604082802d9: [Map]
 - type: JS_OBJECT_TYPE
 - instance size: 28
 - inobject properties: 4
 - elements kind: HOLEY_ELEMENTS
 - unused property fields: 4
 - enum length: invalid
 - back pointer: 0x26040804030d <undefined>
 - prototype_validity cell: 0x2604081c0451 <Cell value= 1>
 - instance descriptors (own) #0: 0x2604080401b5 <DescriptorArray[0]>
 - prototype: 0x2604082413c9 <Object map = 0x2604082801c1>
 - constructor: 0x2604082413e5 <JSFunction Object (sfi = 0x2604081c5869)>
 - dependent code: 0x2604080401ed <Other heap object (WEAK_FIXED_ARRAY_TYPE)>
 - construction counter: 0

注: 虽然 node 和 deno 都支持 V8 的 --allow_natives_syntax 参数,但是如果你使用 node 或者 deno 运行,只能得到一行类似 0x053bedbc1399 <Object map = 0x53b630d1d51> 的输出。 如果想得到详细的输出,必须手动编译,并且在编译过程中增加 --is_debug=true 参数。

License

作品justjavac 创作,采用知识共享署名-非商业性使用-相同方式共享 3.0 **大陆许可协议进行许可。凡是转载的文章,翻译的文章,或者由其他作者投稿的文章,版权归原作者所有。

v8-javascript-memory's People

Contributors

justjavac avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

729993031

v8-javascript-memory's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.