Hi all, I am using the script DetectLeaks.cs which was shared in the unity wiki to display number of allocated memory. I found out that the number of objects in a scene is increasing every time I play the game on each scene. I read some thread about this and the recommendation is to call the Application.GarbageCollectUnusedAssets(). But curiously, that function is not present in my editor. Am I missing a namespace or directive? If so, what should it be? :roll: Or do you have any other suggestion on how I could clear the memory leak? Thanks.
Memory does get allocated continuously, but it will automatically be reclaimed. The whole idea of garbage collection is that dead memory blocks are ignored until memory runs out, at which point a collection takes place (although it may also be done at other strategic times).
None of this is harmful and it is not in any sense a memory leak. A leak means that memory is allocated in such a way that it can never be recovered after use. Eventually, memory will run out and the result is a crash.
If you believe you have discovered a real leak, then it is most likely a bug in your own code or in Unity itself. The only other issue with garbage collection is when it happens much too frequently or at inopportune moments, usually as the result of a naive choice of algorithm.
So does it mean that the increase in the number of objects that is detected by finding all the objects in the scene is not a memory leak and is normal? So you mean, it does not affect the performance of my game? Thanks. ![]()
Its normal yes.
It can and will affect your performance if you do it like insane (each update again and again) as the searching takes a fair amount of time. also if it involves a plentitude of string ops, you create a lot of objects that need to be reclaimed, as each string operation generates a new string.
Okay, got it. Thanks for the replies. ![]()
Note that Unity objects (as detected by DetectLeaks.cs) are not part of the Mono garbage collection process, which only applies to Mono stuff such as variables, arrays, etc. If you see the number of objects continuously going up, that means your code is at fault and should be fixed so this doesn’t happen, because it is in fact not normal and can’t just happen by itself.
–Eric
Hi eric, yes, the number of objects in my scene is continuously going up everytime I go back to the main menu, which is where i used the DetectLeaks.cs code. Follow up question, is the number of objects detected the total number of objects in that scene only, or the whole project? I think there’s really a fault in my code because it starts at 1047 objects at first play.
The script uses FindObjectsOfType, which, as the docs say:
"Returns a list of all active loaded objects of Type type.
It will return no assets (meshes, textures, prefabs, …) or inactive objects."
There is a certain baseline of objects made that Unity uses. If you run the script on an otherwise empty project, you can see that it’s 800 or so. (Making the GUI, for example, necessarily needs to create objects.) This number doesn’t go up by itself though…if it goes up, it means you are creating objects but not destroying them. Sometimes this isn’t particularly obvious, such as altering individual materials, where Unity creates new ones for you behind the scenes. That’s what the script is for, though, so you can see this happening and take steps to correct it.
–Eric
Oh, I see. but this really stops me. I’ll try to figure out how this is happening, and how to resolve this, since I am using SpriteUI in creating the objects in my scene. I was thinking of destroying them before I leave the scene, but I think it is not really the solution. Besides, unity crashes when I’m doing that. ![]()
EDIT: I tried to deactivate the objects, which I create at the start of my scene, at the exit of the level and the increase in number is reduced from +9 to +3 on every play. I am not sure if deactivating them really helps, but destroying them makes my game not working. So for now, this would be my workaround on this. I hope anyone could suggest any better ideas on fixing this.
I’m still finding where those three objects are coming from. :roll: