Google map 的 Map.panTo() 方法的 Javascript 内存泄漏

标签 javascript asp.net google-maps memory-leaks

我在使用加载 Google map 并不断从一个点平移到另一个点的 Web 应用程序时遇到了 Javascript 内存不足错误。大约需要半天时间才会耗尽内存,但我真的希望它能持续更长时间。我 已经确定使用map.panTo方法时会发生内存泄漏,尽管 我非常怀疑这可能是我使用它的方式(Javascript 不是我的强项) 套装)。您能看一下这段代码并帮我修复这个内存泄漏吗?为了节省调试时间,我加快了此演示代码中平移的时间间隔(即使通过检查 Windows 任务管理器中的进程,泄漏也很明显)。它运行于 ASP .Net 3.5 Web Forms,但没有回发,并且代码完全是 HTML、CSS 和 Javascript。它在 IE 中泄漏最严重(这是我需要用来显示它的)。

编辑:

  1. 我尝试过使用不同版本的 Google Maps API(2、3.0 和 3.6)。
  2. 我知道我不需要将示例代码中的映射放入哈希表或数组中,但这无论如何不会影响内存泄漏。
  3. 我正在努力避免黑客攻击,例如经常刷新页面或使用静态 Google map API。
  4. 无论谁想出了如何修复内存泄漏问题,或者谁能清楚地识别并证明无法修复内存泄漏的原因(如果情况确实如此),我们都将给予悬赏。

代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Map Shifter</title>
    <style type="text/css">
        #divMap { height: 400px; width:400px; position:absolute; top:10%; left:10%; border: 2px solid #ff1100; }    
    </style>
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?v=2&sensor=false"></script>
    <script type="text/javascript" language="javascript">
        //Have tried both HashTable and Array (Both Leak)...
        var m_arrMaps = new HashTable(); //new Array();
        var m_Timer;
        function HashTable() {
            return this;
        }
        function LoadMapAndStartShifting() {
            var objMapCenterPoint = new google.maps.LatLng(20.5, -156);
            var objMapOptions = { zoom: 9, center: objMapCenterPoint, mapTypeId: google.maps.MapTypeId.ROADMAP,
                scaleControl: false, streetViewControl:false, zoomControl: false, mapTypeControl: false, panControl: false
            }
            var map = new google.maps.Map(document.getElementById("divMap"), objMapOptions);
            m_arrMaps["ShiftingMap"] = map;
            setTimeout("ShiftMap(20.5, -156);", 700);
        }
        function ShiftMap(decLat, decLng) {
            var objLatLong = new google.maps.LatLng(decLat, decLng);
            //Have tried setCenter instead of preferred panTo and it still leaks!
            m_arrMaps["ShiftingMap"].panTo(objLatLong);
            if (decLat == 20.5) {
                //Leaks...
                ResetTimer(39, -87);
            }
            else {
                //Still leaks...
                setTimeout("ShiftMap(20.5, -156);", 700);
            }
        }
        function ResetTimer(decLat, decLng) {
            m_Timer = setTimeout("ShiftMap(" + decLat + ", " + decLng + ");", 700);    
        }    
    </script>
</head>
<body onload="LoadMapAndStartShifting();">
    <form id="form1" runat="server">
    <div id="divMap"></div>
   </form>
</body>

最佳答案

为什么要为 map 创建哈希表?您可以将 map 创建为全局变量。我很惊讶这会产生任何影响,但你的评论“//已经尝试过哈希表和数组(两者都泄漏)......”让我想知道为什么你需要哈希表或数组。

您还可以通过将选项结构直接传递到 Map() 函数中来消除 objMapOptions 和 objMapCenterPoint 变量,同样将 latLng 传递到 panTo() 函数中。

<script type="text/javascript" language="javascript">
    var m_Timer, map;

    function LoadMapAndStartShifting() {
        map = new google.maps.Map(document.getElementById("divMap"), { zoom: 9, center: new google.maps.LatLng(20.5, -156), mapTypeId: google.maps.MapTypeId.ROADMAP,
            scaleControl: false, streetViewControl:false, zoomControl: false, mapTypeControl: false, panControl: false
        });
        setTimeout("ShiftMap(20.5, -156);", 700);
    }
    function ShiftMap(decLat, decLng) {
        map.panTo(new google.maps.LatLng(decLat, decLng));
        if (decLat == 20.5) {
            //Leaks...
            ResetTimer(39, -87);
        }
        else {
            //Still leaks...
            setTimeout("ShiftMap(20.5, -156);", 700);
        }
    }
    function ResetTimer(decLat, decLng) {
        m_Timer = setTimeout("ShiftMap(" + decLat + ", " + decLng + ");", 700);    
    }    
</script>

另一个建议,缓存 map 怎么样?我不知道这个示例代码是否真的是你正在做的,但如果它只是以这种方式在两个 map 之间切换,你不能使用静态 map API,缓存图像,然后使用javascript来旋转 map 吗?图片?

关于Google map 的 Map.panTo() 方法的 Javascript 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7446619/

相关文章:

javascript - 如何在 Jest 的 Worker 的默认函数中测试函数?

javascript - 单击编辑 jquery

c# - 根据时间更改 GridView 单元格的颜色

javascript - 谷歌地图 API : How do you ensure that the Google Maps Autocomplete text is an actual address before submitting?

javascript - 在谷歌地图标记中显示 JSON 数据

Javascript 全局变量作用域问题

javascript - 如何找到数组中的第一个频率值?

c# - 创建新模型时上传图像

javascript - 如何在javascript中动态添加asp-for

javascript - 将标签添加到外观和感觉都像地方的自定义标记