Unity Web GC Bug

I noticed that GC does not work correctly on Web platform. It fails to collect all unreferenced C# objects. It does not reproduce on other build platforms. This is a big issue for us, because we want to run our game on iOS web and that platform has very strict RAM limits.
I have a sample repro project on github GitHub - astro75/unity-webgl-gc-bug
I also reported this bug to Unity but I have no idea if that issue will get fixed. Unity Issue Tracker - Garbage Collector memory leak when allocating and deallocating memory in Web builds

Did anyone encounter this issue or knows a workaround?

Hi!

We are looking into this issue.
Would using the NativeArray collections for temporary data be an alternative for you? They are a thin wrapper around a buffer of native memory and do not use Garbage Collection. Instead you can immediately free their memory using the Dispose() method. They also offer control over the allocator so you can allocate memory with different lifetimes.

Hi, thanks for looking into it.
We already optimized largest RAM allocations using NativeArray. Also optimized max RAM usage by a lot. I found this bug by accident when testing GC API on web builds.
But the project is huge and it is impossible to track and optimize everything at this stage.
In some cases reducing total RAM usage means we use lighter pooling setup that causes more allocations overall, which probably triggers this GC bug even more.

Sorry to hear that. I will keep you posted when there are updates for this bug.