My WebGL game seems to slow down after running for several hours. I’m currently running it in Chrome with the Profiler connected, and here’s some data from the Memory window:
GameObjects in Scene: 2,151
Total Objects in Scene: 10,084
Total Object Count: 14,369
After running for 2 hours:
GameObjects in Scene: 2,464
Total Objects in Scene: 11,748
Total Object Count: 16,127
I could really use some advice on how to track down where all these extra objects are. I’m still pretty new to the Profiler. In other words, is there a way to show all the new objects since a certain time? Halp!
Well it’s sometimes difficult to track these down. They are usually related to a hidden object creation that you didn’t clean up. Some examples are “Renderer.material” or “MeshFilter.mesh”. When accessing those Unity will automatically create a new instance of the Material / Mesh for this object. However when you destroy the gameobject the Material / Mesh instance is still around.
Generally every UnityEngine.Object derived class is a “tracked object” which has to be destroyed explicitly. Though there are some implicit destructions going on. When a scene is “unloaded” all gameobjects are destroyed automatically (except those marked with DontDestroyOnLoad). Along with the gameobjects the attached components are also destroyed since there’s an “owning” relationship between components and their gameobject.
Have a look at this forum post. One can use “Resources.UnloadUnusedAssets” to clean up tracked objects which aren’t referenced anymore from any active object. However it’s a quite heavy method call. It’s generally recommended to clean up your stuff yourself.