Jump to content

What's the best/recommended way for memory management in BJS?


heyzxz
 Share

Recommended Posts

Hi again, 

Well as it shows in the doc, most of the BJS objects (meshes, materials, textures...) need a 'scene' parameter in their constructor function, I guess the main purpose of this design is making the memory management clearer and easier,  so the scene takes fully control of the memory management for all stuffs, when you leave a scene, all you need is simply calling a 'scene.dispose' and it will clear/release everything for you, right?

But I have some questions about this:

1. Does this mean when designing a BJS game, it's NOT recommended to share objects(especially model objects ) with multiple scenes?  For example, player's game properties, a gun. What is the correct/recommended way to manage it? Re-instantiate the gun in each scene? or holding the gun(model) out of scene and share it with other scenes?

2. How to deal with the later-loading objects? I mean when calling 'scene.dispose',  what if there are still something in the loading?? does the 'scene.dispose' takes care of them? It looks not... Please see this PG: http://www.babylonjs-playground.com/#280KRP#1, there is a texture still in loading when calling the 'scene.dispose', then, you'll see some webgl warnings and also you'll find the onload callback for the texture is triggered (which shouldn't be)...   

Screen-Shot-2017-03-24-at-12_10.38-AM.jpg.580d9e683b6f83e4912c14a2b917e414.jpg

A similar example:  What if dispose the scene when there is a  'BABYLON.SceneLoader.ImportMesh...'  NOT finished...

How do I deal with these cases?

Thank you! 

 

 

Link to comment
Share on other sites

Hey!

You are right :) The scene oriented approach is here to help managing resources.

1. My recommendation would be to have a "holding" scene where your objects belong and when you want to use them in a specific scene you can then use addMesh/removeMesh to move your mesh (You will also have to do it for the materials)

1'. You can also think about a big scene where you have hidden objects that can be used only when needed

2.As you found out this is not supported by scene.dispose but I will fix it for next commit (Checking if scene is disposed in the importMesh callback)

Link to comment
Share on other sites

On 3/24/2017 at 0:41 AM, Deltakosh said:

Hey!

You are right :) The scene oriented approach is here to help managing resources.

1. My recommendation would be to have a "holding" scene where your objects belong and when you want to use them in a specific scene you can then use addMesh/removeMesh to move your mesh (You will also have to do it for the materials)

1'. You can also think about a big scene where you have hidden objects that can be used only when needed

2.As you found out this is not supported by scene.dispose but I will fix it for next commit (Checking if scene is disposed in the importMesh callback)

Hi again @Deltakosh,

I followed your #1 suggestion: create a holding scene to hold all the shared stuffs (game properties), and then use addMesh/removeMesh to move them to others scenes, but unfortunately I got another problem :(

The rendering for materials (checked for StandardMaterial, PBRMaterial, but I guess other things such as Meshes, Textures..may still have this problem) are based on their own scenes, which means if an object belongs to SceneA, then I add it to SceneB( by using 'addMesh' ), when rendering this object in the sceneB, it still access SceneA's properties ( object.getScene(), object.getScene().activeCamera... )  to do its render, which is wrong in my case.  And I didn't find a 'public way' to change its '_scene' property.

https://github.com/BabylonJS/Babylon.js/blob/master/src/Materials/babylon.pbrMaterial.ts#L1334

https://github.com/BabylonJS/Babylon.js/blob/master/src/Materials/babylon.shaderMaterial.ts#L250

Any ideas ?

Link to comment
Share on other sites

Yeah that's hacky... I think I probably won't do this for now as I'm targeting on a long-term project,  hacky ways may bring potential risks in the future...

So I'm now giving up caching shared BJS objects and choose to create/dispose them in every scene...

But I really like your 'holding scene' idea, and I hope BJS can think about it and some day can provide an official way to support it. Because it's a great way to keep the memory more stable, which can lead to much less GC rounds of the browsers than frequently creating/disposing things (as we know the GC round somethings is the killer for the performance)

Link to comment
Share on other sites

Actually we could do an experiment: does it work when you change the mesh_scene? Because you may also need to change the material._scene in the same way. And if your mesh is referenced by a light, shadows, or an instance then everyone has to move.

The more I think about i, the more I think we should not do it :)

And what about having only one scene and playing with mesh.clone? It is acceptable in your case?

Link to comment
Share on other sites

22 minutes ago, Deltakosh said:

Actually we could do an experiment: does it work when you change the mesh_scene? Because you may also need to change the material._scene in the same way. And if your mesh is referenced by a light, shadows, or an instance then everyone has to move.

Yes, agree.

23 minutes ago, Deltakosh said:

And what about having only one scene and playing with mesh.clone? It is acceptable in your case?

Not very sure what do you mean by 'mesh.clone' do you mean just use only one scene and create(may be clone if possible)/dispose game stuffs in that scene? Yes, I'm currently doing this way :) 

Thank you.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...