- Home /
Am I leaking Mesh components or does Unity Cache Meshes between scenes?
Hi There
I was disturbed to see that moving between levels of our game (back and forth between two levels) and without changing anything, without saving or changing the number of meshes, that mesh count increases by two moving from one level to the next and the RAM used by meshes also increases level to level.
Firstly I am surprised to find that after calling Resources.UnloadUnusedAssets() having loaded a completely different level, if I call Resources.FindObjectsOfTypeAll() that all the meshes from the previous level are still loaded. Is Unity caching these? Certainly the mesh count goes up by only 2, far fewer than in the scenes concerned, but the mesh RAM is going up by either 5.1 or 5.4 MB, consistently, bar the slight fluctuation in the amounts.
While I can call Resources.FindObjectsOfTypeAll() and I think identify which Mesh are being added, I can't think of an easy way to find these meshes.
Note that the metrics I am observing are when connecting to a remote player - either iPhone or OSX Desktop player.
None of the meshes are marked DontDestroyOnLoad() but even so, it is hard to discover which meshes are being affected and as I am moving between levels, I cannot see by any visual inspection, that I am getting duplicates.
Cheers Bovine
I haven't come across this issue, but I'd be interested to see what's causing it, so I can avoid this problem... seems rather nasty, given how far out of your way you're going for garbage collection... are you using JS or C#, just out of curiosity?
I'm using C#, our levels are quite large, especially for an iOS game. I am doing a plain vanilla test swapping between some very, very simple scenes.
Answer by Bovine · Aug 24, 2012 at 01:02 PM
My simple test moving between two scenes - the mesh count and RAM is entirely predictable. Which means it must be me, or more accurately, it means that the save game system I am using is hanging onto mesh references.
It possibly explains another leak too.
While I admire the willingness to blame yourself, this doesn't actually answer your question. I too am having this same problem, and I do not have any save game functionality. I can consistently see my mesh count increase at run time every time I load a level. Happening in Editor, on PC, using C#. One unique behavior I've noticed is I have some objects that are deactivated by default, and I toggle them on/off as needed during game play. If I change the logic so everything is active at start, and then deactivate the things I don't need at run time, the leaked mesh problem does not happen(Though everything is visible for a second at load and looks terrible). If I change it back to be deactivated by default, then the leaked mesh problem returns. This is 100% reproducible. I can easily query the meshes in script and find all of them, but, as there is no way to get the object a mesh is associated with, I can't find them in the scene, nor discover where they came from.
Interesting. Certainly confirms the behavior I'm seeing. Unfortunately the link you gave appears to be broken, but I am looking into the work around you mentioned to see what I can figure out.
Accidentally deleted the post... the link was to the save system use, Unity Serializer but I can't figure out why it won't work - seems Unity Answers is adding a trailing character encoding, perhaps a space?... anyone I edited the link, maybe it'll work now, otherwise remove anything after .com and manually go to the serializer.
That said, unless you're using it too it's not likely to help you - I was documenting the system I was using.
If you're calling Resources.FindObjectsOfTypeAll() anywhere, perhaps passing typeof($$anonymous$$esh) in as a parameter, then you maybe suffering a similar problem if you're caching every mesh returned by same.
if you're NOT calling this, then you might want to consider calling it and seeing if your mesh count is going up.
Note that if you're running in the editor the profiler metrics (and I think the above method) will return editor objects as well, so use carefully and of course, the numbers returned might not be an accurate reflection of the real world
Original post below.... .......
Hi There
I can't recall the details, but essentially the save game system we're using was caching everything in the scene, but despite the fact that the cache was cleared between levels, the Combine$$anonymous$$eshes created, I believe as part of static batching, are retained between levels and 'leak' so going from one level to the next and back, I would have two copies of all statically batched stuff from the first level being entered twice and 1 copy from the 2nd level.
On memory sparse devices like the 3GS this rapidly becomes a huge problem as I can be leaking 5+$$anonymous$$B between levels.
$$anonymous$$y solution, was to not add the combined meshes to the cache and in fact the Unity Serializer we use now has a filter delegate $$anonymous$$ike added so we could disable this (http://whydoidoit.com).
I can't say for sure whether this is a Unity bug or a bug in $$anonymous$$ike's code, but when I encountered it, it certainly looked like the former.
One irritating thing, is that these combined static meshes have a negative id on the PC but a positive one on iOS and so the only way to tell is to look the name of the mesh, which, if it contains 'Combined $$anonymous$$esh' is one of the offending meshes and we don't put that anywhere!
Perhaps that will help? GL.