Issue with UMG and Removing Widgets from Memory

I’m banging my head against a wall and so frustrated; I’m greatly appreciative for any help. Here’s the situation:

I have a notification system that consists of a parent widget (literally just a vertical box) and children widgets which displays individual information. It works basically identically to Stardew Valley where anytime the character picks something up, a new child widget is added to show what they picked up and it disappears after five seconds or so.

But here’s the annoying part: regardless of how many times I call “Remove from Parent,” the widgets won’t clear from memory, and they aren’t even referenced in any variables. UE4’s garbage collection is supposed to automatically clear widgets from memory but that is clearly not working here.

Sidenote: calling remove from parent also doesn’t stop code from executing within my children widgets, which seems very odd to me.

I’ve greatly simplified the code to the following and still receive the same issue of widgets not leaving memory:

ParentWidget:

ChildWidget:
Screenshot_20220913_032909

Things I have tried:

  1. The parent widget variable is stored as a reference in my game mode bp. This is the only place it is referenced. I’ve tried clearing that variable and removing the parent from memory but all the children still remain in memory.
  2. I’ve tried calling “Get All Widgets of Class” for the children widgets and, using a for each node, manually removed from parent for all of them. This also does not work.

Things I am trying to avoid: calling “Garbage Collect.” I know I am manually able to call garbage collect (and it does work) but that is such a terrible solution as it causes FPS stutters and is such an unnecessarily intensive operation for what should have a simple solution (particularly doing such an operation for such a fundamental aspect of gameplay that the users will be experiencing a lot would be not great). Yes, I could batch and call Garbage Collect only every so often, but again, this feels like such an inelegant solution. There MUST be another way.

Thank you in advance for your help.

i don’t know about the garbage collection system. If you set the variable to nothing does that change things?

What I do for a similar system is that I have a widget container that is always part of the HUD and I just change its text/image values. So I am not creating or destroying anything, just updating text/image data from a data table.

Have you tried adding a “Clear Children” node to the VerticalBox?
I’ve used it before to clean out thousands of Child Widgets, and it seems to work

@burn_lz It’s not about widgets remaining in the VB. It’s about them lingering in the memory despite being dereferenced (not referenced to start with!) and removed from the parent container.


@mmmmGoodDinner Garbage Collection went through a significant number of changes between 4.20-4.25. Features were added and removed.

The results are not instantaneous, objects are not released immediately - the engine creates clusters in order to optimise the process.

Run something along the lines of on the side (with the class being the offending widget):

And… keep doing what you’re doing. It may take a minute but the widgets will go away.

You are a hero! Thank you so much… I totally wasn’t waiting long enough. One I waited a full minute and then initiated another UI element they were indeed cleared from memory.

In my last title I had the same issue but it never cleared, and so users experienced a memory leak after a few hours of gameplay. But my code practices weren’t as good then so it’s likely I did still have those widgets referenced somewhere and they weren’t being collected as a result. I suppose I was just hyper paranoid.

Thanks for taking the time to respond and explain things clearly!

1 Like