Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Threejs内存泄漏 #57

Open
veciolin opened this issue Jan 13, 2025 · 6 comments
Open

Threejs内存泄漏 #57

veciolin opened this issue Jan 13, 2025 · 6 comments

Comments

@veciolin
Copy link

Hi,

不得不说您的整体方案非常完善,写得非常好。

我发现在Home的index.vue中,onmount会去创建对应的threejs的效果,但是在onUmounted有一些没有释放。
如果打开页面后,频繁多次切换会有内存泄漏,整个浏览器会比较卡。可以合入了类似的cleanup


const cleanup = () => {
  animationRunning.value = false;

  clearIntervalTimer();

  if (scene.value) {
    scene.value.traverse((object: THREE.Object3D) => {
      if ((object as any).material) {
        if (Array.isArray((object as any).material)) {
          (object as any).material.forEach((material: THREE.Material) => {
            material.dispose();
          });
        } else {
          (object as any).material.dispose();
        }
      }
      if ((object as any).geometry) {
        (object as any).geometry.dispose();
      }
      if ((object as any).texture) {
        (object as any).texture.dispose();
      }
    });
    scene.value.clear();
  }

  if (objects.value) {
    objects.value.forEach((object) => {
      if (object.element) {
        object.element.remove();
      }
    });
    objects.value = [];
  }

  if (controls.value) {
    controls.value.removeEventListener("change", render);
    controls.value.dispose();
  }


  scene.value = null;
  camera.value = null;
  renderer.value = null;
  controls.value = null;
};

我测试后,应该是基本没有泄漏的问题。另外我尝试了下用tauri打包了下,功能也基本没问题,有需要感觉可以也加入,当然我看您也加了Docker,也很方便。

@LOG1997
Copy link
Owner

LOG1997 commented Jan 13, 2025

好的,感谢,写的时候都没注意

@kentge
Copy link

kentge commented Jan 15, 2025

function animation() { TWEEN.update(); controls.value.update(); // 设置自动旋转 // 设置相机位置 requestAnimationFrame(animation); }
我想问一下,requestAnimationFrame这个是递归调用的,如果不取消,是不是也会造成内存泄漏

@veciolin
Copy link
Author

requestAnimationFrame

我的理解是要,这个得记录下animate_id,在退出的时候调用cancelAnimationFrame.

@kentge
Copy link

kentge commented Jan 15, 2025

requestAnimationFrame

我的理解是要,这个得记录下animate_id,在退出的时候调用cancelAnimationFrame.

okk

@kentge
Copy link

kentge commented Jan 15, 2025

const cleanup = () => {
animationRunning.value = false;

clearIntervalTimer();

if (scene.value) {

加了cleanup后,我发现频繁切换,dom节点还是会稳步增加,是不是还有什么地方没有处理

@LOG1997
Copy link
Owner

LOG1997 commented Jan 15, 2025

const cleanup = () => {
animationRunning.value = false;
clearIntervalTimer();
if (scene.value) {

加了cleanup后,我发现频繁切换,dom节点还是会稳步增加,是不是还有什么地方没有处理

是的,我还在排查。昨天加班还没时间弄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants