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.

Any progress on this bug ? @MarcelPursche

Hi!
Unfortunately not yet. We are actively investigating this bug but is rather tricky.

Would you please prioritize this issue? Our real estate applications are crashing after sometimes. We have multiple projects published already.

We checked our code to be sure that is not the cause of the leak. It’s stable after many allocation and disposing in the editor.

Version: 6.1.7f

We’ve also been having similar issues with our application regularly crashing on desktop and mobile web after long periods of regular usage or short periods of intense use, especially loading 3d models from GLB/GLTF files. We were using Piglet to load these files. When we reached out to see what could be causing this issue, Unity support said that it could be a problem with that plugin, so we switched to Unity’s first-party GLTFast plugin, but that didn’t seem to help with the memory issues. Models load slightly faster, so at least it wasn’t a total bust, though :slight_smile:

@MarcelPursche I would also like to ask for prioritization of this issue - this one causes our web-based app to crash consistently, with no way for us to fix it on our side.

Same problem here ! Our web app is randomly crashing after few minutes. It would be great if this issue can be a top priority

Bad news for those waiting a solution for (UUM-86352).

Unfortunately, there is no way to free memory from unused (Resources.UnloadUnusedAssets) textures, materials and meshes properly without GC unless they are addressables.

We are fetching a lot of GLB files remotely with textures in them. We are culling GLBs by camera frustrum and need to destroy each object out-of-frustrum; otherwise, the browser is throwing OOM. That’s why manual GC is crucial in our case. Caching those GLBs is out of option since we are not the provider.

Do you have any suggestions to solve this issue?

Bad news for those waiting a solution for (UUM-86352).

Even worse news for those waiting for a solution: They’ve marked this bug as “by design”.

@MarcelPursche Given that it has been determined that this issue is not going to be fixed, what options do we have going forward to actually make our app function without running into out of memory issues with normal, ordinary usage?

The state that Unity 6 is in makes the Web platform a ticking timebomb and unreasonable for anything more than hobbyist use. The literal only way to prevent our app from crashing is asking the user to limit the amount of time they spend in the app to a few minutes - which is the opposite of what we want our customers to do.

What options do we have to make our web builds work as intended again?