We’re seeing GC allocations when repeatedly playing a 3D one-shot footstep event through StudioEventEmitter.Play(). Looking at the Unity integration source, each playback creates an FMOD instance and calls RuntimeManager.AttachInstanceToGameObject(). Its FindOrAddAttachedInstance() method allocates a managed new AttachedInstance(). Finished attachments are removed rather than pooled. Is there a supported way to avoid this per-playback allocation while retaining overlapping sounds and transform tracking?
We use this for playing footsteps on enemy creatures, which is something that gets called a lot.
There is not currently a path to avoid that when using the StudioEventEmitter, although you could create your own script following our examples: https://www.fmod.com/docs/2.03/unity/examples-basic.html, either using the FMOD Studio API directly or the RuntimeManager.PlayOneShot.
Thanks Cameron.
I took a closer look and its a fair bit of work to get several systems in our game to avoid the allocations, requiring us to re-do a bunch of logic that is already shipped with FMOD. Would you consider fixing the GC in the FMOD Unity integration? I’ve put together a small, contained change which keeps StudioEventEmitter and AttachInstanceToGameObject behaving exactly as they do now.
Problem: each Play() of a 3D one-shot creates a new EventInstance, and RuntimeManager.AttachInstanceToGameObject → FindOrAddAttachedInstance then does new AttachedInstance() for it. When the event stops, Update() (or DetachInstanceFromGameObject) removes that object from attachedInstances and drops it, so every play of every 3D one-shot emitter leaves one object for the GC.
I would imagine this is something that a lot of people are running into without realising for audio like footsteps, surface impacts and gunshots. Things that play very regularly, and so zero GC allocation here would be desirable.
Solution: keep the removed AttachedInstance objects in a “free” list and reuse them. The list never holds more than the peak number of attached instances, so after warm-up there are zero allocations. Nothing in the public API changes.
--- a/Assets/Plugins/FMOD/src/RuntimeManager.cs
+++ b/Assets/Plugins/FMOD/src/RuntimeManager.cs
@@
private List<AttachedInstance> attachedInstances = new List<AttachedInstance>(128);
+ private Stack<AttachedInstance> freeAttachedInstances = new Stack<AttachedInstance>(128);
@@
+ private void RecycleAttachedInstance(AttachedInstance attachedInstance)
+ {
+ attachedInstance.instance.clearHandle();
+ attachedInstance.transform = null;
+ #if UNITY_PHYSICS_EXIST
+ attachedInstance.rigidBody = null;
+ #endif
+ attachedInstance.lastFramePosition = Vector3.zero;
+ attachedInstance.nonRigidbodyVelocity = false;
+ #if UNITY_PHYSICS2D_EXIST
+ attachedInstance.rigidBody2D = null;
+ #endif
+ freeAttachedInstances.Push(attachedInstance);
+ }
+
private class AttachedInstance
{
@@ private void Update()
if (playbackState == FMOD.Studio.PLAYBACK_STATE.STOPPED ||
attachedInstances[i].transform == null // destroyed game object
)
{
+ RecycleAttachedInstance(attachedInstances[i]);
attachedInstances[i] = attachedInstances[attachedInstances.Count - 1];
attachedInstances.RemoveAt(attachedInstances.Count - 1);
@@ private static AttachedInstance FindOrAddAttachedInstance(...)
if (attachedInstance == null)
{
- attachedInstance = new AttachedInstance();
+ attachedInstance = Instance.freeAttachedInstances.Count > 0 ? Instance.freeAttachedInstances.Pop() : new AttachedInstance();
Instance.attachedInstances.Add(attachedInstance);
}
@@ public static void DetachInstanceFromGameObject(FMOD.Studio.EventInstance instance)
if (manager.attachedInstances[i].instance.handle == instance.handle)
{
+ manager.RecycleAttachedInstance(manager.attachedInstances[i]);
manager.attachedInstances[i] = manager.attachedInstances[manager.attachedInstances.Count - 1];