如何销毁JavaScript对象? [英] How to destroy a JavaScript object?

查看:839
本文介绍了如何销毁JavaScript对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我遇到了一个耗费太多内存且增加10 MB /秒的应用程序。

Recently, I came across one of my application which consumes too much memory and increasing by 10 MB/sec.

所以,我想知道最好的方法销毁JavaScript对象和变量,以便内存消耗保持不变,我的FF不会被破坏。

So, I like to know the best way to destroy JavaScript object and variables so memory consumption stay down and my FF can't get destroyed.

我在每8秒间隔调用两个JavaScript而不重新加载页面。

I am calling two of my JavaScript in every 8 sec interval without reloading the page.

function refresh() {
    $('#table_info').remove();
    $('#table').hide();
    if (refreshTimer) {
        clearTimeout(refreshTimer);
        refreshTimer = null ;
    }
    document.getElementById('refresh_topology').disabled=true; 
    $('<div id="preload_xml"></div>').html('<img src="pic/dataload.gif" alt="loading data" /><h3>Loading Data...</h3>').prependTo($("#td_123"));
    $("#topo").hide();
    $('#root').remove();
    show_topology();
}

如何查看哪个变量导致内存开销和停止执行该过程的方法?

How can I see which variable cause Memory overhead and method to stop execution of that process?

推荐答案

您可以将所有代码放在一个名称空间中,如下所示:

You could put all of your code under one namespace like this:

var namespace = {};

namespace.someClassObj = {};

delete namespace.someClassObj;

使用 delete 关键字将删除参考对于该属性,但在低级别,JavaScript垃圾收集器(GC)将获得有关要回收哪些对象的更多信息。

Using the delete keyword will delete the reference to the property, but on the low level the JavaScript garbage collector (GC) will get more information about which objects to be reclaimed.

您还可以使用Chrome开发者工具获取应用的内存配置文件,以及应用中的哪些对象需要按比例缩小。

You could also use Chrome Developer Tools to get a memory profile of your app, and which objects in your app are needing to be scaled down.

这篇关于如何销毁JavaScript对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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