- Home /
Serializing class that has List of members of same type and referencing them in scene causes awfull long assembly reload time
Scenario:
Have a class that is [Serializable] and contains couple private members - Lists of same class like this
[Serializable]
public class SerializationListRecursionTest
{
private List<SerializationListRecursionTest> list1;
private List<SerializationListRecursionTest> list2;
private List<SerializationListRecursionTest> list3;
private List<SerializationListRecursionTest> list4;
}
Have a MonoBehaviour class that references that class couple times, like this
public class TestStateReference : MonoBehaviour { private SerializationListRecursionTest serializationListRecursionTest;
private SerializationListRecursionTest serializationListRecursionTest1; private SerializationListRecursionTest serializationListRecursionTest2; }
Have an empty scene and add like six objects that each has this MB attached
Do a code change (or force assembly reload in any way)
Unity editor freezes for couple seconds, memory consumption goes high (from around 110MB in this empty project to like 800MB and then goes low again). Assembly reload finishes. You might say its not that bad. But once there are more objects that references this one class or this clas is referenced indirectly from some other object, the consumption goes up to around 2.3GB and it takes around 20s or longer to reload the assembly.
Notes
removing serializable attribute makes problem go away
replacing List with just type makes problem go away
using simple arrays instead of list does not make problem go away
assembly reload in scene that does not contain objects that has this MB attached makes problem go away
In case you are wondering why am I making such a complicated code structure, see attached example. I am trying to write a Hierarchical State Machine Framework, that will be editable in custom editor window and save it into custom asset. Part of this framework is class HFSMState that inherits from State and can have List of another States/HFSMStates (cause it's hierachical)
I know that there is already logged issue that resembles my problem http://issuetracker.unity3d.com/issues/recursive-serialization-makes-unity-fail-badly but in my case this one is not caused by any recursion, it's caused just by Array/List recursion. And the sample project is missing. Also it is almost 5 years old, so I was hoping that something might change :)
Link on project
https://www.dropbox.com/s/g8e4l5jc8j9qst4/XinityAiToolTest.zip
Has anyone encountered this error and found a way to deal with it or is it just a 5 year old Unity memory leak bug that no one bother to fix yet?