I want to be able to temporarily load code into memory, run it on an actor then remove it when not needed such that it gets removed from memory too.
Actor Components seem an obvious choice. I can add components at runtime and then remove them. But using the ‘Add Component’ and ‘Remove Component’ Nodes creates a Hard-References to the said components in the blueprints. And Hard References obviously mean the components will be loaded into memory even when not in use.
I don’t want that.
If using actors, I can load the actor at runtime using Soft-Reference and then destroy it when not needed. Thus it will get garbage collected.
I’m leaning on using Actors. But I wanted to confirm, If maybe there is another way to add components that does not create Hard References? Or should I just use Actors?
What do you think?
“Actors” are largely data. The code you build with Unreal is always “in memory” even if there are currently no actors that reference the code.
If you really need to load/unload actual code, then you need to use platform-specific methods to load/unload dynamic libraries. Note that OS-es may or may not actually unmap the code, even if you call dlclose()/FreeLibrary().
My main question is why you need to “unload the code,” though. What problem are you trying to solve?
It’s not much of a problem. It’s more of a cautious measure I’m trying to take while managing memory. I recently learned about Hard and Soft References, How to use Soft References to load meshes, particle systems, etc. to save on memory when not needed.
So, I thought I could do similar thing with actual code, load it when needed and unload later.
But now that I think about it, it’s causing me more problems than it is helping. I just checked and code blueprints (without any meshes, materials, etc.) don’t really take up much memory.
So, I’m gonna leave it as it is and use soft refs to only manage meshes, particle systems and other assets.
I might have been overthinking on memory management