如何获得JavaScript对象的大小,并在浏览器内存计数 [英] How to get javascript objects size and count in browser memory

查看:2193
本文介绍了如何获得JavaScript对象的大小,并在浏览器内存计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道你们中的大多数将指向我到铬探查堆快照,但空页(无JS无CSS只是HTML>体),它显示了8MB堆大小和12 3万的对象取决于他的心情,所以这个任务完全没有用处的工具,我认为这堆分析器对数据自己的拙见它显示而不是事实上,尽管铬开发工具其余都是真棒。

i know most of you will point me to the chrome profiler heap snapshot, but on empty page (no js no css just html>body) it shows 8mb heap size and 12 to 30 thousand objects depends on his mood, so it is totally useless tool for this task, and i think this heap profiler has its own humble opinion on data it shows rather then fact, though rest of chrome dev tools are awesome.

基本上我的骨干1/1页的应用程序在内存中保持成长,即使我做一切可能的垃圾收集舞蹈, JSON.stringify(OBJ)。长度抛出动作不安全上大多数对象,我怎么能追查那些僵尸容易然后尝试和错误。

Basically my backbone 1page app keeps growing in memory even though i'm doing all possible garbage collection dance, JSON.stringify(Obj).length throws action not secure on most objects, how i can track down those zombies easier then trial and error.

推荐答案

您迪斯Chrome开发者工具,但据我所知,他们是你得到了最好的。你只需要明智地使用它们。

You diss the Chrome Developer Tools, but as far as I know, they're the best you got. You just have to use them judiciously.

比如说你要测试你的应用程序的一些动作是否泄漏内存。这可能是一个观点的呈现,或获取一些新的数据。让我们称之为 动作

Say you want to test whether some action in your application leaks memory. It could be the rendering of a view, or fetching some new data. Let's call this the Action.

为了找出多少内存被保留,又有多少被保留,你首先需要获得一个可衡量的基准,并摆脱了噪音。需要三个步骤来实现的那些东西。

In order to to find out how much memory is reserved, and how much is retained, first you need to get a measurable baseline, and get rid of the noise. You need three steps to achieve those things.


  1. 热身

启动应用程序(浏览到您的网站)。执行 动作 。采取堆快照。该快照将被丢弃,但它会运行GC,让你一个干净的石板。热身还可以确保你没有得到你的衡量数据的任何绒毛:脚本评估,初始加载资源异步等

Start your application (navigate to your website). Perform Action. Take a heap snapshot. This snapshot is discarded, but it'll run the GC and get you a clean slate. The warm-up also ensures that you don't get any fuzz on your measurable data: script evaluation, initial loading resources async etc.

基线

执行 动作 三次。采取堆快照。这是基线,我们将比较我们的记忆resevation和保留。我们做的 动作 三次获得一个合理的平均水平的情况下,有一个在执行路径一些小的变化。请一定要尝试重复的 动作 完全相同的方式每次。

Perform Action three times. Take a heap snapshot. This is the baseline to which we'll compare our memory resevation and retention. We do the Action three times to get a sensible average in case there is some small variability in the execution path. Make sure to try to repeat the Action exactly the same way every time.

测量

执行 动作 三次。采取堆快照。

Perform Action three times. Take a heap snapshot.

现在你有三个快照。首先将被丢弃,但是对我们来说是在摘要作为快照2和3 之间进行分配的对象,以及三角洲有趣的比较从快照3快照2.你可以找到从配置视图的底部/状态栏这些意见。

Now you'll have three snapshots. The first will be discarded, but what's interesting for us are the Summary for Objects allocated between snapshots 2 and 3, as well as the delta Comparison from Snapshot 3 to Snapshot 2. You can find these views from the bottom/status bar of the Profiles view.

您之间看到的数据在基线测量 shapshots是真正的内存配置文件的 动作 。之后,你只需要正确地知道如何跨preT的数据。对于我建议谷歌的事件探查器文档。

The data you see between the Baseline and Measurement shapshots is the true memory profile for Action. After that you just need to know how to interpret the data correctly. For that I recommend Google's documentation on the profiler.

我不认为存在一个更好的工具,或在这个时候的一种方式。如果没有呢,我很想听到它。

I don't think there exists a better tool or a way at this time. If there does, I would love to hear about it.

这篇关于如何获得JavaScript对象的大小,并在浏览器内存计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