jQuery或javascript来查找页面的内存使用情况 [英] jQuery or javascript to find memory usage of page

查看:222
本文介绍了jQuery或javascript来查找页面的内存使用情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法找出网页或jquery应用程序使用了多少内存?

Is there a way to find out how much memory is being used by a web page, or by my jquery application?

这是我的情况:

我正在使用jquery前端和一个安静的后端构建一个数据量很大的webapp以JSON提供数据。页面加载一次,然后一切都通过ajax发生。

I'm building a data heavy webapp using a jquery frontend and a restful backend that serves data in JSON. The page is loaded once, and then everything happens via ajax.

UI为用户提供了在UI中创建多个选项卡的方法,每个选项卡可以包含批量和很多数据。我正在考虑限制他们可以创建的标签数量,但他们认为只有在内存使用量超过某个阈值时才限制它们会很好。

The UI provides users with a way to create multiple tabs within the UI, and each tab can contain lots and lots of data. I'm considering limiting the number of tabs they can create, but was thinking it would be nice to only limit them once memory usage has gone above a certain threshold.

基于在答案上,我想做一些澄清:

Based on the answers, I'd like to make some clarfications:


  • 我正在寻找运行时解决方案(不仅仅是开发人员工具),这样我的应用程序就可以根据用户浏览器中的内存使用情况确定操作。

  • 计算DOM元素或文档大小可能是一个很好的估计,但它可能非常不准确,因为它不包括事件绑定,data(),插件和其他内存数据结构。

推荐答案

2015年更新

如果你想支持所有使用中的主流浏览器,那么在2012年这是不可能的。不幸的是,现在这仍然是仅限Chrome 功能(非标准扩展程序) window.performance )。

Back in 2012 this wasn't possible, if you wanted to support all major browsers in-use. Unfortunately, right now this is still a Chrome only feature (a non-standard extension of window.performance).

window.performance.memory

浏览器支持:Chrome 6 +

Browser support: Chrome 6+

2012回答


有没有办法找出网页或我的jquery应用程序使用了多少内存?我正在寻找运行时解决方案(不仅仅是开发人员工具),以便我的应用程序可以根据用户浏览器中的内存使用情况确定操作。

Is there a way to find out how much memory is being used by a web page, or by my jquery application? I'm looking for a runtime solution (not just developer tools), so that my application can determine actions based on memory usage in a user's browser.

简单但正确的答案是。并非所有浏览器都会向您公开此类数据。而且我认为你应该放弃这个想法只是因为手工解决方案的复杂性和不准确性可能会引入比它解决的更多问题。

The simple but correct answer is no. Not all browsers expose such data to you. And I think you should drop the idea simply because the complexity and inaccuracy of a "handmade" solution may introduce more problem than it solves.


计算DOM元素或文档大小可能是一个很好的估计,但它可能非常不准确,因为它不包括事件绑定,data(),插件和其他内存数据结构。

Counting DOM elements or document size might be a good estimation, but it could be quite inaccurate since it wouldn't include event binding, data(), plugins, and other in-memory data structures.

如果你真的想坚持自己的想法,你应该分开固定和动态内容。

If you really want to stick with your idea you should separate fixed and dynamic content.

固定内容不依赖于用户操作(脚本文件,插件等使用的内存)

其他所有内容都被认为是动态的,应该是确定限制时的主要关注点。

Fixed content is not dependant on user actions (memory used by script files, plugins, etc.)
Everything else is considered dynamic and should be your main focus when determining your limit.

但是没有简单的方法来总结它们。您可以实现一个收集所有这些信息的跟踪系统。所有操作都应调用适当的跟踪方法。例如:

But there is no easy way to summarize them. You could implement a tracking system that gathers all these information. All operations should call the appropriate tracking methods. e.g:

包装或覆盖 jQuery.data 方法通知跟踪系统你的数据分配。

Wrap or overwrite jQuery.data method to inform the tracking system about your data allocations.

包装html操作,以便跟踪添加或删除内容( innerHTML.length 是最佳估计)。

Wrap html manipulations so that adding or removing content is also tracked (innerHTML.length is the best estimate).

如果您保留大型内存中对象,则还应对其进行监控。

If you keep large in-memory objects they should also be monitored.

至于事件绑定你应该使用事件委托,然后它也可以被认为是一个有点固定的因素。

As for event binding you should use event delegation and then it could also be considered a somewhat fixed factor.

另一个方面很难估计你的正确的内存要求是不同的浏览器可能以不同的方式分配内存(对于Javascript对象和DOM元素)。

Another aspect that makes it hard to estimate your memory requirements correctly is that different browsers may allocate memory differently (for Javascript objects and DOM elements).

这篇关于jQuery或javascript来查找页面的内存使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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