Create and dismantle a dynamic Three.js scene with memory management efficiency

Imagine a scenario where we have a webpage with two buttons: create and destroy. By clicking the create button, the Three.js scene in this memory test is dynamically added to the page and starts running. On the other hand, clicking the destroy button should remove the scene, deallocate all buffers, and free up memory resources.

Is there a way to achieve this without using frames or altering the URL?

Thank you.

Answer №2

In the world of programming languages, JavaScript stands out as a garbage-collected one. This means that when you no longer need a specific object, such as an old scene, the memory it occupies will be cleared up unless there is a glitch in the system. From what I can tell, the page you referenced appears to be functioning properly.

Answer №3

I struggled with this issue for a while, reaching out to Chrome and Three.js with bug reports but finding no solution.

It appears that memory was not being released even after an extended period of time, indicating that something was still referencing the memory block and preventing the garbage collector from freeing it up.

Fortunately, I discovered a workaround that proved effective for me:

  1. Start by creating an array to store all items added to the scene.
  2. Whenever you add a new item to the scene, be sure to also add it to this array.
  3. When it's time to destroy an item, use `scene.remove('item name')` to remove it from the scene.
  4. Finally, iterate through the array and manually set all items to undefined.

By following this approach, I successfully reclaimed over 600MB of RAM from my Three.js scene.

UPDATE:

In response to Mr. Doob and WestLangley's suggestion in this thread about memory leaks in Three.js when handling numerous shapes, I have yet to test their advice with my own code.

If you are using WebGLRenderer, consider adding the following steps after removing a mesh with `scene.remove( mesh )`:

- Deallocte the memory with `renderer.deallocateObject( mesh );`

- You can also deallocate textures with `renderer.deallocateTexture( texture );`

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

Refresh the three.js Raycaster when the window size changes

Currently, I am implementing a ray caster to choose objects within my three.js scene. The specific objects I am working with are box geometries shaped like flooring. After a successful selection of an object, I encountered an issue where resizing the wind ...

Laravel 4 Data Cleaning for Improved Security

I am currently utilizing Laravel 4 to establish a RESTful interface for my AngularJS application. Right now, I am looking to update an object within my model named Discount Link. The method I am using involves: $data = Input::all(); $affectedRows ...

Recursion using Node.js Promises

I'm facing some difficulties while iterating through my Promises and completing my parser code: let startFrom = 0, totalRecords = 10000, doneRecords = 0 const rows = 10 const parserRequests = function () { if (startFrom <= totalRecords) { ...

html5 video: experiencing sluggish loading

Initially, I came across some topics with a similar title while searching for answers but could not find a satisfactory solution. Currently, I am experimenting with the auto-loading of HTML5 video on Android and iPad devices. It seems like auto-load featu ...

Unraveling Vue Async Components - Harnessing the power of emitted events to resolve

I am looking to create a Vue async component that stays in a loading state until a custom event is triggered. This means it will render a specified loading component until the event occurs. Here's an example of how I want it to work: const AsyncComp ...

looking to restrict the interface to only specific types

Currently working with TypeScript and I have a query regarding the utilization of TypeScript interface. Is there a way to selectively extract and output specific key types from the interface being used? While I am able to isolate the type I need, I am inte ...

What could be causing the empty object return from the Async function in my Typescript code on Next JS?

Encountering issues with an async function. In the ../lib folder, I have a class for handling data from an API website. However, when attempting to load the API data within an async function, I encounter difficulties. The async function does not return a ...

Fixing issue of Rails AJAX causing partials to be overwritten upon page refresh

While trying to implement a user show view with dynamic post creation, I encountered an issue when refreshing the page after successfully creating posts. The issue arises when the previously created posts are overwritten with null IDs. Additionally, I am u ...

Getting YQL financial information using jQuery and showing it using the .html() method

Struggling with the .html() function and YQL data pulled issue. Despite data being pulled successfully (as seen in the YQL console), it is not being displayed. Here's the provided code snippet: HTML Section <ul id="TECO-container"> <li ...

Can an inner promise in JavaScript not be caught by another promise?

When using promises, I am encountering an issue where the "catch" doesn't seem to catch its child promises. This results in me having to include two instances of the "catch" block. Facility.userHaveAccess(locationObject.created_by, locationObject.fac ...

The Shell Application is not refreshing React HtmlElement Micro Front end

I am currently facing an issue when trying to inject the following React MFE into another Angular shell application. The MFE loads successfully the first time, but if it is hidden or removed from the DOM and then reloaded, it fails to load. Could you plea ...

Is it feasible to completely erase the coding history of a website that has been indexed

Is it possible to completely erase the code-history of a website once it has been indexed by Google and other search engines? I have some content on a website that I want to remove from history, but I'm not sure if it's possible due to factors li ...

Issue: unhandled exception: Marketplace URI is not valid

It seems like I am the first one in this community to ask a question related to balancedpayments. I recently started working with balancedpayments. Here's what I've done so far: Set up a test marketplace Added a "webhoo ...

Issue with Bootstrap 4 nav bar where the first nav item is not highlighted and a problem with scrollspy

I have encountered an issue that I need help resolving. My Bootstrap 4 navbar has scrollspy enabled to update as you navigate through the page's sections, and a jQuery function provides smooth scrolling when clicking on navigation links. However, I am ...

How to make an entire video clickable on Android for seamless playback?

I have implemented an HTML5 video in my mobile web application. Currently, users need to click the small play icon at the bottom left of the video to start playing it. Is there a way to make the entire video clickable so it plays when clicked anywhere on t ...

Is there a way to link dynamic server fields with VueJS?

How can I bind data posted by the server with Vue.js in order to display the data instead of field names? <script> module.exports = { data: function () { return { filedNameFromServer: ['{{filed1}}' ...

What is the best way to dynamically assign a value to an anchor tag upon each click using jQuery?

How can I pass a value to an anchor tag on every click using jQuery? I am encountering an issue with my current code where the value is not being retrieved when I click the button. //HTML snippet <button id="a-selectNm" data-a_name="<?php echo $row[ ...

Tips for parsing information from a text file and displaying it on a website in table format

Would like to create a 2x4 table where the first column has a dropdown for selection. Upon choosing an option, the associated data will populate the other 3 columns. I'm new to this, so please bear with me. Using x, y, z as placeholders, the data wil ...

Pressing the up arrow in Javascript to retrieve the most recent inputs

Is there a way to retrieve the most recent inputs I entered in a specific order? For example: I have an array with 20 elements, and every time I enter something, I remove the first element from the array and add the new input at the end. So, when I press ...

Error Encountered in Reactjs: TypeError When Looping Through State Array to Render JSX Component

Currently in the process of constructing a gallery view showcasing cards that are populated with images fetched through an axios API call. The API call is made within a useEffect hook and the resulting object is then stored using useState. However, when ...