據我所知,聽衆被從內存中刪除。
從理論上講,這可能是因爲聽衆不會主動聽取 - 谷歌地圖只會將它們存儲在相關的功能中,然後詢問該功能在觸發事件時具有哪些聽衆。
實際上,我跑的測試,並且這些結果如下:在JS
Tabs 1 and 2 create a point with 20000 listeners, and track the listeners.
Tab 3 creates a point with 20000 listeners, but doesn't track them.
Stage 1 is after they've got the listeners.
Stage 2 is after they've dropped something (tab 1: point, 2: listeners, 3: point)
Stage 3 is after they've dropped the other (tab 1: listeners, 2: point, 3: no change (listeners dropped implicitly at stage 2))
Tab PID Memory (live, K) at stage...
1 2 3
1 4643 18194 16026 8265
2 4659 18174 8155 8227
3 4676 17750 8152 8202
內存使用情況是荒謬很難跟蹤,部分(I懷疑),因爲自動垃圾收集器的。爲了得到這些結果,我使用了chrome內置的任務管理器,並且在實驗的每個主要步驟之後,循環大量內存,然後停止使用以強制垃圾收集器的手。
這裏的一個好奇是,當標籤1掉落點時,它丟失了大約2000K的內存,而當標籤2下降時,它增加77K(考慮到內存使用的波動性,可以忽略不計)。我懷疑這是因爲標籤1下降了一個點與20000聽衆附加,而標籤2下降一個裸點。
這是我使用的源代碼。我不提供小提琴鏈接,因爲小提琴只是使跟蹤內存使用更加困難:/
HTML(插入自己的API密鑰):
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
html, body {height: 100%; width: 100%;}
#map {width: 100%; height: 80%;}
</style>
</head>
<body>
<h1>Welcome to MapLand, home of a single map.</h1>
<div id="map"></div>
<button onclick="addPts(true);">add points (store listeners)</button>
<button onclick="addPts(false);">add points (don't store listeners)</button>
--------
<button onclick="deletePts();">delete points</button>
<button onclick="deleteLs();">delete listeners</button>
--------
<button onclick="checkListenerVar();">check listener var</button>
<button onclick="cycleLargeChunk();">cycle large chunk</button>
</body>
<script src="test2.js"></script>
<script async src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAorBo-zMFJo9viqdv3pkuhMyg5hytpbaQ&callback=initMap"></script>
</html>
JS(test2.js):
var uluru = {lat: -25.363, lng: 131.044};
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: uluru
});
}
var ptLat = uluru.lat, ptLng = uluru.lng;
var pts = []; // points
var ls = []; // listeners
var listenerVar;
function addPts(storeLs) {
var m = new google.maps.Marker({
position: {lat: ptLat, lng: ptLng},
map: map
});
ptLng += 1;
if (ptLng > uluru.lng + 20) {
ptLng = uluru.lng;
ptLat += 1;
}
pts.push(m);
for (var i = 50000; i > 0; i--) {
var l = google.maps.event.addListener(m, 'click', function() {listenerVar = i;});
if (storeLs) ls.push(l);
}
}
function deletePts() {
for (var i = pts.length-1; i >= 0; i--) pts[i].setMap(null);
pts = [];
ptLat = uluru.lat;
ptLng = uluru.lng;
}
function deleteLs() {
for (var i = ls.length-1; i >= 0; i--) ls[i].remove();
ls = [];
}
function checkListenerVar() {
alert("Listener var is " + listenerVar + ", but is being reset to -1.");
listenerVar = -1;
}
function cycleLargeChunk() {
var l = [0];
for (var i = 0; i < 26; i++) {
l = l.concat(l);
}
//Force the environment to hang on to l for at least a second.
setTimeout(function() {console.log(l.length);}, 1000);
}