Using ThreeJS in an iframe on a mobile device can lead to crashes in the browser due to multiple

I am encountering an issue with my ThreeJS based pages that are included via Iframe into other pages. The problem arises when switching between the two pages with ThreeJS Iframe on a mobile phone (specifically an iPhone 7 with just 1GB RAM). After two or three switches between the two links "Bota" and "Brasna", the browser crashes. Interestingly, if I navigate from a page with Iframe directly to a ThreeJS page (without Iframe) and then back to a page with ThreeJS in Iframe, everything works fine. Similarly, if I go from a page with ThreeJS in an iframe to a vanilla page without ThreeJS and then back to a page with ThreeJS in an iframe, I can do so multiple times without any issues. The problem only occurs when I switch from a page with ThreeJS in Iframe to another page with ThreeJS in an iframe. It seems that the ThreeJS page remains in memory after redirecting to another page when it is in an iframe. I am seeking advice or thoughts on why this behavior is occurring. Any insights would be greatly appreciated.

Answer №1

When dealing with Three.js and having issues with memory management on redirections, it seems to be a common problem with Three.js itself. One solution is to create a custom dispose method that can be called during redirections to properly dispose of all Three.js resources. Below is an example of such a method that I use in my project. Keep in mind that in this example, `this.world` refers to `scene.children[0]`, but you can adapt it to directly traverse the scene if needed.

dispose: async function () {

    //console.log(window.performance.memory);

    return new Promise(disposed => {
        this.world.traverse(function (obj) {
            if (obj.geometry) {
                obj.geometry.dispose();
            }
            if (obj.material) {
                if (obj.material instanceof THREE.MeshFaceMaterial) {
                    obj.material.materials.forEach(function (m) {
                        m.dispose();
                        if (m.map) {
                            m.map.dispose();
                        }
                    });
                } else {
                    obj.material.dispose();
                }

                let m = obj.material;
                let md = (m.map || m.alphaMap || m.aoMap || m.bumpMap || m.displacementMap || m.emissiveMap || m.envMap || m.lightMap || m.metalnessMap || m.normalMap || m.roughnessMap)
                if (md) {
                    if (m.map) m.map.dispose();
                    if (m.alphaMap) m.alphaMap.dispose();
                    if (m.aoMap) m.aoMap.dispose();
                    if (m.bumpMap) m.bumpMap.dispose();
                    if (m.displacementMap) m.displacementMap.dispose();
                    if (m.emissiveMap) m.emissiveMap.dispose();
                    if (m.envMap) m.envMap.dispose();
                    if (m.lightMap) m.lightMap.dispose();
                    if (m.metalnessMap) m.metalnessMap.dispose();
                    if (m.normalMap) m.normalMap.dispose();
                    if (m.roughnessMap) m.roughnessMap.dispose();
                }
            }
            if (obj.dispose) {
                obj.dispose();
            }
        });
        this.scene.remove(this.world);
        this.scene.dispose();
        this.world.children = [];
        this.world = null;
        this.labelRenderer.dispose();
        this.renderer.dispose();
        disposed('dispose finished');
        //console.log(window.performance.memory);
    });
}

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

Simulate a keyboard key being pressed and held for 5 seconds upon loading the page

Is it possible to create a script that automatically triggers an event to press and hold down the Space key for 5 seconds upon page load, without any user interaction? After the 5 seconds, the key should be released. It is important to emphasize that abso ...

Having trouble getting the Vue.js sample app to run using "npm run dev"? The error message "Error: Cannot find module 'node:url'" may

As a beginner in vuejs, I am currently following the steps below to create my vue js app on my local system: Step: npm init vue@latest ✔ Project name: … VueDemo ✔ Add TypeScript? … No / Yes ✔ Add JSX Support? … No / Yes ✔ Add Vue Router f ...

Creating a new image by converting canvas to an image and displaying it in a separate window with Ruby on Rails and

I'm having some trouble with this issue and would really appreciate your help. My goal is to convert a canvas into an image using toDataURL when a link, button, or image is clicked. Once the image is generated, I want it to open in a new window. Can ...

Can you please guide me on retrieving information from an API using HTML, CSS, and JavaScript?

My task is to develop a web application using HTML, CSS, and JS with the following functionality: Retrieve a list of users from this API: https://jsonplaceholder.typicode.com/users. Upon clicking on a user, show a list of albums associated with that user ...

