Changing "levels": Would you re-initialize the renderer or dispose of every resource?

I’m currently (still after years) in the process of building a game and right now tinkering with the idea of re-initializing the entire renderer when changing levels. Each level consists of “components” and a wide assortment of meshes, materials, etc. with some of them being unique to that level.

For as long as I can remember working with Three, there have been several questions about “how to properly dispose of objects”. Textures or geometries may reside in memory after an object has been removed from the scene. This made me wonder if it would be feasible to just re-initialize (recreate) the WebGLRenderer every time a level will load? The average time the player spends in one level varies from a couple of seconds to a few minutes tops.

Right now every component in my engine has a dispose() method and it is each components’ own responsibility to clean up/dispose of every resource it allocated, either geometry, materials, or other things unrelated to the renderer.

I’m wondering how you would approach this situation? Would it be safer (avoiding accidental memory-leaks) to just re-initialize the WebGLRenderer every time a level loads, or is meticulously disposing of every resource manually really the way to go? I’m already pretty far into development, and its a pretty big refactor to change the current behavior of disposing everything one-by-one.

Since I’m unsure of the caveats of either method, I’m really interested in your thoughts about this and especially the reasoning behind it.

In my experience - what you’re doing is the right approach. If you nuke the renderer - it will impact things outside of your levels/scenes, stuff like a simulator that runs dependent on renderer, or postprocessing stack. But the main issue is re-initialization in my opinion, whenever you switch levels - you’d have to recompile all shaders, and reupload all of the texture data to the GPU. That’s potentially a lot of work and it would hurt your load times a lot.

So, yeah, doing memory management is a pain, but it’s worth it :slight_smile: