2017-08-29 57 views
3

概述:這個項目,我一直在努力從外部源接收消息,並根據收到的信息更新的兵地圖。但是,當我讓頁面運行很長時間時,頁面最終會耗盡所有內存並崩潰。Bing地圖V8 JS API內存泄漏問題

具體內容:這個memory issue出現了,我從Google地圖切換到Bing地圖,無論我嘗試過什麼,我都無法解決它。我已經遍尋搜索,試圖通過Bing地圖最佳做法來解決此問題,或者至少找到導致此問題的原因,但我一直無法找到任何可以解決問題的方法。

我有一個detached DOMs量顯著,並嘗試更密切地分析他們使用谷歌Chrome瀏覽器的控制檯,但一直未果。

嘗試的修復:

  • 移動與所有的處理程序添加到地圖初始化函數(如下顯示)的事,但似乎並沒有太多的改善。
  • 載入從地圖初始化功能的任何Bing地圖模塊(顯示如下)。這清理了很多代碼,但似乎對內存使用量沒有太大影響。
  • 同步加載地圖。然而,這似乎打破了與網站上的Bing地圖有關的一切。
  • 停止在接收到消息中的地圖的處置。這有助於關鍵使用,但對內存使用沒有幫助。

相關代碼:


這裏是地圖的初始化函數(異步通過Bing地圖運行調用的HTML):

