Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Editor: Memory leak when loading 4096x4096 texture several times #21151

Closed
RealTecWare opened this issue Jan 26, 2021 · 7 comments · Fixed by #21159
Closed

Editor: Memory leak when loading 4096x4096 texture several times #21151

RealTecWare opened this issue Jan 26, 2021 · 7 comments · Fixed by #21159

Comments

@RealTecWare
Copy link

Describe the bug

I tried to edit the MaterialsVariantsShoe.glb file from the KhronosGroup git in the editor and changed the normalMap and aoMap textures several times to a 4096x4096 texture.

I could use the same texture several times and the memory starts to increase after ~9-10 times (in my tests the value of changes sometimes were even smaller).
That causes the webgl context to crash after enough changes.

To Reproduce

Steps to reproduce the behavior:

  1. Go to ThreeJS Editor
  2. Import MaterialsVariantsShoe.glb
  3. Change material to MeshPhysicalMaterial
  4. Set the normalMap / aoMap >10 times to a 4096x4096 texture
  5. The leak begins to grow after every following change

Expected behavior

After my analysis this only happens in 64bit browsers.
The following lines of WebGLState.js causes the increasment.

function texImage2D() {

	try {

		gl.texImage2D.apply( gl, arguments ); //After this function call the memory was increased highly and is only freed after closing or reloading the tab / browser.

	} catch ( error ) {

		console.error( 'THREE.WebGLState:', error );

	}

}

Screenshots

After 30 changes with the same texture:

Browser Taskmanager Image
Vivaldi 32bit image
Vivaldi 64bit image
Chrome 32bit 32bit_30times
Chrome 64bit 64bit_30times

Platform:

  • Device: [Desktop, Mobile]
  • OS: [Windows, Android, IPadOS]
  • Browser: [Chrome, Canary Chrome, Firefox, Safari, Edge, Opera, Vivaldi, Opera GX, Chrome for Android]
  • Three.js version: r117 - r124
@mrdoob mrdoob changed the title Set material several times to 4096x4096 - Memory Leak in 64bit browsers Editor: Memory leak when loading 4096x4096 texture several times Jan 26, 2021
@mrdoob
Copy link
Owner

mrdoob commented Jan 26, 2021

What happens if you load it more than 30 times? Are you able to crash the browser?

@RealTecWare
Copy link
Author

I'm able to crash the webgl context with this, after the memory is over ~8GB (set the normalMap ~90-100 Times in my test).

context_lost

@mrdoob mrdoob added this to the rXXX milestone Jan 26, 2021
@Mugen87 Mugen87 added the Editor label Jan 27, 2021
@RealTecWare
Copy link
Author

I have created a small example, where I simulate the situation. (~53 secounds - ~53 changes to the same texture)

If you use this example without ao change it only change the normalMap. (95 secounds - 95 changes to the same texture)

@Mugen87
Copy link
Collaborator

Mugen87 commented Jan 27, 2021

Can you please call Texture.dispose() every time you replace an existing texture? Meaning:

if ( child.material.aoMap !== null ) child.material.aoMap.dispose();
child.material.aoMap = texture;

@RealTecWare
Copy link
Author

Thanks, that fixes the problem in my example.
Could it be that the editor never dispose the textures that are loaded?

@Mugen87
Copy link
Collaborator

Mugen87 commented Jan 27, 2021

Could it be that the editor never dispose the textures that are loaded?

Yes, I guess this is the problem. A quick search shows that dispose() is never used on a texture.

@RealTecWare
Copy link
Author

The biggest question in my mind is at the end, why does the 64 bit browsers crash and 32 bit don't?
Maybe better garbage collection or less caching on the nativ side, because memory is a bigger thing with 32 bit.

But for now the problem can be solved by disposing and that's all what matters.

@mrdoob mrdoob removed this from the rXXX milestone Feb 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants