StudioEventEmitter.SetParameter allocates memory

Hey,

I noticed after some recent profiling that StudioEventEmitter.SetParameter allocates memory, In our game this is pretty undesirable. I saw that this issue was raised before in StudioEventEmitter.SetParameter creates Garbage but the recommended solution is no longer valid as that part of the API is no longer public (we’re using 2.02.06)

Instead I found another solution which required a slight modification to the plugin code

diff --git a/ShackUnity/Assets/Plugins/FMOD/src/StudioEventEmitter.cs b/ShackUnity/Assets/Plugins/FMOD/src/StudioEventEmitter.cs
index 9280b82de3d5ceb54f210b8a3b680fd4d5a106c8..5c22e4655454633c7398d24a281ce4b145879e53 100644
--- a/ShackUnity/Assets/Plugins/FMOD/src/StudioEventEmitter.cs
+++ b/ShackUnity/Assets/Plugins/FMOD/src/StudioEventEmitter.cs
@@ -270,7 +270,8 @@ namespace FMODUnity
         {
             if (Settings.Instance.StopEventsOutsideMaxDistance && IsActive)
             {
-                ParamRef cachedParam = cachedParams.Find(x => x.Name == name);
+                string avoidAllocName = name;
+                ParamRef cachedParam = cachedParams.Find(x => x.Name == avoidAllocName);
 
                 if (cachedParam == null)
                 {
@@ -296,7 +297,8 @@ namespace FMODUnity
         {
             if (Settings.Instance.StopEventsOutsideMaxDistance && IsActive)
             {
-                ParamRef cachedParam = cachedParams.Find(x => x.ID.Equals(id));
+                FMOD.Studio.PARAMETER_ID avoidAllocId = id;
+                ParamRef cachedParam = cachedParams.Find(x => x.ID.Equals(avoidAllocId));
 
                 if (cachedParam == null)
                 {

By copying the functions parameters that are used within the lambda inside the block in which the lambda executes prevents the allocation occurring for every function call and instead it will only allocate if the Settings.Instance.StopEventsOutsideMaxDistance block is enterred, which in our game never is.

This type of allocation is discussed further in the following stack overflow question .net - non executing linq causing memory allocation C# - Stack Overflow

I recommend rolling this change into your next fix version.

1 Like

Thanks for the suggestion, we’ll evaluate it and see about getting it into a future release.