{"id":15646313,"url":"https://github.com/justjavac/v8-javascript-memory","last_synced_at":"2025-04-30T12:04:55.332Z","repository":{"id":42195097,"uuid":"303868917","full_name":"justjavac/v8-javascript-memory","owner":"justjavac","description":"V8 JavaScript 内存占用分析","archived":false,"fork":false,"pushed_at":"2022-04-11T02:08:15.000Z","size":282,"stargazers_count":49,"open_issues_count":0,"forks_count":1,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-02-25T07:41:42.230Z","etag":null,"topics":["chrome-devtools","javascript","v8","v8-javascript-engine","v8-profiler"],"latest_commit_sha":null,"homepage":"https://justjavac.com/v8-javascript-memory/","language":"HTML","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/justjavac.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-10-14T01:20:28.000Z","updated_at":"2024-01-04T16:51:18.000Z","dependencies_parsed_at":"2022-08-12T09:10:20.246Z","dependency_job_id":null,"html_url":"https://github.com/justjavac/v8-javascript-memory","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/justjavac%2Fv8-javascript-memory","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/justjavac%2Fv8-javascript-memory/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/justjavac%2Fv8-javascript-memory/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/justjavac%2Fv8-javascript-memory/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/justjavac","download_url":"https://codeload.github.com/justjavac/v8-javascript-memory/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242650896,"owners_count":20163610,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["chrome-devtools","javascript","v8","v8-javascript-engine","v8-profiler"],"created_at":"2024-10-03T12:12:23.834Z","updated_at":"2025-03-09T05:30:29.925Z","avatar_url":"https://github.com/justjavac.png","language":"HTML","readme":"# v8-javascript-memory\n\nV8 JavaScript 内存占用分析。\n\n\u003e 回答知乎的问题：[v8 中 Symbol() 、Object.create(null) 和 {} 的内存占用分别是多少？](https://www.zhihu.com/question/425300093/answer/1523228095)\n\n## 步骤\n\n1. 使用 Chrome 浏览器访问 \u003chttps://justjavac.com/v8-javascript-memory/\u003e\n\n1. 打开 Dev Tools，如图：\n\n  ![](./screen.png)\n\n  1. 选择 Memory 标签页\n  1. 点击 take heap snapshots\n  1. 在过滤框中输入 `ho` 快速过滤出 holder\n\n## 名词解释\n\n**Shallow Size**：对象自身占用内存的大小，不包括它引用的对象。JavaScript 对象会将一些内存用于自身的说明和保存中间值。通常，只有数组和字符串会有明显的浅层大小。\n\n**Retained Size**：这是将对象本身连同其无法从 **GC root** 到达的相关对象一起删除后释放的内存大小。\n\n单位是字节(Byte)。\n\n## 分析\n\n从截图中可以看到，`Symbol()` 的内存占用是 16。\n\n`Object.create(null)` 自身占用 12，总占用 88。\n\n`{}` 自身占用 28，总占用 28。\n\n继续展开你会看到其他信息：\n\n![](./screen2.png)\n\n1. `__proto__` 是原型链。\n2. `map` 就是很多文章都在介绍的 V8 对象的黑魔法 Hidden Class。\n\n## 使用 V8 进行调试\n\nV8 的 `%DebugPrint()` 函数可以打印出对象的调试信息。这需要手动使用 `--is_debug=true` 参数来编译 V8。\n\n代码：\n\n```js\nlet o = {};\n%DebugPrint(o);\n```\n\n运行：`d8 --allow_natives_syntax heap.js`\n\n输出：\n\n```plain\nDebugPrint: 0x2604080c60e9: [JS_OBJECT_TYPE]\n - map: 0x2604082802d9 \u003cMap(HOLEY_ELEMENTS)\u003e [FastProperties]\n - prototype: 0x2604082413c9 \u003cObject map = 0x2604082801c1\u003e\n - elements: 0x2604080406e9 \u003cFixedArray[0]\u003e [HOLEY_ELEMENTS]\n - properties: 0x2604080406e9 \u003cFixedArray[0]\u003e {}\n0x2604082802d9: [Map]\n - type: JS_OBJECT_TYPE\n - instance size: 28\n - inobject properties: 4\n - elements kind: HOLEY_ELEMENTS\n - unused property fields: 4\n - enum length: invalid\n - back pointer: 0x26040804030d \u003cundefined\u003e\n - prototype_validity cell: 0x2604081c0451 \u003cCell value= 1\u003e\n - instance descriptors (own) #0: 0x2604080401b5 \u003cDescriptorArray[0]\u003e\n - prototype: 0x2604082413c9 \u003cObject map = 0x2604082801c1\u003e\n - constructor: 0x2604082413e5 \u003cJSFunction Object (sfi = 0x2604081c5869)\u003e\n - dependent code: 0x2604080401ed \u003cOther heap object (WEAK_FIXED_ARRAY_TYPE)\u003e\n - construction counter: 0\n```\n\n**注：** 虽然 node 和 deno 都支持 V8 的 `--allow_natives_syntax` 参数，但是如果你使用 node 或者 deno 运行，只能得到一行类似 `0x053bedbc1399 \u003cObject map = 0x53b630d1d51\u003e` 的输出。 如果想得到详细的输出，必须手动编译，并且在编译过程中增加 `--is_debug=true` 参数。\n\n## License\n\n本\u003cspan xmlns:dct=\"http://purl.org/dc/terms/\" href=\"http://purl.org/dc/dcmitype/Text\" rel=\"dct:type\"\u003e作品\u003c/span\u003e由 \u003ca xmlns:cc=\"https://creativecommons.org/ns#\" href=\"https://justjavac.com\" property=\"cc:attributionName\" rel=\"cc:attributionURL\"\u003ejustjavac\u003c/a\u003e 创作，采用\u003ca rel=\"license\" href=\"https://creativecommons.org/licenses/by-nc-sa/3.0/cn/\"\u003e知识共享署名-非商业性使用-相同方式共享 3.0 中国大陆许可协议\u003c/a\u003e进行许可。凡是转载的文章，翻译的文章，或者由其他作者投稿的文章，版权归原作者所有。\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjustjavac%2Fv8-javascript-memory","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjustjavac%2Fv8-javascript-memory","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjustjavac%2Fv8-javascript-memory/lists"}