Unity Editor's memory footprint spikes after hitting play, before game begins

This is a massive project. With an empty scene loaded, Unity’s using ~0.5gigs of memory. In the main menu scene, it sits at ~1 gig. This is fine, I’m not in control of art assets or anything, I don’t know how big it should be.

If I open and run an empty scene, it does not spike memory. If I then move to my main scene and run that, Unity’s memory usage according to task manager rises up to 3.5 gigs before the game even begins. That is, I press the play button, Unity stops responding. I can open the task manager and watch its memory slowly climb to 3.5g, then slowly drop back to 1g, then it starts playing. The profiler hasn’t kicked in, and all of my initialization code thinks it’s time zero after the memory hump, so I don’t know of a way to find issues.

If I open an empty scene after running the main menu, even the empty scene will rise to 3.5 gigs of memory, in exactly the same fashion.

I’m not sure what information is helpful in determining the problem here. I’m mostly looking for, has anyone encountered anything similar, any words of advice? I’m going to just disable objects one at a time and hope that at some point it will stop being silly, but each attempt takes several minutes to be sure I’m checking everything correctly and it could take days to go through each object one by one even in such a small scene.

This issue was caused by using [Serializable] classes as data storage in MonoBehaviour assets. Apparently Unity hates this with a passion, and expresses its hate by loading the offending assets several hundred thousand times. When I remove all my data storage calls but one, and point that one to an identical setup using ScriptableObjects instead of serializable classes, the issue is completely gone. There may be other culprits, I suppose I’ll find them as I go along.

Edit: I lied, it keeps spiking once I start adding in real data. Back to the drawing board.

I believe the Unity Editor needs to “get all it’s eggs in a row” before the scene starts. The larger the project, the more time and memory it will take for this process to happen. Have you tried exporting the project and seeing if the memory spike still occurs?

I don’t know if this is intended behaviour or not; it certainly seems intended, but I would do everything in my power to prevent this if I were in control of Unity, so I dunno. :slight_smile:

We were able to resolve our issue by creating a new, empty scene. All that scene does is load the main menu (the problematic, spiky scene above). This new scene sits happily at ~300megs of memory, and rises to ~900megs to load the main menu, which is fine - that’s what the main menu sits at. There is no memory spike whatsoever.

I can only conclude that Unity double- or triple- or quadruple- loads objects as it starts up.

Out of curiosity, I clicked on a few complex prefabs in the project before starting from this fresh scene, and Unity used up so much memory during startup after that that it actually crashed itself. This is bad behaviour, from my point of view; I have no idea what Unity thinks it’s doing after I press the play button and before it starts actually playing, but it’s definitely touching objects that were at one point loaded into memory, and somehow it’s finding it necessary to eat up every resource it can. Upon restarting Unity and playing the scene without touching project files, there is no lag and no memory spike and, in fact, no issue whatsoever. It runs exactly as desired as long as I don’t touch my prefabs and then also try to run it without restarting.