近期在做性能測试的时候发现BingMap内存泄露(memory leak)的问题,查找了一些国外的帖子,发现也有类似的问题,可是没有好的解决的方法。

https://social.msdn.microsoft.com/Forums/en-US/3226f255-2ae1-4718-b848-5f24e76b64b0/your-pushpins-are-broken-addremove-leads-to-memory-leak?forum=bingmapsajax

经过一番尝试。找到了一个折中的解决方式,分享一下。

先贴一下Testing Code吧:

<!DOCTYPE html>
<%--<meta http-equiv='x-ua-compatible' content='IE=7;IE=9' />--%> <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Scripts/jquery-1.8.3.min.js" type="text/javascript"></script>
<script charset="UTF-8" type="text/javascript" src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0"></script>
</head>
<body>
<form id="form1" runat="server">
<div id="myMap" style="width:800px;height:600px;position:absolute">
</div>
<input id="btnAdd" type="button" style="position:absolute;top:700px;left:10px;" value="Add" onclick="addBatchPushpin();"/>
<input id="btnDelete" type="button" style="position:absolute;top:700px;left:110px;" value="Delete" onclick="deleteBatchPushpin();" />
<input id="btnClear" type="button" style="position:absolute;top:700px;left:210px;" value="Clear" onclick="clearPushpin();"/>
<input id="Button1" type="button" style="position:absolute;top:700px;left:310px;" value="Find" onclick="findPushpin();"/> <div id="hidElement"></div>
</form>
</body>
</html>
<script type="text/javascript" >
var map = null;
var _layer = null;
var _bingMapKey = "ApirbCqPCAM1bVBVeh5CrrNm-7lzdCkbDT5OPkck8pXXeKWWGNUrIGsTqlkDHlu8";
$(document).ready(function ()
{
var mapOptions = {
credentials: _bingMapKey,
enableClickableLogo: false,
enableSearchLogo: false,
showMapTypeSelector: true,
showCopyright: false
}
map = new Microsoft.Maps.Map($("#myMap")[0], mapOptions);
var infoboxLayer = new Microsoft.Maps.EntityCollection();
map.entities.push(infoboxLayer);
map.setView({ animate: true, zoom: 5, center: new Microsoft.Maps.Location(40, -71) }); _layer = new Microsoft.Maps.EntityCollection();
map.entities.push(_layer); //var lat = 40.56090348161442;
//var lon = -74.345836486816438;
//addPushpin(lat,lon); setInterval(addBatchPushpin, 500);
setInterval(deleteBatchPushpin, 1000);
}) function addBatchPushpin()
{
var lat = 40.56090348161442;
var lon = -74.345836486816438;
for (var i = 0; i < 20; i++)
{
var randomLatitude = Math.random() + lat;
var randomLongitude = Math.random() + lon; addPushpin(randomLatitude, randomLongitude);
}
} function deleteBatchPushpin()
{
var len = _layer.getLength();
for (var i = 0; i < len; i++)
{
_layer.removeAt(0);
$('#cfsNew').remove();
window.CollectGarbage();
}
} function addPushpin(lat, lon)
{
var htmlTest = '<table id="cfsNew" cellpadding="0" cellspacing="0"><tr><td><div style="border: black 2px solid; padding-bottom: 0em; line-height: 12px; background-color: white; margin: 0em;padding-left: 0px; padding-right: 0px; font-family: Arial; color: black;font-size: 12px; font-weight: bold;padding-top: 0em;"><div style="white-space: nowrap;">{P2012-001093} </div><div style="font-weight: bold; font-size: 12px; font-family:Arial"><table style="font-size:inherit;" cellpadding="0" cellspacing="0"><tr><td style="width: 5px; height:5px;"><div style=" background-color: rgb(0,128,0); width: 5px;display: block; margin-bottom: 2px; height: 6px; overflow: hidden; margin-right: 1px"> </div></td><td style="color: rgb(0,128,0);font-size: 12px; font-family:Arial; font-weight:bold;">{297}</td></tr><tr><td style="width: 5px;"><div style=" background-color: rgb(0,128,0); width: 5px;display: block; margin-bottom: 2px; height: 6px; overflow: hidden; margin-right: 1px"></div></td><td style="color: rgb(0,128,0);font-size: 12px; font-family:Arial; font-weight:bold;">907</td></tr><tr><td style="width: 5px;"><div style=" background-color: rgb(255,0,0); width: 5px;display: block; margin-bottom: 4px; height: 6px; overflow: hidden; margin-right: 1px"></div></td><td style="color: rgb(0,128,0);font-size: 12px; font-family:Arial; font-weight:bold;">401</td></tr><tr><td style="width: 5px;"><div style=" background-color: rgb(0,0,220); width: 5px;display: block; margin-bottom: 4px; height: 6px; overflow: hidden; margin-right: 1px"></div></td><td style="color: rgb(0,128,0);font-size: 12px; font-family:Arial; font-weight:bold;">408</td></tr></table></div></div></td></tr><tr><td align="center"><div align="center"><img style="position:relative; width: 40px; height: 40px; left: 20px;top:-1px;" src="Images/ReOpened Box.png" /> <img style="position:relative; width: 35px; height: 35px; left: -21px; top: -1px;" src="Images/pin.png" /></div></td></tr></table>';
var loc = new Microsoft.Maps.Location(lat, lon);
var html = htmlTest; var pushpinOptions = { htmlContent: html, zIndex: 100000, width: getWidth(html) };
var pushpin = new Microsoft.Maps.Pushpin(loc, pushpinOptions);
_layer.push(pushpin);
} function clearPushpin()
{
_layer.clear();
window.CollectGarbage();
} function findPushpin()
{
var test = $('#cfsNew').length;
alert(test);
} function getWidth(html)
{
element = $("#hidElement");
element.html(html);
var height = element[0].scrollHeight;
var width = element.children().width();
return width;
} </script>

