CesiumGS / cesium

An open-source JavaScript library for world-class 3D globes and maps :earth_americas:
https://cesium.com/cesiumjs/
Apache License 2.0
12.82k stars 3.46k forks source link

when we use viewer.destroy after viewer.flyTo(some entity or dataSource),will cause memory leaks #8378

Open itfsw opened 4 years ago

itfsw commented 4 years ago

Sandcastle example: Example Browser: Chrome

  1. when use zoomTo will never case;
  2. use flyTo will like this: QQ截图20191108182611
  3. we guess something happened at scene.postRender -> updateZoomTarget -> viewer._zoomIsFlight -> camera.flyToBoundingSphere -> cancel when viewer.destroy so we try
    viewer.flyTo(xxx).when(()=>{
    viewer._zoomIsFlight  = false
    })

    before use destroy, it still cannot free entity object,so it may happend when use flyTo method

OmarShehata commented 4 years ago

Thanks for the Sandcastle @itfsw . I can see by capturing a heap snapshot, with the zoomTo and without the zoomTo, after they complete, that in one case if you filter for "Entity" you'll see the memory still exists, and the other it does not.

I think this is worth looking more into.

lLeiyi commented 4 months ago
Cesium.TweenCollection.prototype.update = function (time) {
  const tweens = this._tweens;

  let i = 0;
  time = defined(time)
    ? time / TimeConstants.SECONDS_PER_MILLISECOND
    : getTimestamp();
  while (i < tweens.length) {
    const tween = tweens[i];
    const tweenjs = tween.tweenjs;

    if (tween.needsStart) {
      tween.needsStart = false;
      tweenjs.start(time);
    } else if (tweenjs.update(time)) {
      i++;
    } else {
      tweenjs.stop();
      tweenjs._group.remove(tweenjs); //important
      tweens.splice(i, 1);
    }
  }
};

I find in TweenCollection.update tweenjs.stop(); can not clear _group, then _group will continue to grow and there is no way to release. _group retains the reference chain of the entity. So I manually cleared _group.