Anyone have any suggestions for how to handle “System.OutOfMemoryException: Out of memory” exceptions?
I am generating a ton of meshes procedurally and decided to split my display meshes and physics/collider meshes for performance reasons (the physics meshes are of course a much simplified version of the visible mesh). However, when I did this, my game croaks shortly with out of memory.
It seems a bit odd in the sense that it doesn’t seem to be crashing at a particularly high number… profiler says 1GB or 2 (on a 16GB 64 bit machine) so I’m not sure if I’m missing something obvious or this ties back in to my vertex count in some odd way.
Any suggestions? This is just a normal desktop build, not a browser or phone or anything. My google searches so far have some back with rather vague info.
Where should I start debugging this? Is this particular to Unity or does it tie back in to Mono?
/edit maybe dynamic batching is a chief contributer?
the physical memory (16 GB) and the memory your os (64 bit) can handle does not matter as unity is a 32 bit application and thus can only adress 3.x GB of memory. this memory is used for EVERYTHING also graphics memory (which is afaik not shown in the taskmanager). so when Unity tells you it is out of memory there is nothing you can do but to reduce memory.
also i’m not sure what all your meshes means for all the materials used. in general the plain amount of mesh data is not all memory required for handling the mesh (textures, materials) so there are “hidden costs”.
thats still the problem. when you run the build there is less memory required anyway and 64 bit may help him but there is still no option to playtest the game in the editor.
It’s a long shot but I made a image editing program in .net once where it kept giving me that error, but I fixed it by forcing the GC to run and it fixed some of the problems with having a huge memory Bitmap or similar. If you wanna try it out use this code shortly before the code that makes you get the error; GC.Collect(1, GCCollectionMode.Forced);
If still not working you can try (if mono supports it) To look into “Applicationdomain” which in some degree makes it possible to split managed code into multiple processes and there by avoid a single process getting Out of memory exepctions.
You can read about both libraries if you google “C# GC” or “C# ApplicationDomain” but please note that i do not know if mono and unity have fully support for any of them since i haven’t tried it out in unity my self.
Thanks everyone, that gives me a good place to start investigating. Jackie, your post was particularly useful, thanks.
I will let you know what I find out. I don’t think I’m tripping over the max memory… it might be that the GC isn’t catching up quick enough though. And yeah I haven’t tested yet outside of the editor since this change. So there’s a lot I don’t know. We’ll see.