On memory consumption and mesh disposal

I am trying to make a MMORPG. Being an MMORPG i have a big issue on quantity of avatars on screen. I already created a method of creating avatars when i am close and removing it when i am not looking.

I created a FBX file of an avatar that consists on 24 skinnedmeshes connected to a skeleton.
When i loaded the FBX file i had this situation on memory:
before avatar
memory-56525020 (from window.performance.memory.usedJSHeapSize)
textures-0
geometries-0
calls-0
triangles-0

After making a clone() of this loaded group and removing 11 pieces of clothes from the original file (with methods removeFromParent() and clear() on each cloth i wanted to remove) i had this situation:

after avatar
memory-58392564
textures-13
geometries-13
calls-13
triangles-12384

this is nice. I managed to get from the 24 meshes i had on my file only 13 on render.

Issue came when i tried to delete this 13 objects from render. Here is my code:
removeAvatar:function(){
if (this._threeObj){
this._threeObj.traverse(object => {
if (!object.isMesh) return

			console.log('dispose geometry!')
			object.geometry.dispose()
		
			if (object.material.isMaterial) {
				console.log('dispose material!')
				object.material.dispose()
			
				// dispose textures
				for (const key of Object.keys(object.material)) {
					const value = object.material[key]
					if (value && typeof value === 'object' && 'minFilter' in value) {
						console.log('dispose texture!')
						value.dispose()
					}
				}
			} else {
				// an array of materials
				for (const material of object.material) {
					console.log('dispose material!')
					object.material.dispose()
				
					// dispose textures
					for (const key of Object.keys(object.material)) {
						const value = object.material[key]
						if (value && typeof value === 'object' && 'minFilter' in value) {
							console.log('dispose texture!')
							value.dispose()
						}
					}
				}
			}
		})
		this._threeObj.clear()
		this._threeObj.removeFromParent () 

	}
},

Notice i dispose of every geometry and every material.

Still…after this procedure i get this result:

after avatar removal
memory-60624016
textures-13
geometries-13
calls-0
triangles-0

I have an increase of memory consumption and it didnt remove geometries or textures from render.
Notice: MY FBX dont have textures. I checked each material and it didnt have textures in it…only colors.

So whats wrong here?

Two adicional information on the issue…

1- I noticed that for each clone i make from the original FBX file object, I geometries dont change, but textures is increased. So if i have 2 clones i have 13 geometries, 26 textures. 100 clones is 13 geometries 130 textures.

2- I decided to make a delay after removal of the avatar of 60 seconds. Noticed the memory was reduced (probably cause i gave time for garbage collector to work) but the geometries and textures didnt change.

Skeletal animations are controlled using textures - the newly created texture for each clone represents bone transformations for the new instance.

So how do i remove the textures of the old bone transformations so that it dont keep increasing forever?

Be sure to dispose the skeleton if you aren’t doing that already (and make sure to dispose stuff only after the object is removed from the scene - this applies to all materials, textures, and skeleton bindings.)

2 Likes

thank you i will try that right away

Man it solved. Thank you a lot. The issue was solved after i removed the object from scene and disposed all skeletons.

1 Like

nice. TIL.