Remove material instance after use?

Hi, I’m creating dynamic grid meshes in my level based on the user click location. I’m changing their material color from blueprint based on the distance from the player. I create material instances as soon as I spawn these grid meshes, with different vector parameters for color. Later when the user clicks on a new location, I destroy these meshes. What I want to know is whether these material instances will get removed from memory when I destroy the mesh actors? Or do I have to remove them explicitly in some way?

Here’s a screenshot of what I’m doing in the blueprint:

Hey Stormrage256 -

Assuming that you are just deleting the mesh and not the whole blueprint that has created the MID (Dyanmic Material Instance) then you will still have the MID in memory but unreferenced. It should not hurt anything as it is part of the memory footprint of the Blueprint itself. Of course, it would also not hurt anything to remove the MID either. Typical garbage cleanup would take care of the MID if you removed the entire blueprint.

Hopefully that helps you out -

Eric Ketchum

Is there any way to remove the dynamic material instances from memory without removing the blueprint? I’ve had situations where there could be a lot of these created… not sure I like the idea of them sitting there unreferenced.

I was also worried by this Eric’s statement: “you will still have the MID in memory but unreferenced”. Characters in my project can swap materials hundreds of times during their life cycle, and there can be dozens of characters on the map, which would leave significant and unnecessary memory footprint.

Maybe we misunderstood Eric in some way, but that statement appears to be false. Though I haven’t found a way to delete a MID manually, I have just checked and can confirm that MIDs follow general UObject garbage collection rule: as soon as they’re not referenced by anything, they get GC’ed and deleted. No need to do anything special.