Directly on the test code:
usingSystem.Collections;usingSystem.Collections.Generic;usingUnityengine; Public classteststructgc:monobehaviour{ Public structStructdef { PublicSystem.Action Act; PublicStructdef (system.action callback) {Act=callback; } } Public intMembervalue; Private voidUpdate () {//using anonymous functions, not accessing external functions, 0 GC{UnityEngine.Profiling.Profiler.BeginSample ("* * * * Test1"); Structdef obj=NewStructdef (NULL); UnityEngine.Profiling.Profiler.EndSample (); } //use anonymous functions, access temporary variables, 112B GC { intTMP =1; UnityEngine.Profiling.Profiler.BeginSample ("* * * * Test2"); Structdef obj=NewStructdef (() = {varv =tmp;}); UnityEngine.Profiling.Profiler.EndSample (); } //using anonymous functions, accessing external variables, 112B GC{UnityEngine.Profiling.Profiler.BeginSample ("* * * * TEST3"); Structdef obj=NewStructdef (() ={debug.logerror (membervalue); }); UnityEngine.Profiling.Profiler.EndSample (); } //do not use anonymous functions, 0 GC{UnityEngine.Profiling.Profiler.BeginSample ("* * * * TEST4"); Structdef obj=Newstructdef (Actcallback); UnityEngine.Profiling.Profiler.EndSample (); } } #regionOptimize for Test4PrivateSystem.Action Actcallback; PublicTESTSTRUCTGC () {Actcallback=Localcallback; } Private voidLocalcallback () {debug.logerror (membervalue); } #endregion}
Reference: https://blog.uwa4d.com/archives/Anonymous_heapmemory.html
anonymous function Access external variables have GC