There are already some posts around the forum having issues with Widgets and GC and it is still an issue. It is said that Widgets get GarbageCollected. I don’t see it working.
Here is the test project. Just press play. Widgets are in /Content
“A widget that is created using Create widget will look for the player, then the world, then the game instance. This means that unless all of them are removed (such as when the level is changed) the widget will continue to be referenced. As long as the widget is referenced, it will not be garbage collected.”
Seriously? I would not call that garbage collection if even the world needs to die before they get picked up. If the world is dead nobody cares about the garbage any more (Well, some might).
“A widget that is created using Create widget will look for the player, then the world, then the game instance. This means that unless all of them are removed (such as when the level is changed) the widget will continue to be referenced. As long as the widget is referenced, it will not be garbage collected.”
Yes, then it is working as intended. But GarbageCollection should collect the garbage while the game runs, not afterwards. When even the GameInstance needs to die to free up widget memory, the game is shut down. If the game is shut down, I don’t care if the GarbageCollector frees widget memory, as the process is done, dead, finished. The operating system will free the used memory anyway.
So it would be nice to have the widget garbage collection working during game time.
Using the public tracker you find that this was reported previously. However, it was closed as a won’t fix. I have provided a link to the public tracker. Please feel free to use the link below for further information.
This is a serious memory leak, and there’s no way to even clean up the mess manually.
How can that be a “won’t fix”?
Any system which dynamically creates widgets as part of its setup will leak memory each time it is activated.
The case where I saw this problem was while dynamically creating widgets for the sole purpose of rendering them to a texture. The widgets are only used inside the blueprint function which sets them up and renders them, they aren’t even drawn.
Each time the texture is rendered, more widgets are created and never garbage collected.
The standard expectation with garbage collection is anything that isn’t referenced any more will be cleaned up, but that isn’t the case here. It’s even worse in this case, because there’s no way to manually destroy or unload a widget once it’s been created.
After doing some digging I was able to find an issue dealing with hard references being created by the Create widget node’s output pin. This appears to have been resolved in 4.19. I have provided a link below to the public tracker. Please feel free to use the link provided for more information.
I can confirm. This is fixed in the 4.19.0-preview 1.
The excess widgets will accumulate, but will then be correctly cleaned up when the garbage collector activates. (I triggered the GC manually in my testing.)