Quick Memory Question

Hi,

Let’s say I have 10 characters all inheriting from a base character BP. 1 is a player character and 9 are AI characters.

My base character BP has a hard reference to a muzzle flash Niagara effect for when the character shoots his weapon. Let’s say the Muzzle Flash is 10MB in memory.

Does Unreal allocate 10MB of memory for the muzzle flash and point all 10 characters to the same memory slot, or does it create a separate memory slot for each character’s hard reference, so therefore using 100MB in memory?

If the former, is there any downside to keeping the muzzle flash as a hard reference if the player is likely to be using it regularly even if some of the AI don’t (i.e. are unarmed)?

Many thanks!

the thing about Hard reference vs soft reference has to do with the amount of memory consumed by Default_Objects (the template in which the instance objects are created from)

so if you mean that the 10MB number comes from the Size map that is the size on Disk of the Particle system that will be loaded into memory when anything that has a hard reference to that Particle system is loaded (or is when the Particle system is loaded into memory through the Asset Manager.

so that 10MB number will only occur once

for how much memory each instance of these Particle System takes that will be dependent on the amount of compute on the GPU that the Particle system needs. Particle Systems live on the GPU not system memory (mainly because they are graphics things, and they don’t typically interact with physics, or gameplay logic.

you can actually look up videos of like Enemy Randomize mods for games to see what effect multiple instance of a particle system can have on performance.

to determine the overall result of your Particle system would be done through Profiling the application (again for Particle system this should impact the GPU not the CPU

what I would summize you will see is the Particle system will be 10MB in size, with each reference to the Particle system would be 64bits (unless you are forcing the engine into 32bit mode), then the Transform of the Particle system (10-doubles and functions for the `FTransform) meaning less then a MB of system memory for each.

for the most part when it comes to when it matters for Hard-Reference vs Soft-Reference is:

  • how big is the default thing itself (this number will only occur once)
  • how many things are there (how many unique models/sounds/animations will there be, will there be 30 unique sword models, will each weapon type have its own animations, or will Sword 10 load an entire Shakespeare play of dialog when equipped)
  • how many of these things will be needed at any given time (will the player have all 30 of these swords at once, or will they only ever have 1 at a time)
  • if you only have 1 Player Character class then hard referencing that should be generally fine, because if the only Player Character isn’t in memory then bad bad things have happened.
  • for each thing that a more persistent object hard references will it have the same lifespan as that more persistent thing.
  • a cast is a hard reference so putting a cast to a class with a “heavy chain” in the game instance means that that is always in memory.

lets say in BP_PlayerCharacter you cast to BP_GoblinEnemy which inherits from BP_Enemy, which inherits from AEnemy in C++, which inherits from AGamePawn in C++, which then inherits from APawn you are loading the entire chain even though BP_Golbin is only in Level1, but because the cast is in the BP_PlayerCharacter BP_Goblin is always loaded
to soften then blow to memory on loading the instance of BP_PlayerCharacter with respect to the reference to the BP_GoblinEnemy (maybe a QA person wanted to spawn a bunch of them in to test AoE things) it could instead be a Soft_reference which is sent to a call to Some Manager to then spawn.
but if BP_GolishEnemy is going to be in every level then even a Hard-Reference to them might not be that much of an impact.
the other option is to clean up your design lets say that every level would have enemies, but not every level will have BP_GoblinEnemy, so instead of putting functions into BP_GoblinEnemy declare those functions in like AEnemy (or BP_Enemy) then redefine those functions in BP_GoblinEnemy and Cast the Actor to AEnemy (or BP_Enemy) when you need to call those functions or get values back.

I really appreciate all of the great info here.

I’m definitely using soft references and interfaces as much as I can and am currently refactoring a lot of my code away from hard references in the process.

It sounds like to my original question, if I have an object that needs to be in memory at all times, then it doesn’t matter too much if multiple characters hard reference it because they will reference the same object in memory?

they will reference the same default held on the disk, but at a very high view of it Yes.

remember that Soft References do have a bit of drawback in the form of needing to go out to disk to get the data and load it into memory (an SSD or Direct Storage will minimize this, but will not erase it) so you are trading memory overhead for time in loads (synchronously or Asynchronously)

Thanks very much!

Yeah, totally get not everything should be a soft reference. I’m thinking more about references that don’t need to be in memory much or need to be instantly on demand.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.