function initialize() { 
const CENTER = new Microsoft.Maps.Location(44.96375272262944, -93.2353971897461); 

// Assigns the zoom depending on whether the device is a mobile device or not 
if (isMobile()) { 
    zoom = 12; 
} else { 
    zoom = 13; 
} 

// Initialize the map 
map = new Microsoft.Maps.Map(document.getElementById('map-canvas'), { 
    credentials: API_KEY, 
    minZoom: zoom, 
    center: CENTER, 
    disableStreetside: true, 
    disableStreetsideAutoCoverage: true, 
    enableClickableLogo: false, 
    showLocateMeButton: false, 
    showMapTypeSelector: false 
}); 

bus1 = { 
    assignment: null, 
    destination: null, 
    distance: null, 
    eta: null, 
    location: null, 
    mph: null, 
    name: null, 
    pin: new Microsoft.Maps.Pushpin(map.getCenter(), { 
     icon: 'img/bus1.png', 
     anchor: new Microsoft.Maps.Point(14, 44), 
     visible: false, 
     text: "", 
     title: "" 
    }), 
    polylineRender: null, 
    time: null, 
    timeout: null, 
}; 
bus2 = { 
    assignment: null, 
    destination: null, 
    distance: null, 
    eta: null, 
    location: null, 
    mph: null, 
    name: null, 
    pin: new Microsoft.Maps.Pushpin(map.getCenter(), { 
     icon: 'img/bus2.png', 
     anchor: new Microsoft.Maps.Point(14, 44), 
     visible: false, 
     text: "", 
     title: "" 
    }), 
    polylineRender: null, 
    time: null, 
    timeout: null, 
}; 
bus3 = { 
    assignment: null, 
    destination: null, 
    distance: null, 
    eta: null, 
    location: null, 
    mph: null, 
    name: null, 
    pin: new Microsoft.Maps.Pushpin(map.getCenter(), { 
     icon: 'img/bus3.png', 
     anchor: new Microsoft.Maps.Point(14, 44), 
     visible: false, 
     text: "", 
     title: "" 
    }), 
    polylineRender: null, 
    time: null, 
    timeout: null, 
}; 

buses = [bus1, bus2, bus3]; 

// Add the traffic layer 
Microsoft.Maps.loadModule('Microsoft.Maps.Traffic', function() { 
    trafficLayer = new Microsoft.Maps.Traffic.TrafficManager(map); 
}); 

// Add the directions manager 
Microsoft.Maps.loadModule('Microsoft.Maps.Directions', function() { 
    bus1.polylineRender = new Microsoft.Maps.Directions.DirectionsManager(map); 
    bus2.polylineRender = new Microsoft.Maps.Directions.DirectionsManager(map); 
    bus3.polylineRender = new Microsoft.Maps.Directions.DirectionsManager(map); 

    Microsoft.Maps.Events.addHandler(bus1.polylineRender, 'directionsError', function (e) { 
     console.log("Error: " + e.message + "\r\nResponse Code: " + e.responseCode); 
    }); 
    Microsoft.Maps.Events.addHandler(bus1.polylineRender, 'directionsUpdated', directionsUpdated); 

    Microsoft.Maps.Events.addHandler(bus2.polylineRender, 'directionsError', function (e) { 
     console.log("Error: " + e.message + "\r\nResponse Code: " + e.responseCode); 
    }); 
    Microsoft.Maps.Events.addHandler(bus2.polylineRender, 'directionsUpdated', directionsUpdated); 

    Microsoft.Maps.Events.addHandler(bus3.polylineRender, 'directionsError', function (e) { 
     console.log("Error: " + e.message + "\r\nResponse Code: " + e.responseCode); 
    }); 
    Microsoft.Maps.Events.addHandler(bus3.polylineRender, 'directionsUpdated', directionsUpdated); 
}); 

// Defines the polygons surrounding each campus 
polygonArrSTP = [ 
    new Microsoft.Maps.Location(44.94619673931851, -93.19240808486938), 
    new Microsoft.Maps.Location(44.941321471037966, -93.19249391555786), 
    new Microsoft.Maps.Location(44.94130628263941, -93.19764375686646), 
    new Microsoft.Maps.Location(44.93790398010943, -93.1975257396698), 
    new Microsoft.Maps.Location(44.937926764055824, -93.1924831867218), 
    new Microsoft.Maps.Location(44.94164802063501, -93.19241881370544), 
    new Microsoft.Maps.Location(44.94164802063501, -93.18739771842957), 
    new Microsoft.Maps.Location(44.94618914576464, -93.18735480308533), 
    new Microsoft.Maps.Location(44.94618914576464, -93.1924295425415), 
]; 
polygonArrMPLS = [ 
    new Microsoft.Maps.Location(44.97380025938377, -93.2795798778534), 
    new Microsoft.Maps.Location(44.97295018417148, -93.27883958816528), 
    new Microsoft.Maps.Location(44.97264658282772, -93.27782034873962), 
    new Microsoft.Maps.Location(44.973595331690625, -93.27698349952698), 
    new Microsoft.Maps.Location(44.9745744240603, -93.27614665031433), 
    new Microsoft.Maps.Location(44.97501463068608, -93.27712297439575), 
    new Microsoft.Maps.Location(44.9747205274961, -93.27738046646118), 
    new Microsoft.Maps.Location(44.974339139822895, -93.27832460403442), 
    new Microsoft.Maps.Location(44.97380025938377, -93.2795798778534) 
]; 

// Adds the campus polygons to the map 
polygonMPLS = new Microsoft.Maps.Polygon(polygonArrMPLS, { 
    fillColor: "rgba(255, 0, 0, 0.4)", 
    strokeColor: '#FF0000', 
    strokeThickness: 2 
}); 
polygonSTP = new Microsoft.Maps.Polygon(polygonArrSTP, { 
    fillColor: "rgba(255, 0, 0, 0.4)", 
    strokeColor: '#FF0000', 
    strokeThickness: 2 
}); 

// Assign the polygons to the Map 
map.entities.push(polygonMPLS); 
map.entities.push(polygonSTP); 

// Set the toggle for advanced mode 
advancedModeEnabled = false; 

generateBusStats(); 
subscribeToPubnub(); 

console.log("Initialization complete."); 
} 

這裏是在收到消息時運行的功能:

