# Garbage Collection causing lag on level unload

**URL:** <https://forums.unrealengine.com/t/garbage-collection-causing-lag-on-level-unload/2303152>\
**Category:** Programming & Scripting\
**Tags:** loading, unreal-engine, question\
**Created:** [January 25, 2025, 9:15pm UTC](https://forums.unrealengine.com/t/garbage-collection-causing-lag-on-level-unload/2303152 "2025-01-25T21:15:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheWolf](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/thewolf/32/1036665_2.png) [@TheWolf](https://forums.unrealengine.com/u/TheWolf)\
**Post date:** [January 25, 2025, 9:15pm UTC](https://forums.unrealengine.com/t/garbage-collection-causing-lag-on-level-unload/2303152/1 "2025-01-25T21:15:05Z")

</div>

Hi, I’m making a game akin to Limbo/Inside where the level loading/unloading is meant to be seamless (eg: no load screens) I’m having an issue where GarbageCollection is causing sharp lag spikes whenever I unload a level.

I have tested loading/unloading via two methods in a packaged development build:

1. Level Streaming : Using ‘Load Stream (by Name)’ to load / ‘Unload Stream Level (by Name)’ to unload with a persistent level housing sublevels that are set initially unloaded.

2. Level Instancing : Using ‘Load Level Instance (by Name)’ to load / ‘Set is Requesting Unload and Removal’ with a persistent level with no sublevels in editor.

Both give me similar results, which look like this (which unsurprisingly creates a very distracting lag spike in-game):

 ![Screenshot 2025-01-25 001219](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/e/4/7e41334176db016d37bbfa46c8829a896f8bdc39.jpeg)

My GC settings are:

 ![Screenshot 2025-01-25 131028](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/2/3/e2340c69f940f28fc6a13c52c233a1827aae0867.jpeg)

Any tips on diagnosing this greatly appreciated!

---

<div class="post-metadata">

**Author:** ![TheWolf](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/thewolf/32/1036665_2.png) [@TheWolf](https://forums.unrealengine.com/u/TheWolf)\
**Post date:** [January 25, 2025, 11:32pm UTC](https://forums.unrealengine.com/t/garbage-collection-causing-lag-on-level-unload/2303152/2 "2025-01-25T23:32:24Z")

</div>

UPDATE : A default setting setting forces GC after streaming a level out, which caused my CG spike:

s.ForceGCAfterLevelStreamedOut

Turning this off appears to have resolved the issue.

Solution credit goes to : Petr Leontev  
([Level Streaming And Garbage Collection Optimization Tweaks In Unreal Engine 4](https://peterleontev.com/blog/level_streaming_optimization/))

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [February 24, 2025, 11:32pm UTC](https://forums.unrealengine.com/t/garbage-collection-causing-lag-on-level-unload/2303152/3 "2025-02-24T23:32:40Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
