# How to avoid GC.MarkDependencies?

**URL:** <https://discussions.unity.com/t/how-to-avoid-gc-markdependencies/589376>\
**Category:** Scripting\
**Tags:** Scripting\
**Created:** [July 14, 2015, 2:13pm UTC](https://discussions.unity.com/t/how-to-avoid-gc-markdependencies/589376 "2015-07-14T14:13:50Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chaoseiro](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/chaoseiro/32/320783_2.png) [@Chaoseiro](https://discussions.unity.com/u/Chaoseiro)\
**Post date:** [July 14, 2015, 2:13pm UTC](https://discussions.unity.com/t/how-to-avoid-gc-markdependencies/589376/1 "2015-07-14T14:13:50Z")

</div>

Hello everyone,

I’m having a huge performance issue that I’m trying to solve somehow…  
Has anyone faced this problem where GC.MarkDependencies takes a huge amount of CPU time? (see attached screenshot)

How it is happening: In my game, the maps (2D tile maps) are generated at Runtime, because the player can choose which set of maps he will explore. They are generated using Coroutines to keep the UI responsive (loading screen), but after loading, GC.MarkDependencies starts it’s job, but takes a really long time to complete, stealing precious frames (in my last profiling, it took 65 frames in my PC, an i7-2670QM, running inside a VM)  
It is hidden inside the Loading.UpdatePreloading and, since it starts when I load my maps, I know I must change this loading somehow…  
Map loading/generation do generate some garbage, about 60kb per map (still working on it), but I believe this would not cause the problem above…

When I say ‘generated’, I mean: creating the material, assigning the TileSet texture to it, creating the Mesh to support each Tile then UV mapping it accordingly to the desired Tiled Map (see [Chaoseiro / X-UniTMX / wiki / Home — Bitbucket](https://bitbucket.org/Chaoseiro/x-unitmx))

Things I still need to try:

- Loading from Prefabs instead of generating at Runtime (I think this would be the solution, but want to hear thoughts from someone that also faced this problem)
- Using Application.LoadLevelAdditiveAsync (I believe this would not help since GC.MarkDependencies fires after maps are loaded)
- Something else I do not know yet 🙂

So, has anyone faced, and solved, this problem?  
Is the Prefab route the probably solution?

I can try to explain better or give more information if needed 🙂

Thanks in advance!

 ![2204013--146414--gcmarkdependencies_headache.PNG](https://europe1.discourse-cdn.com/unity/original/4X/3/4/2/342e5d3be70dd6e3a58227ab19a122cfe5ed3b33.png)

---

<div class="post-metadata">

**Author:** ![jesta](https://avatars.discourse-cdn.com/v4/letter/j/c5a1d2/32.png) [@jesta](https://discussions.unity.com/u/jesta)\
**Post date:** [September 15, 2015, 2:25pm UTC](https://discussions.unity.com/t/how-to-avoid-gc-markdependencies/589376/2 "2015-09-15T14:25:55Z")

</div>

Having the same problem here. We’re using Unity 5.0.4.

---

<div class="post-metadata">

**Author:** ![Sparrowfc](https://avatars.discourse-cdn.com/v4/letter/s/53a042/32.png) [@Sparrowfc](https://discussions.unity.com/u/Sparrowfc)\
**Post date:** [January 12, 2016, 9:27am UTC](https://discussions.unity.com/t/how-to-avoid-gc-markdependencies/589376/3 "2016-01-12T09:27:03Z")

</div>

bump, huge influence on iPhone 4s, but not found on other advanced generation of devices

---

<div class="post-metadata">

**Author:** ![Wrymnn](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/wrymnn/32/293021_2.png) [@Wrymnn](https://discussions.unity.com/u/Wrymnn)\
**Post date:** [February 29, 2016, 11:47am UTC](https://discussions.unity.com/t/how-to-avoid-gc-markdependencies/589376/4 "2016-02-29T11:47:39Z")

</div>

Same problem here, big problem when generating terrain and using threading…

---

<div class="post-metadata">

**Author:** ![Nick\_lzn](https://avatars.discourse-cdn.com/v4/letter/n/dbc845/32.png) [@Nick\_lzn](https://discussions.unity.com/u/Nick_lzn)\
**Post date:** [July 13, 2016, 9:59am UTC](https://discussions.unity.com/t/how-to-avoid-gc-markdependencies/589376/5 "2016-07-13T09:59:31Z")

</div>

I find out the reason. remove the Resources.UnloadUnusedAssets. It can cause GCCollect many times, cost amount of CPU times in some Low side mobile phone. You can use Resources.UnloadAssets(“”); manually. This API can not cause GC.

---

<div class="post-metadata">

**Author:** ![Chaoseiro](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/chaoseiro/32/320783_2.png) [@Chaoseiro](https://discussions.unity.com/u/Chaoseiro)\
**Post date:** [January 11, 2018, 6:26pm UTC](https://discussions.unity.com/t/how-to-avoid-gc-markdependencies/589376/6 "2018-01-11T18:26:08Z")

</div>

> [@Nick\_lzn](#):
>
> I find out the reason. remove the Resources.UnloadUnusedAssets. It can cause GCCollect many times, cost amount of CPU times in some Low side mobile phone. You can use Resources.UnloadAssets(“”); manually. This API can not cause GC.

After some years, finally came back to this project/problem, and this solves it 🙂  
(I also did the whole prefab thing, but that was also needed for other optimizations)

Thanks!

---

<div class="post-metadata">

**Author:** ![jellevda](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/jellevda/32/900059_2.png) [@jellevda](https://discussions.unity.com/u/jellevda)\
**Post date:** [September 24, 2018, 12:48pm UTC](https://discussions.unity.com/t/how-to-avoid-gc-markdependencies/589376/7 "2018-09-24T12:48:36Z")

</div>

I have a similar situation going where chunks of environment are being streamed at runtime, I’m using an asset for this called SECTR Stream from the store and apparently it calls Resources.UnloadUnusedAssets() as well. In some areas this was causing a 1800ms spike in the profiler. After getting rid of it no more spikes. Thanks so much for pointing this out Nick\_lzn!

---

<div class="post-metadata">

**Author:** ![rh\_galaxy](https://avatars.discourse-cdn.com/v4/letter/r/5f8ce5/32.png) [@rh\_galaxy](https://discussions.unity.com/u/rh_galaxy)\
**Post date:** [June 12, 2019, 8:59am UTC](https://discussions.unity.com/t/how-to-avoid-gc-markdependencies/589376/8 "2019-06-12T08:59:55Z")

</div>

I’m having a spike of 150ms, right after LoadSceneAsync is done after first frame of the new scene this happens. I don’t see what I can do about it, how can I avoid “Resources.UnloadUnusedAssets” when i’m not calling it? I also use Incremental GC to avoid these kind of spikes.

From the game log:  
“Unloading 144 unused Assets to reduce memory usage. Loaded Objects now: 2246.  
Total: 82.161980 ms (FindLiveObjects: 0.188295 ms CreateObjectMapping: 0.040623 ms MarkObjects: 81.442011 ms DeleteObjects: 0.491050 ms)”

This is extra bad in my VR application and I’m stuck… It’s enough frames lost that OpenVR puts the user back in the “grid world environment”… Can’t have that.

Do I have to avoid using Scenes at all?

---

<div class="post-metadata">

**Author:** ![gilley033](https://avatars.discourse-cdn.com/v4/letter/g/c6cbf5/32.png) [@gilley033](https://discussions.unity.com/u/gilley033)\
**Post date:** [April 8, 2020, 7:29pm UTC](https://discussions.unity.com/t/how-to-avoid-gc-markdependencies/589376/9 "2020-04-08T19:29:37Z")

</div>

Resources.UnloadUnusedAssets unloads native Unity assets from memory, keeping your games memory use down. If you just remove calls to it, you are potentially introducing a memory leak into your game.

Please run your game and monitor the memory in the profiler or the Task Manager to check if you have a memory leak. If you do, you will need to either manually unload assets one by one using Resources.UnloadAsset, or call UnloadUnusedAssets at a point where the user will not notice the hitch, such as during a loading screen.

---

<div class="post-metadata">

**Author:** ![cdr9042](https://avatars.discourse-cdn.com/v4/letter/c/ec9cab/32.png) [@cdr9042](https://discussions.unity.com/u/cdr9042)\
**Post date:** [June 3, 2020, 11:11am UTC](https://discussions.unity.com/t/how-to-avoid-gc-markdependencies/589376/10 "2020-06-03T11:11:21Z")

</div>

> [@rh\_galaxy](#):
>
> I’m having a spike of 150ms, right after LoadSceneAsync is done after first frame of the new scene this happens. I don’t see what I can do about it, how can I avoid “Resources.UnloadUnusedAssets” when i’m not calling it? I also use Incremental GC to avoid these kind of spikes.
> 
> From the game log:  
> “Unloading 144 unused Assets to reduce memory usage. Loaded Objects now: 2246.  
> Total: 82.161980 ms (FindLiveObjects: 0.188295 ms CreateObjectMapping: 0.040623 ms MarkObjects: 81.442011 ms DeleteObjects: 0.491050 ms)”
> 
> This is extra bad in my VR application and I’m stuck… It’s enough frames lost that OpenVR puts the user back in the “grid world environment”… Can’t have that.
> 
> Do I have to avoid using Scenes at all?

I have same problems as you. Huge lag spike due to GC.MarkDependencies when using LoadSceneAsync

---

<div class="post-metadata">

**Author:** ![wagenheimer](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/wagenheimer/32/90665_2.png) [@wagenheimer](https://discussions.unity.com/u/wagenheimer)\
**Post date:** [November 9, 2021, 9:02pm UTC](https://discussions.unity.com/t/how-to-avoid-gc-markdependencies/589376/11 "2021-11-09T21:02:49Z")

</div>

Any improvement on this? I have the exact same problem on Unity 2021.1.27f1

---

<div class="post-metadata">

**Author:** ![rimkchouic](https://avatars.discourse-cdn.com/v4/letter/r/f14d63/32.png) [@rimkchouic](https://discussions.unity.com/u/rimkchouic)\
**Post date:** [February 11, 2022, 9:24am UTC](https://discussions.unity.com/t/how-to-avoid-gc-markdependencies/589376/12 "2022-02-11T09:24:58Z")

</div>

I have 3 scenes, 1 referenced in the build and the other 2 are addressables that are loaded / unloaded from the first one.  
These scenes each contain two buttons and a TextMeshPro input field.

On my decently configured PC, it takes 600ms.  
On a OnePlus 7 Pro, 6-7k ms.  
On a OnePlus 3T (2016) device, one of the frame takes between 12k and 14k ms.

According to the profiler, the responsible is GC.MarkDependencies. @Nick_lzn 's solution doesn’t work for me as the only places where `Resources.UnloadAssets();` is called is in URP and TextMeshPro code. I still tried removing those calls and ran the profiler again, bearing the same results.

If anyone would have any idea what might be causing this, it’d be great.

**EDIT:** I was using Photon’s fusion networking api. I tried having the same setup without it and the issues went away. I know where to dig now.

 ![7888084--1003807--Profiler_13kms.png](https://europe1.discourse-cdn.com/unity/original/3X/8/b/8be2001e07988cb10ff00ef68b4e8fe4d68bfcd2.png)

---

<div class="post-metadata">

**Author:** ![MartinTilo](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/martintilo/32/91392_2.png) [@MartinTilo](https://discussions.unity.com/u/MartinTilo)\
**Post date:** [April 19, 2022, 12:12pm UTC](https://discussions.unity.com/t/how-to-avoid-gc-markdependencies/589376/13 "2022-04-19T12:12:46Z")

</div>

To hopefully add some clarity to this thread let me quote my reply from [this related thread](https://forum.unity.com/threads/resolved-no-should-i-just-resources-unloadunusedassets-everything.1119994/#post-8060135) :

> [@MartinTilo](#):
>
> Yeah, you shouldn’t call Resources.UnloadUnusedAssets all over the place but only in a rather controlled way. E.g. once after a destructive SceneUnload (i.e. this inncludes a Scene Load that replaces the previous scene) if you need to lower the memory pressure at the beginning of that scene, and in particular if you’ve created a bunch of instances of types inheriting from UnityEngine.Object at runtime and lost track of them (and therefore can’t call Destroy on them to get rid of them). Similar logic applies to for procedural level creation workflows where you have to either track everything you created and loaded so you can directly Destroy or unload them, or find a loading screen or similar situation within which you could trigger Resources.UnloadUnusedAssets.
> 
> Addressables and AssetBundles in general can help with the loading part, prefabs can help you in the creation part. Where Prefabs also helps in reducing the amount of Managed Shell objects to all these UnityEngine.Objects you’re creating at runtime. Every such objectthat you access via scripting code gets a shell for scripting access. So when you create everything from Scripting code, that’s all of them. If part of these objects is loaded from prefabs or scenes, onlytrh objects you actually need to access during runtime get Managed Shell Objects created for them.
> 
> Lastly, GC.MarkDependencies is the marker for the Asset GC, i.e. UnloadUnusedObjects. The Asset GC triggers the Scripting GC as part of the MarDependencies step, but the Scripting GC (GC.Collect) is otherwise separate from this.
> 
> This doesmean though that the time the Asset GC take, while (partially) multithreadded, scales linearly with the amount of Managed (aka Scripting) Objects (including the Managed Shells for Native Objects) and Native (aka Assets and Scene Objects) Objects AND with the amount of connections between these.
> 
> I hope this clarifies some things about this rather abstract profiler marker

Happy to answer any related questions as best I can. Also, the [Memory Profiler package](https://docs.unity3d.com/Packages/com.unity.memoryprofiler@latest/) aims at clarifying the status and reasoning for objects to be retained in memory via the References and Selected Item Details sections. So if in doubt, give that a try 🙂