分析一下详细的原因吧,就是往地图上添加pushpin的时候,实际上是添加了一个HTML元素。可是移除的时候仅仅是调用_layre.remove(pushpin)或者_layer.removeAt(index)。这两个方法是一样的效果,仅仅是从BingMap的EntityCollection集合中移除了Pushpin对象。可是Pushpin的HTML对象在内存中依旧存在,所以频繁添加移除会导致内存不断的增长。

測试结果:

1. 将cfsNew的HTML元素直接放到页面上,然后每次通过取DOM元素的方式取去加入到地图上。内存增长的速度大概是每秒10M。

2. 将cfsNew的HTML写成一个var的字符串的形式赋给Pushpin的contentHTML上,内存增长的速度大概每秒1M。

3. 假设不用HTML格式的Pushpin。用默认的Pushpin的图标(水滴状),内存添加的频率为每秒0.1M。

4. 加入了$('#cfsNew').remove();方法之后,内存持续添加的情况消失了。

眼下我仅仅是通过删除是手动移除HTML对象的方式解决问题,只是因为每一个图标的HTML是不一样的。还须要绑定特别的属性做这种标记。给程序开发添加了非常大的不方便,还是希望Microsoft能发现并解决问题。

BingMap频繁Add Pushpin和Delete Pushpin会导致内存泄露的更多相关文章

  1. SVN中正确的add操作和delete操作

    add操作: delete操作:

  2. 内存分配(new/delete,malloc/free,allocator,内存池)

    以下来源http://www.cnblogs.com/JCSU/articles/1051826.html 程序员们经常编写内存管理程序,往往提心吊胆.如果不想触雷,唯一的解决办法就是发现所有潜伏的地 ...

  3. 【ThinkingInC++】64、重载new和delete,来模仿内存的分配

    /** * 书本:[ThinkingInC++] * 功能:重载new和delete.来模仿内存的分配 * 时间:2014年10月5日14:30:11 * 作者:cutter_point */ #in ...

  4. Qt中内存泄露和退出崩溃的问题 delete

    Qt中帮程序员做了一些内存回收的事情,但正因为这些反而让对此不熟悉的人会屡屡犯错. 收录一篇不错的文章: 在C++中学习过程中,我们都知道: delete 和 new 必须 配对使用(一 一对应):d ...

  5. 从零开始学C++之重载 operator new 和 operator delete 实现一个简单内存泄漏跟踪器

    先来说下实现思路:可以实现一个Trace类,调用 operator new 的时候就将指向分配内存的指针.当前文件.当前行等信息添加进Trace 成员map容器内,在调用operator delete ...

  6. operator new 和 operator delete 实现一个简单内存泄漏跟踪器

    先来说下实现思路:可以实现一个Trace类,调用 operator new 的时候就将指向分配内存的指针.当前文件.当前行等信息添加进Trace 成员map容器内,在调用operator delete ...

  7. new和delete动态分配和撤销内存

    视频:C++引用及new和delete的使用 一.new用法 使用new运算符时必须已知数据类型,new运算符会向系统堆区申请足够的存储空间,如果申请成功,就返回该内存块的首地址,动态分配失败,则返回 ...

  8. C++:重载全局new/delete实现跨平台多线程内存检测

    Reference: https://blog.csdn.net/u014023615/article/details/39551191 Reference: https://blog.csdn.ne ...

  9. 虚析构函数? vptr? 指针偏移?多态数组? delete 基类指针 内存泄漏?崩溃?

    五条基本规则: 1.如果基类已经插入了vptr, 则派生类将继承和重用该vptr.vptr(一般在对象内存模型的顶部)必须随着对象类型的变化而不断地改变它的指向,以保证其值和当前对象的实际类型是一致的 ...

