unity3d - High memory allocations when unregistering delegates from event in C# -
unity3d - High memory allocations when unregistering delegates from event in C# -
we developing game unity3d engine (which uses mono user code - our code written in c#).
the scenario have class exposing event, around ~ 250 registrations event (each level object on game's map registers event):
// every level registers (around 250 levels) scoresdatahelper.onscoresupdate += handleonscoresupdate;
when destroying scene, every object unregisters event:
scoresdatahelper.onscoresupdate -= handleonscoresupdate;
when using built-in profiler, seeing huge memory allocation, digging deeper shows due delegates beingness unregistered.
i suspect due fact delegates immutable , when chaining them together, new instances created ?
here's screenshot unity's profiler:
is there way avoid these memory allocations when dealing big number of event subscriptions?
as confirmed in comments want unsubscribe event subscriptions, there no reason unsubscribe 1 one.
you set event null. unsubscribe event without allocating memory.
this.onscoresupdate = null;
one thing note can't outside of scoresdatahelper
class. must within scoresdatahelper
class.
c# unity3d mono
Comments
Post a Comment