Is there a tool/function to use to find out where the managed memory is being used? I’ve got a memory leak in my project which gets pretty bad after playing for a while, and I can’t find a way to profile the managed memory. The builtin profiler seems to only give the total heap size (which grows over time).
Edit; To clarrify my issue:
My issue is with presumed garbage that is referenced somewhere in my code, so it isn’t actual garbage according to the GC (righlty so). Think like a custom terrain solution with LODs. Somewhere a class keeps references to low level LODs that are not used anymore. To find it, you would have to either know your codebase thoroughly enough to realize it (codebase must be relatively small, or very clean) or instrument a lot of code to find out who keeps what (a lot of work). Dumping a memory hierarchy/graph would reveal the class being responsible for a lot more memory than it’s supposed to be quite quickly.
Eliminate foreach. If you are using a version of Unity that is previous to 5.5 etc, for each loops fail to properly be cleaned up by the garbage collector. The garbage adds up. It is not necessarily a commonly-known issue. Also eliminate LINQ if you use it - replace it with plain 'ol generics.
As far as I’m aware, every foreach generates 12 or 24 bytes of garbage, but it is properly cleaned up by the garbage collector when it’s triggered.
Sorry, re-reading the OP I see I haven’t properly explained my problem:
My issue is with presumed garbage that is referenced somewhere in my code, so it isn’t actual garbage according to the GC (righlty so). Think like a custom terrain solution with LODs. Somewhere a class keeps references to low level LODs that are not used anymore. To find it, you would have to either know your codebase thoroughly enough to realize it (codebase must be relatively small, or very clean) or instrument a lot of code to find out who keeps what (a lot of work). Dumping a memory hierarchy/graph would reveal the class being responsible for a lot more memory than it’s supposed to be quite quickly.
@Zuntatos you should use the Unity profiler to diagnose what’s exactly is kept in memory (assets, objects, etc).
Specifically, you should look into the “Memory” section of the profiler and set it to “Detailed” mode.
I am aware of the builtin memory profiler. My problem with it is this:
That’s as detailed as it goes for heap memory debugging. It does work great for unity objects lingering about, don’t get me wrong, but not for ‘normal’ objects.
Resources.UnloadUnusedAssets() only unloads, well, assets. I guess this includes monobehaviours and other components. But those are listed in the memory profiler as well I believe. My leak is happening by some static class / field keeping references to class instances that are not needed anymore, and those accumulate over time.
I’m iterating over all types in all assemblies, and adding any static fields as roots. I also consider every component (monobehaviour instances, scriptableobject instances, unity builtin component instances) as root objects.
I may be missing something here, but what you’re doing is only listing the size of static members of compiled types, not the ones that are actually loaded into memory. Also, not sure how accurate it is.
The only piece of real information you can get out of this is looking at all the loaded Unity objects as roots and iterating from there. Your abilities to correctly and accurately measure things in this case are also very limited.
The only way to correctly measure the managed heap information is to interact with the Mono runtime environment and query it for this data. This is how .NET profilers usually work from what i know.
I fully agree that some data is missing and that the profilers built into the runtime are (vastly) superior. But I’d likely have to recompile mono.dll or otherwise ‘hack’ something in unity to turn it on.
Some references I’m missing (that I know about);
static fields of generic classes, would need to know the type parameters (may count static fields on generic instances found once when traversing the rest someday, but would not guarantee all would be found)
Types generated at runtime (I’m not sure about this, no experience with it, they may be loaded into an assembly which would be tracked like the other assemblies)
I thought objects referenced by delegates only would also be missing, but just checked it and those are properly counted. (those are a major source of leaks afaik)
I’m probably missing more, I’d love to know about it (and possible solutions to count them). In my project I only get ~340 MiB out of the reported used heap of ~440 MiB, so something is wrong for sure. I doubt that that 100 MiB of data is in generic statics (practically zero use) or runtime types (I don’t use them at all to my knowledge), but I can’t be sure.
Size estimates do consider object overhead and padding (to my best understanding of it, little resources available).
Do you mean the generic classes and/or runtime generated types? If so, I addressed them now in this post. Otherwise, can you please elaborate?
no, i meant that if i understand your process correctly, you are checking all static members on a given assembly; but this doesn’t mean that this static class is even loaded into memory.
Ah, yes, the static constructors may not have run yet, and will be triggered. There seems to be no way of knowing whether or not the type is initialized that way. It does have some impact on memory use, iterating over all types adds ~3 MB total to the heap size, most of which (2.5MB ish) seems to be in my own assembly.
@liortal I’m using IL2CPP to build a test project on iOS. Why I can only see native memory usage and get nothing on ‘managed memory’. I’ve logged out the PackedMemorySnapshot.managedHeapSections.Length, and it turns out zero.
There’s this issue: https://issuetracker.unity3d.com/issues/ios-profile-does-not-take-detailed-memory-samples linked to from bitbucket, it says it does not work on IS with: Unity 5.3.6p8, 5.4.1f1, 5.4.2p2, 5.4.2p4 and is fixed in 5.4.1f1 (not sure how it’s both broken and fixed in same builds, but okay). There’s a possibility the bitbucket readme hasn’t updated properly while the version requirement is now patched 5.4 and up, or 5.5+.
Disclaimer: No experience using it since I’m not using IL2CPP.
P.S: My dodgy profiler may work properly on IL2CPP on mobile/uwp etc if the location where it logs to is changed to the persistent path. It’d be better then no profiling.
That’s too bad, my current version is 5.3.4p1 and I’ve just downloaded 5.3.6p8 yesterday…so you mean all the 5.3 versions and patches don’t work anymore? That would be so so bad…