# Question about garbage collect and widgets

**URL:** <https://forums.unrealengine.com/t/question-about-garbage-collect-and-widgets/484702>\
**Category:** UI\
**Tags:** garbage-collector, remove-from-parent, unreal-engine, Widget, question\
**Created:** [June 10, 2021, 6:43am UTC](https://forums.unrealengine.com/t/question-about-garbage-collect-and-widgets/484702 "2021-06-10T06:43:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![BioMedKUL](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@BioMedKUL](https://forums.unrealengine.com/u/BioMedKUL)\
**Post date:** [June 10, 2021, 6:43am UTC](https://forums.unrealengine.com/t/question-about-garbage-collect-and-widgets/484702/1 "2021-06-10T06:43:18Z")

</div>

Hi,

I have a specific setup where an overview widget with 3 tabs dynamically creates widgets and adds them as a child to a vertical box inside the overview. Each tab contains a list of exercises (each exercise is generated as a widget with custom content and markup). Whenever I switch a tab, I clean up the vertical box and create a new list of exercises. However, constantly switching between these 3 tabs, causes many (old) widget instances to stay behind. I tried the “remove from parent” node but it doesn’t remove them from memory. There is also no proper way in UE4 to destroy a widget reference as you can do with an actor. I could reuse the created widgets again but they are dynamically generated and can have their status changed every time so then I would use bindings/ticks to update their content which is not good for performance.

I found a easy solution to remove all these unused instances that were created using a node called “ **garbage collect** ” whenever I want to clean up all old widgets and recreate only the widgets from an active tab still in memory. **However, are there any risks involved using this node?** I understand the principe of garbage collection but have no experience actively using it in UE4. Not sure if things may break elsewhere, when this node is called.

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [June 10, 2021, 8:39am UTC](https://forums.unrealengine.com/t/question-about-garbage-collect-and-widgets/484702/2 "2021-06-10T08:39:47Z")

</div>

> However, are there any risks involved  
> using this node? I understand the  
> principe of garbage collection but  
> have no experience actively using it  
> in UE4. Not sure if things may break  
> elsewhere, when this node is called.

I’ve been using it for years. Never run into anything unforeseen. For a widget to be garbage collected, it must have no parent container and be completely unreferenced. It’s actually not that easy to release widget memory…

If you Create Widget → Add to Viewport, the viewport is now _the parent container_ and the GC will not scoop it. For the Collect Garbage to kick in, you’d need to Remove From Parent & null Set all references, if any exist.

---

<div class="post-metadata">

**Author:** ![BioMedKUL](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@BioMedKUL](https://forums.unrealengine.com/u/BioMedKUL)\
**Post date:** [July 7, 2021, 9:34am UTC](https://forums.unrealengine.com/t/question-about-garbage-collect-and-widgets/484702/3 "2021-07-07T09:34:37Z")

</div>

Hi Everynone, thanks for your comment. I’ll keep using the garbage container then since it’s the only way I found to clean up any non-referenced widgets.
