Search code examples
c#unity-game-enginemono

High memory allocations when unregistering delegates from event in C#


We are developing a game with the Unity3D engine (which uses Mono for user code - our code is written in C#).

The scenario is that we have a class exposing an event, with around ~ 250 registrations to that event (each level object on the game's map registers itself to that event):

// Every level registers itself (around 250 levels)
ScoresDataHelper.OnScoresUpdate += HandleOnScoresUpdate;

When destroying this scene, every object unregisters itself from the event:

ScoresDataHelper.OnScoresUpdate -= HandleOnScoresUpdate;

When using the built-in profiler, i am seeing a huge memory allocation, digging deeper shows that it is due to the delegates being unregistered.

I suspect this is due to the fact that Delegates are immutable and when chaining them together, new instances are created ?

Here's a screenshot from Unity's profiler:

Profiler

Is there any way to avoid these memory allocations when dealing with a large number of event subscriptions?


Solution

  • As you confirmed in comments that you want to unsubscribe all the event subscriptions, there is no reason to unsubscribe it one by one.

    You could just set the event to null. This will unsubscribe everything from the event without allocating any memory.

    this.OnScoresUpdate = null;
    

    One thing to note is that you can't do this from outside of the ScoresDataHelper class. It must be inside ScoresDataHelper class.