function redraw(payload) { 

// If the user is does not have the page active, the payload is refused 
if (!acceptingPayloads) { 
    return false; 
} 

let location = new Microsoft.Maps.Location(payload.message.lat, payload.message.lng); 
let name = payload.message.name; 
let dest = payload.message.dest; 
let mph = payload.message.mph; 

const STP = new Microsoft.Maps.Location(44.9416428, -93.1917952); 
const MPLS = new Microsoft.Maps.Location(44.9747502, -93.2774464); 

if (dest.toUpperCase() === "S") { 
    dest = {letter: "S", name: "St. Paul", coords: STP}; 
} else if (dest.toUpperCase() === "M") { 
    dest = {letter: "M", name: "Minneapolis", coords: MPLS}; 
} else { 
    dest = null; 
} 

console.log(name + ": " + location.latitude + ", " + location.longitude + " - " + dest.name + " - " + mph + " mph"); 

// Gets the bus object that the payload was sent from 
currentBus = getCurrentBus(name); 

// Removes the timeout for the current bus 
if (currentBus.timeout !== null) { 
    clearTimeout(currentBus.timeout); 
} 

currentBus.location = location; 
currentBus.destination = dest; 
currentBus.mph = mph; 
currentBus.time = Date.now(); 
currentBus.name = name; 

// Restart the timeout for the current bus 
beginTimeout(); 

// Calculate the distance between the current bus and its destination 
calcDistToDest(); 

$("." + currentBus.assignment + "-item").css('display', 'block') 
} 

最後,這裏是我用來獲取點之間的距離的函數:

function calcDistToDest() { 

// Clear all information from the Directions Manager 
currentBus.polylineRender.clearAll(); 

// Set Route Mode to driving and the render options 
currentBus.polylineRender.setRequestOptions({ 
    routeMode: Microsoft.Maps.Directions.RouteMode.driving 
}); 
currentBus.polylineRender.setRenderOptions({ 
    autoUpdateMapView: false, 
    drivingPolylineOptions: { 
     visible: POLYLINE_VISIBILITY 
    }, 
    waypointPushpinOptions: { 
     visible: false 
    }, 
    firstWaypointPushpinOptions: { 
     anchor: currentBus.pin.getAnchor(), 
     icon: currentBus.pin.getIcon(), 
     title: currentBus.pin.getTitle(), 
     text: currentBus.pin.getText() 
    } 
}); 

// Sets the waypoint of the bus's current position and destination 
currentBus.polylineRender.addWaypoint(new Microsoft.Maps.Directions.Waypoint({ 
    location: currentBus.location 
})); 
currentBus.polylineRender.addWaypoint(new Microsoft.Maps.Directions.Waypoint({ 
    location: currentBus.destination.coords 
})); 

// Calculate the directions 
currentBus.polylineRender.calculateDirections(); 
} 

基本上我尋找的是一個方法來找出是什麼原因造成這個問題,如果問題很簡單,我只是錯過了一些顯而易見的問題或者避免這個問題的最佳做法,這個問題的實際解決方案。

注:我的道歉張貼了這麼多的代碼。很難確定發佈什麼代碼,因爲我不知道代碼的哪一部分導致了問題。讓我知道這是否需要修改,或者是否需要其他信息,我會很樂意。我也從同一個文件中遺漏了很多看似不相關的JS代碼,如果需要的話,我很樂意添加它。

+0

您使用的是Bing Maps V8的哪個分支?凍結的分支在一年內還沒有更新,並且有很多已知的內存泄漏,其中很多已經在主要發佈分支中修復。實驗分支中還有更多的修復。您使用的是哪種瀏覽器,我相信您報告的問題是IE的一個已知問題。 – rbrundritt

+0

所以我一直在使用發佈分支。在Chrome,Firefox,IE和Edge上,這個問題一直在以同樣的速度發生。 –

+1

你可以嘗試實驗分支,看看問題是否仍然存在。如果解決方案已經解決,它可能會在下個星期在主要發佈分支中發佈,如果它能及時發佈,或者接近9月底。 – rbrundritt

回答

0

內存泄漏主要通過執行Bing Maps的實驗分支來解決,如rbrundritt所示。

下面是如何使用的實驗分支導入Bing地圖的例子:

<script src='https://www.bing.com/api/maps/mapcontrol?branch=experimental&callback=[Insert callback function]' async defer></script> 

我後來能夠切換回發佈分支作爲內存泄漏的修復被推到該分支。有關Bing Maps地圖控制分支的更多信息可以在here找到。