Dealing with memory leaks in three.js can be a common issue, especially when utilizing the

In my three.js project, I am constructing a Geometry and filling it with vertices to create a 2D terrain. Once the terrain is generated, I am adding all the Vector3 and Face3 elements to the geometry, and then adjusting each vertex and face on every frame update.

Since I am dynamically modifying the face vertices, I need to notify three.js to update the faces. This is achieved by setting

geometry.elementsNeedUpdate = true
. However, I have observed that this approach leads to a significant increase in memory consumption (approximately an additional ~50mb of RAM usage per second).

Below is a snippet illustrating my implementation:

function pushEverything(geom) {
    for (var i = 0; i < 10000; i++) {
        geom.vertices.push(new THREE.Vector3(...));
        geom.faces.push(new THREE.Face3(...));
        geom.faces.push(new THREE.Face3(...));
    }
}

function rebuild(geom) {
    for (var face of geom.faces) {
        face.a = ...
        face.b = ...
        face.c = ...
    }
    geom.elementsNeedUpdate = true
}

var renderer = new THREE.WebGLRenderer({
        canvas: document.getElementById("my-canvas")
});
var geom = new THREE.Geometry();
var camera = new THREE.PerspectiveCamera(...);
pushEverything(geom);
while (true) {
    // Perform some terrain modifications
    rebuild(geom);
    renderer.render(geom, camera);
    sleep(1000 / 30);
}

Following the guidance provided in this question, I have tried switching to geometry.vertices[x].copy(...) instead of

geometry.vertices[x] = new Vector3(...)
.

My main concern is: why does enabling

geometry.elementsNeedUpdate = true
result in such high memory usage? Are there any alternative approaches for updating a Geometry's faces?

I am utilizing three.js version 0.87.1 from NPM.

Answer №1

The problem has been identified and resolved. It wasn't a memory leak caused by three.js, but rather one that I created myself.

The issue stemmed from creating a Geometry, cloning it, making modifications to the clone, and then merging it back into the original. It turns out that failing to call geometry.dispose() on the cloned geometry was causing the memory leak. Essentially, I was cloning the geometry every frame, leading to a significant increase in memory usage.

To remedy the situation, I decided to convert the Geometry to a BufferGeometry and properly dispose of it by calling geometry.dispose() when no longer needed. This adjustment has now resulted in the expected memory usage levels.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Implementing event handlers with 'v-on' on dynamically inserted content using 'v-html' in Vue

Can Vue allow for the addition of v-on events on strings included in v-html? In this scenario, clicking the 'Maggie' link doesn't produce any action. It appears that it's not recognized by Vue. Is there an alternative method to achieve ...

Tips for Including a Parallax Image Within a Parallax Section

Currently, I am working on implementing a parallax effect that involves having one image nested inside another, both of which will move at different speeds. My progress has been somewhat successful, however, the effect seems to only work on screens narrowe ...

Execute a query in Firestore with a "where" filter

I am trying to filter and retrieve specific items based on the categoryID using the "where" method and the "==" operator. In my Firestore collection named "task", each user has a document with an array of different tasks. Here is the structure: Click here ...

What steps should be taken to retrieve the contents of a file that has been chosen using the browse

You have successfully implemented a browse button that allows the user to navigate the directory and choose a file. The path and file name are then displayed in the text element of the Browse button complex. Now, the question arises - how can I extract dat ...

Refresh the page when the parameter in the URL changes

I'm encountering a small issue that I can't seem to resolve. Currently, I am on the following page: http://example.com:8080/#/user/5ad142e8063ebb0537c5343e There is a link on this page that points to the URL below: http://example.com:8080/#/u ...

Discover the way to retrieve PHP SESSION variable within JavaScript

I'm currently working on developing a platform for image uploads. As part of this project, I assign a unique identifier to each user upon login using $_SESSION['id'] in PHP. Now, I am looking for a way to verify if the $_SESSION['id&apo ...

Having trouble transmitting data with axios between React frontend and Node.js backend

My current challenge involves using axios to communicate with the back-end. The code structure seems to be causing an issue because when I attempt to access req.body in the back-end, it returns undefined. Here is a snippet of my front-end code: const respo ...

The function queryDatabases is not supported in the DocumentDB JavaScript API

Currently, I am developing a service for Azure Functions using JavaScript/Node.js. However, I encounter an error when trying to access the function DocumentClient.queryDatabases. Despite having the correct references installed in Visual Studio Code and bei ...

Determine the percentage of clicks on an HTML5 canvas radial progress bar

Recently, I designed a circular progress bar displaying a specific percentage. However, I am facing a challenge in determining how to calculate the percentage when a user clicks on either the black or green part of the circle. Could you provide insight on ...

Javascript - accessing a local file (located in the same directory as the HTML file, not an uploaded file)

I have been attempting to find a solution, but have had no success. Is there a way to read a file using JavaScript or HTML? I have a text file named "sample.txt" with some information in it. This file is located in the same folder as the HTML file that con ...

Reinitializing the CanvasRenderingContext2D transform with resetTransform and harnessing the power

I've encountered an unexpected issue with the Google Closure Compiler in ADVANCED mode, and I'm struggling to understand it: it's renaming the function resetTransform of CanvasRenderingContext2D Unfortunately, I can't directly share th ...

Adding objects to an existing array in Vue.js can be a seamless and

Struggling to populate my existing array with elements from a JSON response using a button click. The issue lies in properly adding these elements to the array. I have an empty array named results where I store the data from the response. export default ...

Steps for choosing an image and embedding it within a div element

Upon loading the site, an image is displayed with the following code: <img id="some_Image" src="some_source"> However, I am looking to avoid requesting this image again from "some_source". This is because calculating the image can be resource-inten ...

JavaScript's ability to show and hide div elements functions properly in Internet Explorer, but is encountering issues in Firefox and Chrome

I have a total of 20 spans with unique IDs, all set to hidden in my stylesheet. To toggle the visibility of these spans upon clicking on sections of an image map, I created this script: function showDiv(pass) { var divs = document.getElementsByTagName ...

Issue with setInterval function execution within an Angular for loop

My goal is to dynamically invoke an API at specific intervals. However, when attempting to utilize the following code snippet in Angular 7, I encountered issues with the interval timing. I am seeking a solution for achieving dynamic short polling. ngOnIn ...

Equal size images displayed within cards in Material UI

Is there a way to create a list of Material UI components with images that have uniform height, even if the original images vary in size? I want to make all image heights responsive and consistent across all cards. Any suggestions on how to achieve this? ...

JavaScript code to generate a UTF8 string from UTF codes

I am in possession of the byte representation of UTF8, such as: 195, 156 for "Ü" (capital U Umlaut) I am struggling to generate a JavaScript-compatible string from these numbers - all my attempts have been unsuccessful. Every method I have tried has mis ...

Having trouble loading the JSON data for display

My select dropdown is supposed to display the names of states. The data is being fetched from PHP and is also available in the controller JS file. However, the data is showing up as blank in the HTML. When I use console.log(), the fetched result from PHP s ...

Exploring the File Selection Dialog in Node.js with TypeScript

Is it possible to display a file dialog in a Node.js TypeScript project without involving a browser or HTML? In my setup, I run the project through CMD and would like to show a box similar to this image: https://i.stack.imgur.com/nJt3h.png Any suggestio ...

Obtaining the current date and time of the user's browser within a server-rendered file

I need to capture the browser/local date and time in a PDF file. The challenge I'm facing is that the PDF file rendering occurs on the server, which means I can't use JavaScript to achieve this as the rendering process is server side. Can anyone ...