Been refactoring last few days so I have been reading/watching a bunch on memory usage. I can’t find a hard answer to one questions though and I’ve read conflicting things.
As an example, I have a parent widget class for a “resource display” and there are a few instances of them on my player UI. If I have a hard ref to an Actor component (via “get comp by class” node) to pull resource information from, would each widget take up space in memory storing the hard ref??
So if the ref to the AC was 1Kb for example, and there were 6 widgets would 6Kb of space be used or since the component is already loaded would it not need to store and use those 6Kbs?
Would like the information in general not just for this specific use case.
Hello!
Regardless if you store hard reference or soft reference in a variable, it’s always just a pointer to an object, so it takes 8 bytes on 64 bit systems.
I’m not sure if UE doesn’t add another layer to pointers making them slightly bigger, but still these are bytes.
So every new variable that holds new widget takes those 8 or a few more bytes.
The difference between soft and hard reference comes when you don’t want to have an object loaded to a memory. So if you have only soft references, your widget won’t be loaded to memory (it’s textures, materials, etc).
But if you already have an object loaded, there is no any difference if you store 1 or 100 of them in separate variables (only those few bytes per variable).
Does the widget store the reference?
Yes → it costs.
No → it wouldn’t cost.
Further.
With BP any variable you create is allocated to memory.
So if you create a variable to store a reference, you automatically pay for the cost of the reference - regardless of it being initialized or set to a default, or even Null.
The cost of the variable in memory depends entierly on the variable type.
For isntance, a transform needs to allocate 2 vectors and a rotator, while a simple vector only allocates the vector3.
In c++ things are a little different.
Variables can be (and often are) just null.
When that’s true they don’t really cost you anything in memory but they are also not accessible.
Int? i = null
Vs
Int i = 0
In c# it’s similar.
The memory is only allocated when the value is not null.
But - and this is important -
checking the value of a null variable or using it, will error out with a null exception.
So you often do sruff like
If (i != null) {}
Before you utilize the value of I…