We have a large World Partition map where we use Mass Spawners to efficiently represent a wide variety of incidental props with low LOD ISMs that “hydrate” to a high LOD Actor representation based on proximity to a Viewer. We use the pooling mechanism within MassActorSpawnerSubsystem to retain the Actor instances to mitigate spawning costs on the game thread for subsequent high LOD requests for the same class type.
During prolonged play of this map, the Mass actor pools were expanding to include more and more BP classes that have been encountered, and thus our memory footprint grew accordingly (the BP classes and their dependencies - meshes, audio etc.) To counteract this, we track activity of the Mass pools and “flush” them if the pool for a particular BP class hasn’t been used to spawn an instance of that type for a given time threshold. A flush simply destroys all the actor instances in the pool, the theory being that the BP class would no longer be reachable in the object graph and it could be safely GCd to reclaim the memory.
However we noticed that the BP class objects were still not getting destroyed after a pool flush and subsequent GC, which “obj ref” command debugging led us to MassRepresentationSubsystem::TemplateActors. It appears there is no automated mechanism where FMassEntityConfig::DestroyEntityTemplate will be called to manage TemplateActors accordingly. We noted the existence of AMassSpawner::ClearTemplates, and its call in AMassSpawner::UnloadConfig, however this appears to be unused. Furthermore, as we have the possibility that multiple MassSpawners reference the same UMassEntityConfigAsset, we cannot safely call ClearTemplates in AMassSpawner::EndPlay (we tried).
Our only potential viable solution at this moment is to intercept when the UMassEntityConfigAsset itself is being BeginDestroyed and issue a call to DestroyEntityTemplate there, as we’re reasonably sure there is no internal state dependent on it at that juncture. This does have the desired effect - TemplateActors gets appropriately trimmed and the BP class objects get properly GCd - seemingly with no stability or logical implications.
Is this approach advisable? Is there a simpler approach we could take with this? We keep thinking we must be missing something here, but we can’t fathom how TemplateActors would be maintained without manually destroying the entity templates ourselves.
Thanks!
[Attachment Removed]