Memory Leak in LineGeometry
See original GitHub issueDescribe the bug
Calling the setPositions
function of LineGeometry or LineSegmentsGeometry multiple times results in a memory leak that can crash the application. This happens because every time its called it creates a new Float32Array, InstancedInterleavedBuffer and two InterleavedBufferAttribute, instead of reusing the old ones.
To Reproduce
Steps to reproduce the behavior:
- Create a scene with ~400 Line2s
- During every frame, call
setPositions
on their geometry to update them - Let it run for some seconds
- The entire browser freezes and the WebGL context gets lots
Code
import * as THREE from "https://unpkg.com/three/build/three.module.js";
import { Line2 } from "https://unpkg.com/three/examples/jsm/lines/Line2.js";
var lines, renderer, scene, camera;
// Increase this number until it crashes (100 is enough for firefox)
const n = 5;
init();
animate();
function init() {
renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(window.devicePixelRatio);
document.body.appendChild(renderer.domElement);
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(
40,
window.innerWidth / window.innerHeight,
0.1,
10000
);
camera.position.set(0, 0, 4);
lines = new Array(n).fill(0).map(() => new Line2());
lines.forEach((line) => scene.add(line));
}
function animate(t) {
requestAnimationFrame(animate);
lines.forEach((line, i) => {
line.material.resolution.set(window.innerWidth, window.innerHeight);
const x = Math.cos(t + i / 20) * 2;
const y = Math.sin(t + i / 20) * 2;
line.geometry.setPositions([x, y, 0, x * 2 * (i / n), y * 2 * (i / n), 0]);
});
renderer.render(scene, camera);
}
Live example
Expected behavior
Not crash, reusing the same attributes.
Screenshots
Not necessary.
Platform:
- Device: [Desktop]
- OS: [MacOS]
- Browser: [Chrome, Firefox]
- Three.js version: [dev, r143]
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:5 (2 by maintainers)
Top Results From Across the Web
memory leak when updating a <Line/> #414 - pmndrs/drei
There is memory leak happend when updating a drei line by time. here by a example: It's a observable component, when planning trajectory ......
Read more >Three.JS VRAM memory leak when adding removing THREE ...
I have encountered a VRAM memory leak in my app. The app adds and removes THREE.Geometry very often to create a volumetric animation....
Read more >Possible memory leak - Esri Community
Hello, I've been using the ArcGIS Pro SDK to write an application which updates a Geodatabase. It seems to be accumulating a lot...
Read more >LineGeometry Class - Typedescriptor
LineGeometry Class ... Constructors. LineGeometry() Initializes a new instance of the LineGeometry class. ... Transform animation bug + memory leak #3394.
Read more >Add/Remove scene object geometry memory leak related
It seems that adding and removing Meshes in THREE.js from a scene, creates a memory leak in VRAM. This seem to be related...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I did that in my solution, but still it seems an abstraction leak having to rely on mutating the buffers directly, when a method could do that for you.
Also I couldn’t find any info anywhere in the docs about the fact that “set” methods are intended to be called just once. What about calling it
setInitialPositions
or something like that, and adding a methodupdatePositions
to do this without crashing the browser? I’m seeing a lot of code online that is callingsetPositions
every frame without knowing this detail, at least a comment could help.For anyone interested, this is how I’ve done it:
Related #21488