随机推荐

  1. 控制div固定在页面的某个位置 ,用js感觉很麻烦 CSS更好一些

    CSS代码:<style type="text/css"> html,body { width:100%; height:100%; margin:0px; paddi ...

  2. Windows7下修改pip源

    以下列举三种方式的pip源配置: 1. 设置环境变量PIP_CONFIG_FILE指向pip.ini源配置文件,pip.ini文件内容如下: [global] index-url = http://m ...

  3. QQ互联账号登录

    本文说明的是依据某应用通过网页的qq信息来登录的过程.用途是利用QQ账号就能高速自己主动注冊并可以登录客户应用. 从webserver与腾讯server通信获取开房平台用户OpenID,再在应用ser ...

  4. CCNA Cloud CLDFND 210-451 QUIZ: Server Virtualization

    Author:海峰 http://weibo.com/344736086 http://yanheven.github.io/ http://blog.csdn.net/yanheven1 1.Whi ...

  5. java.lang.IllegalArgumentException: The observer is null.终于解决方式

    java.lang.IllegalArgumentException: The observer is null.终于解决方式 在使用数据适配的时候的问题: java.lang.IllegalArgu ...

  6. js---11闭包

    //匿名立即调用函数 (function(){//把a,b,f全部隐藏在函数中,外部访问不到, var a = 5; var b = 6; function f(){ alert(a); } wind ...

  7. python-打开网页

    最近一直想通过python来实现网页的操作.因为想把自己vimrc的配置直接通过脚本来实现.比较理想的情况下,就是通过一个脚本,把自己需要的一些资源直接从网上下载下来,然后再对下载的资源进行安装等操作 ...

  8. C#与Linux守护进程

    用C#编写Linux守护进程   如果要在Red Hat Enterprise Linux上将.NET Core进程作为后台进程运行,则可以创建自定义systemd单元.今天我将为.NET Core编 ...

  9. git 版本管理工具说明

    $ git init                 (初始化本地仓库,会生成.git 文件夹  .git 文件夹里存储了所有的版本信息.标记等内容) $ git add .              ...

  10. 湖南省第八届大学生计算机程序设计竞赛(A,B,C,E,F,I,J)

    A 三家人 Description 有三户人家共拥有一座花园,每户人家的太太均需帮忙整理花园.A 太太工作了5 天,B 太太则工作了4 天,才将花园整理完毕.C 太太因为正身怀六甲无法加入她们的行列, ...