Google Maps JS v3 - 分离的DOM树 - 内存泄漏? [英] Google Maps JS v3 - detached DOM tree - memory leak?

查看:177
本文介绍了Google Maps JS v3 - 分离的DOM树 - 内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题。我通过以下代码,通过 setMap(null)选项删除了包含标记的所有引用。

I have the following issue. I am removing all references to a google maps instance including markers via the setMap(null) option via the following code:

destroyMaps = function () {
    leftMap = null;
    window.map = null;
    geocoder = null;
    for (var i=0; i<window.rightMarkers.length; i++) {
        window.rightMarkers[i].setMap(null);
        window.rightMarkers[i] = null;
    }
    window.rightMarkers = null;
    $("#map-canvas-right").remove();

    for (var i=0; i<window.leftMarkers.length; i++) {
        window.leftMarkers[i].setMap(null);
        window.leftMarkers[i] = null;
    }
    window.leftMarkers = null;
    $("#map-canvas-left").remove();

}

仅引用 leftMap的东西我的整个代码中的 window.map 是:

var marker = new google.maps.Marker({
                    position: myLatlng,
                    map: window.map,
                    icon: window.pins[keyword_category.category_name],
                    shadow: window.pins["Shadow"],

                    title:job.job_title
});
marker.job_type =  keyword_category.category_name;
window.rightMarkers.push(marker);



对于 leftMap



For leftMap

var marker = new google.maps.Marker({
                position: myLatlng,
                map: leftMap,
                icon: window.pins[keyword_category.category_name],
                shadow: window.pins["Shadow"],

                title:job.job_title
 });
 window.leftMarkers.push(marker);

然而,在我分离的DOM树中,在映射创建/谷歌地图图块:

However in my detached DOM tree, when comparing before the maps were created / after they were destroyed, remains the google maps tiles:

(右键单击 - 打开图片以查看完整大小)

(Right click - open image to see full size)

我可以做些什么来找出原因这个DOM泄漏?

What can I do to find out what's causing this DOM leak?

推荐答案

这是Google Maps API v3中的一个已知问题 - 甚至纯粹的创建和破坏 google.maps 对象(无标记创建)会导致内存泄漏。请参阅 问题3803:错误:销毁Google地图实例不要释放内存

This is a known issue in Google Maps API v3 - even pure creation and destruction of google.maps object (no marker creation) results in memory leak. See Issue 3803: Bug: Destroying Google Map Instance Never Frees Memory.

他们通过创建创建和销毁 google.maps 对象的简单循环来重现问题。见

They reproduce the issue by creating a simple loop that creates and destroys the google.maps object. See

http://jsfiddle.net/KWf4r/

按下开始之后,您将观察您的浏览器在内存中增长,直到您停止。

After pressing start, you will observe your browser to grow in memory until you press stop.

问题不是固定的,似乎没有正式的解决方法。肯定有一种方法,但这并不是一个干净的解决方法,显然可能会在下一次发布的Google地图中停止工作,其中包括讨论

The issue is not fixed and there doesn't seem to be an official workaround available. There is certainly a way, but it's not a clean workaround that apparently might stop working in next release of google maps api - citing the discussion:


我实际上已经设法通过手动
找到一个半可行的修复程序,摧毁了Google地图创建的许多元素(和
删除侦听器)。不过,我正在使用大量无证件的东西
来执行此操作(我必须检查chrome检查器才能看到要尝试
删除),所以这似乎不是正确的方法。

I've actually managed to find a semi-workable fix by manually destroying a lot of the elements that google maps creates (and removing listeners). However, I am using a lot of undocumented things to do this (I had to check in the chrome inspector to see what to try to remove), so this doesn't seem the right way to go.

这篇关于Google Maps JS v3 - 分离的DOM树 - 内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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