Out of memory / Asset loading management

Hi there,

I’m currently working on a pretty massive project, and we’re running out of memory pretty fast on some platforms, and we’re unsure as to why.

We’ve run a memreport while playing the game, and the number of objects loaded is absolutely absurd.

So basically my questions come in this form:

What exactly triggers assets to be loaded?

When making a streaming level invisible BUT loaded, are resources linked to this level wholly kept in memory?

Is there any documentation/tutorial on memory management in UE4? (Blueprint project)

Being more specific here, if I look at a uasset through the reference viewer, does everything referenced by and referencing to this particular object is automatically loaded, when that object is loaded?

If the last question is true, how can I limit the references to a certain much needed class all around such as the Game Instance object?

Would calling a function on the Game Instance through an interface instead of using a reference to the instance help limit the references to the Game Instance?

ANY Help towards answering ANY of these question would be of tremendous help.

Thanks everyone!

Adding some comments to these questions.

I’ve been testing out with memreport on a packaged version of the game, and loading additional streaming level increases the process physical memory usage, as expected of course, but when unloaded, none of the increased memory is freed. Is this normal behavior?

Basically here’s what’s happening:

I load the game, I make a memreport:

CommandLine Options:  
Time Since Boot: 42.23 Seconds

Platform Memory Stats for WindowsNoEditor
Process Physical Memory: 1023.96 MB used, 1344.32 MB peak
Process Virtual Memory: 3458.73 MB used, 3489.29 MB peak
Physical Memory: 23047.08 MB used,  1477.55 MB free, 24524.63 MB total
Virtual Memory: 3976.62 MB used,  1477.55 MB free, 134217728.00 MB total

I load a sub-level, I make a memreport:

CommandLine Options:  
Time Since Boot: 128.38 Seconds

Platform Memory Stats for WindowsNoEditor
Process Physical Memory: 1855.71 MB used, 1942.49 MB peak
Process Virtual Memory: 4406.66 MB used, 4558.07 MB peak
Physical Memory: 23999.25 MB used,  525.38 MB free, 24524.63 MB total
Virtual Memory: 4975.52 MB used,  525.38 MB free, 134217728.00 MB total

I unload the sub-level that I loaded, I make a memreport:

CommandLine Options:  
Time Since Boot: 217.59 Seconds

Platform Memory Stats for WindowsNoEditor
Process Physical Memory: 1963.81 MB used, 2125.04 MB peak
Process Virtual Memory: 4485.51 MB used, 4731.87 MB peak
Physical Memory: 21630.46 MB used,  2894.16 MB free, 24524.63 MB total
Virtual Memory: 5108.64 MB used,  2894.16 MB free, 134217728.00 MB total

None of the memory is ever released. Also note that during that time, we didn’t spawn any additional actors through code.

Is there any info on how ue4 manages memory through blueprint scripting anywhere?

Same here. There’s obviously something wrong with memory management in last version(s). Even the editor doesn’t release memory of unloaded objects. UE4 process is often taking more than 4Gb of RAM even with no window opened and on empty level, just because you opened a level before. I have to close and re open the editor.

I can anwser few of those quastions

What exactly triggers assets to be loaded?

When it needed, when you load asset that requires other assets those assets are also loaded when object of that asset is created or they are loaded manually . UE4 need them inside memory or else code will hit null pointers which cause crashes.

When making a streaming level invisible BUT loaded, are resources linked to this level wholly kept in memory?

Yes, visibility options is primarily for visual aspects without need to reload everything to make something visible again. Only thing you saving here is GPU work load. You need to unload level in order for it to be removed from memory.

Is there any documentation/tutorial on memory management in UE4?

I don’t think so, knowledge about is spread around everywhere, search anything about asset management and loading because it usually involved around that, don’t be scared of looking at C++ content too, also see below you might find something in links i give oyu.

Being more specific here, if I look at a uasset through the reference viewer, does everything referenced by and referencing to this particular object is automatically loaded, when that object is loaded?

Generly yes, UE4 memory management can’t specific what code gonna do with asset, so it load them all so there no null pointers. UE4 on C++ side can’t detect if code access the pointer and magically load the asset, object pointers there are raw memory addresses. In perspective on next question, keep in mind you reference initial asset by setting them on in default variables and component configuration, object on initiation needs them on when it is created, which in most cases are assets as practically only assets can be referenced on default. Also note that we talking here about objects (blue links), having class pointers (puprle links) that link to other blueprints should not load the asset until object from that class is made, so we talking here only about active objects which in most cases are assets.

If the last question is true, how can I limit the references to a certain much needed class all around such as the Game Instance object?

UE4 unload the object that no one reference them anymore (but after a while or inactivity), so set all variables with asset that are not used anymore to None or null pointer (nullptr) in C++. In case of actors keep in mind they will stay in memory as long as they are inside the world and world still exists, so destroy all not needed actors too or unload level with them.

Would calling a function on the Game Instance through an interface instead of using a reference to the instance help limit the references to the Game Instance?

No, Interface is just a type allowing to reference unrelated classes with common function tied to interface, it is not some special communication code, on C++ side it still same memory pointer pointing to same object, it just coding tool.

What you might look in to are soft object pointers (preciously Asset pointers) which gives more control over asset loading it:

They are also in blueprints under same name.

Hey there ,

Big thanks for your reply. That is extremely helpful!

In reference to your answer of the question about references (Also note that we talking here about objects (blue links)):

Take a look at the following example:

Suppose we have two Blueprints: MyGameInstance, and ActorBlueprint.

MyGameInstance implements a function called Tools_PrintString. Suppose ActorBlueprint calls the function Tools_PrintString on BeginPlay. We get the following graph in reference viewer:

Does that mean that as soon as the MyGameInstance class is loaded, it will automatically load the ActorBlueprint in memory, even if ActorBlueprint is not in the current level?

Memreport seems to suggest so.

The reason I ask this question is that in our game, most of the actors have multiple save state, and the Game Instance is the one that processes the checkpoint system. I think you can see the problem. As soon as we enter the level, every single blueprint in need of a checkpoint check is loaded, even if it’s not loaded in any opened level, because it calls the Game Instance to perform a checkpoint check.

So basically, the question is: is there a way for a blueprint to access the Game Instance without having to be loaded automatically? Maybe through soft reference as you mentioned?

I will be digging deeper on that issue soon enough, but if I can have some input or direction, that would be great.

Huge thanks for taking the time to help us!

Not sure how UE4 deals with it, but all blueprint are definitely loaded on start to create UClass object for them, that does not mean all the asset related to the blueprints are loaded too. looking to soft object pointers might be the way, on C++ there also more options, contacting with asset registry as well as implementing custom asset manager What is the Asset Manager? - Programming & Scripting - Epic Developer Community Forums you might look in to that too

Alrighty, big thanks for your help, I will be looking into both soft reference and custom asset management and will be coming back if a more specific question pops up.