Is it possible for XSS attacks to exploit the HREF attribute when used with jQuery?

Burp suite displaying an error message. The application seems to have a potential vulnerability related to DOM-based cross-site scripting. Data is retrieved from the location and passed to jQuery() using the following statement: jQuery(location). ...

Error: The 'length' property cannot be searched for using the 'in' operator

Hmm, I keep getting an error that says "Uncaught TypeError: Cannot use 'in' operator to search for 'length' in" Every time I attempt a $.each on this JSON object: {"type":"Anuncio","textos":["Probando ...

Illuminating and shading particles in ThreeJS using the Framebuffer Object

An update was made to incorporate a simple lighting solution from a recommended link provided by ScieCode. The first part of the linked page was utilized for this purpose. The outcome of this implementation can be observed in the image located at the end o ...

What are the consequences of submitting a form to a different website through POST method?

Dealing with a CMS that does not offer an easy way to add a NodeJS route for handling form data can be quite challenging. I'm considering the following setup - would this be considered bad practice or unsafe? Server 1 (Ghost CMS) : www.domain.com &l ...

Strange behavior observed when resizing objects in Three.js WebGl

Everything was going smoothly with my code until I decided to enable WebGL. It seems that the function responsible for resizing my object every frame rate stopped working. function animate(){ window.requestAnimationFrame(animate); s.setPositio ...

Tips for customizing the timing of a Bootstrap modal's opening delay?

I have integrated gem "bootstrap-sass", "~> 2.3.0.0", which indicates that I am utilizing Bootstrap 2. Detailed information on the modal can be found here. The code for my custom modal looks like this: #Modal.modal.hide.fade{"aria-hidden" => "true" ...

Unable to get jQuery plugin to function properly within a .vue component

I am currently working on a Vue component for my Laravel 5.3 application. I am trying to integrate the Laravel File Manager into a standalone button, but it seems like it's not functioning properly. When I click on the button to choose an image, nothi ...

What is the best way to transfer an ID to a dropdown selection list?

Whenever a user chooses an option from a drop-down menu, an event needs to be triggered. I have a checkbox that retrieves an ID from the database; based on this ID, a user can be added or removed from the drop-down menu. var ajReq = new XMLHttpRequest(); ...

Trigger a function from a Bootstrap modal

Here is a preview of my HTML code : <form action="form2_action.php" method="post"> <table> <tr> <td>First Name </td> <td>:</td> <td><input type="text" id="f ...

Using onClick function to pass the value of an input textbox in PHP

I have an input text field and I am looking to pass the values entered inside the element through a JavaScript onClick event. <form> <fieldset> <label>Common Site ID: </label><span><?php echo $commonsiteid ?>< ...

Timing of Bindings in AngularJS

In my setup, I have a controller that calls a service to retrieve a list of categories: $scope.enquiryCategories = CategoryServices.listCategories(); The service then fetches this data from an external API: listCategories: function () { return $http({ ...

Encountering difficulty accessing the object from the props within the created method

After retrieving an object from an API resource and storing it in a property, I encountered an issue where the children components were unable to access the object inside the created method. This prevented me from assigning the values of the object to my d ...

Is writeConcern not being returned by MongoDb's deleteOne function?

I have a function that deletes a specific document in MongoDb based on an id match. Below is the code snippet. deletePoll: function(db, user_id, callback) { db.collection('polls').deleteOne({ _id: user_id }, ...

"Utilizing JavaScript to locate a corresponding row in a table within an MVC view

I am currently updating my row using Ajax call and now I want to update the row with the new data without refreshing the page. Right now, I am matching based on DisplayName but I would like to match it with the ID since it's unique and the ID is conta ...

Deciphering the significance of matrices within a Three.js 3D object

Exploring the source code of THREE.Object3D reveals three key properties: matrix, matrixWorld, and matrixRotationWorld. Upon closer inspection, it appears that the object's position, scale, and rotation data can be extracted from the matrix. Furtherm ...

Filter JSON data in AngularJS ng-repeat by a specific ID

I'm having difficulty getting this to function properly. I've been attempting to filter a JSON file by specific ID and then iterate using ng-repeat. Here's what I have tried: This is the button trigger: <a href="#/compare-details">& ...