Unity2020.3.20f1
Running My app on WindowServer 2019,
Every frame only 138Byte alloc, Everything goes right unity wil trigger gc after a few minutes,
BUT After several Hours,The total memory increased So large,
In Profiler I Just Find Only 136B memory alloc, But GC Used Memory just increase 4GB,I can’t find where these big memory alloc.
Just MonoHeapSizeLong and GetMonoUsedSizeLong Increased Huge but GetTotalAllocatedMemoryLong GetTotalReservedMemoryLong GetTotalUnusedReservedMemoryLong seems normals
I can’t find why GetMonoHeapSizeLong GetMonoUsedSizeLong become so odd
Your last screenshot doesn’t seem to show a sample at a point where your memory usage actually makes those huge jumps up. You know that you can use the arrow keys to move one sample to the left / right? Turn on deep profiling and actually select one of the frames where a spike happens. You clearly have a huge increase at larger time intervals. Maybe a coroutine or invokeRepeating call.
So you need to properly analyse the profiler results
I don’t get your new screenshot. It’s still a frame you’re showing which allocates 139kb. Here, I made a screenshot from your first screenshot.
The “red dot” is the memory usage of the frame you have selected. As you can see the line is almost straight (I overpainted the yellow line since it was below the tooltips). What you should select is a frame where the memory usage increases heavily. Like the pink line I drawn in to the left. Here the usage makes a huge jump. So have a look at those frames and see how much is actually allocated in those frames and what is allocating it. I don’t know how you made the screenshot, but you can simply pause the game in order to analyse the profiler results.
As I said above you can use the arrow keys to go one frame forwards / backwards in order to fine select a certain frame. Again, we can not do that for you. You have the project and profiler data.
Thanks so much , i will select a certain frame try find where memeory alloc.
I will check if this big memory alloc in other thread, unity’s profiler can’t show other thread’s gc status
It can, but the Hierarchy view usually only shows the Main Tread, though in newer Unity versions it can be switched to show other threads as well.
The Memory Module shows GC.Alloc in frame across threads and the CPU Usage Module’s Timeline view shows Magenta Samples for GC.Alloc across all frames too
Instead of looking for the frame where the memory is getting allocated, you might also get somewhere by looking at what is in memory once it is using this much by using the Memory Profiler package. A combination of these two approaches is likely to help you the most in understanding what’s going on and how to fix it.
Finally with look up other thread gc in Unity3d’s Profiler, I find that the big gc was happened in other thread not in mainthread “while(true)” I used https://github.com/simonwittber/uniwebserver for create a http server in unity3d.
string ReadLine(NetworkStream stream)
{
var s = new List<byte>();
while (true)
{
var b = (byte)stream.ReadByte();
if (b < 0) break;
if (b == '\n')
{
break;
}
s.Add(b);
}
return System.Text.Encoding.UTF8.GetString(s.ToArray()).Trim();
}
I recently dealt with something similar. The heap was just continuing to grow in builds only (Win 10 64), despite some barely-significant alloc outside of the heavy lifting of scene inits and specific stuff at other times I knew the reason for, nothing could explain why a few hundred MB was just not being freed on every scene unload. It should never have been going higher than 2-3 gb or so (based on Play Mode and WebGL builds), but definitely came off leaky and was easily on the way to consuming all available resources / crashing out unless I’d close it before then.
I learned the Addressables Event Viewer stuff (amazing btw), found no problems there, probably tried every which combination of builds with Mono and IL2CPP and incremental GC and Burst. Things were working fine in the Editor and WebGL… well, after a dozen builds, I found some code that had gone grey - literally and figuratively, I sort of just kept skimming right over it each time without noticing what it was / figuring it wasn’t relevant.
Turns out, a long while back I was messing with runtime GC settings, experimenting with tuning how and when to perform it, and forgot a line when I removed the related stuff later. I hadn’t profiled Windows builds for a while since then due to not being focused on a release of a build for it, and just never noticed what I had done until the next time I wanted to spend time with related optimization (yesterday).
Well sure enough, after finding and removing this, things went back to working as expected.
The moral is - while unrelated to your specific problem (but definitely on topic) - it’s worth searching for any code with “GC.” or “GarbageCollector.” that might be in the project and commenting it out while trying to track down a problem like this