"Ran out of memory allocating x bytes with alignment 0" Error when saving-loading

Hello,

I have a survival type game, where I save certain variables from my actors, and when loading I first destroy existing actors (if any), then spawn fresh actors and restore variables to actors from save file.

However, only sometimes, my game crashes during saving or loading and I get the following crash message:



Fatal error: [File:D:\Build\++UE4\Sync\Engine\Source\Runtime\Core\Private\GenericPlatform\GenericPlatformMemory.cpp] [Line: 200] Ran out of memory allocating 50237006952 bytes with alignment 0

KERNELBASE
UE4Editor_Core
UE4Editor_Core
UE4Editor_Core
UE4Editor_Core
UE4Editor_Core
UE4Editor_Core
UE4Editor_StoneAgeColony_5538!TArray<TSparseArrayElementOrFreeListLink<TAlignedBytes<24,4> >,FDefaultAllocator>::ResizeTo() [c:\o\epic games\ue_4.21\engine\source\runtime\core\public\containers\array.h:2474]
UE4Editor_StoneAgeColony_5538!TSet<TTuple<FName,float>,TDefaultMapHashableKeyFuncs<FName,float,0>,FDefaultSetAllocator>::operator=() [c:\o\epic games\ue_4.21\engine\source\runtime\core\public\containers\set.h:250]
UE4Editor_StoneAgeColony_5538!ASettlementMember::RegisterActorDetailsToSave() [d:\stoneagecolony\source\stoneagecolony\settlementmember.cpp:278]
UE4Editor_StoneAgeColony_5538!GameSaver::RegisterActors<ASettlementMember>() [d:\stoneagecolony\source\stoneagecolony\gamesaver.cpp:89]
UE4Editor_StoneAgeColony_5538!GameSaver::SaveGame() [d:\stoneagecolony\source\stoneagecolony\gamesaver.cpp:45]
UE4Editor_StoneAgeColony_5538!AObjectBed::OnUsed() [d:\stoneagecolony\source\stoneagecolony\objectbed.cpp:31]
UE4Editor_StoneAgeColony_5538!AStoneAgeColonyCharacter::Use() [d:\unreal projects\stoneagecolony\source\stoneagecolony\stoneagecolonycharacter.cpp:454]
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Core
UE4Editor_Core
UE4Editor_Core
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_UnrealEd
UE4Editor_UnrealEd
UE4Editor
UE4Editor
UE4Editor
UE4Editor
UE4Editor
kernel32
ntdll


I store my variables into a class that inherits from “USaveGame”. When saving, I just store required variables to instance of this class, and when loading I read from save file and assign variables to my actors. For some variables, I created a struct and do saving-loading of variables through these structs. For example:



USTRUCT(BlueprintType)
struct FHumanCharacterDetails
{
    GENERATED_USTRUCT_BODY()

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Location")
    FTransform Transform;

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "SpecialID")
    FString SpecialID;

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "HomeSpecialID")
    FString HomeSpecialID;

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "ProfessionName")
    EProfession ProfessionType;

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "FaceDetails")
    TMap<FName, float> FaceDetails;

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Equipment")
    TMap<EEquipmentSlots, int32> EquippedItems;
};


I register variables like this:



void ASettlementMember::RegisterActorDetailsToSave()
{
    Super::RegisterActorDetailsToSave();

    // Assign details to struct.
    CharDetails.Transform = GetActorTransform();
    CharDetails.FaceDetails = MorphManager->FaceDetails;
    CharDetails.SpecialID = SpecialID;
    CharDetails.HomeSpecialID = HomeSpecialID;
    CharDetails.ProfessionType = Profession.Type;

    // Save equipments
    CharDetails.EquippedItems = EquipmentManager->EquippedItems;

    // Save details as struct to communicator. Which will be used during saving.
    Communicator::GetInstance().SpawnedCharacterDetails.Add(CharDetails);
}


And read back like this after spawning my actors during loading:



FTransform ActorTransform = Details.Transform;
auto Spawned = Communicator::GetInstance().World->SpawnActor<ASettlementMember>(ActorToSpawn, ActorTransform, SpawnParams);
Spawned->SetupBelongingSettlement();
Spawned->SpecialID = Details.SpecialID;
Spawned->HomeSpecialID = Details.HomeSpecialID;


Is there something i am missing? The fact that I get these errors only sometimes worries me. I tried closing-reopening editor and only sometimes I get this error. Also sometimes I get an error when reading from a data table which gives me an access violation error. But only like 1 in 10 tries.

Edit: Typo

Likely your Communicator Instance is null somehow, or the SpawnedCharacterDetails is getting nulled, and when it goes to add the CharDetails, it crashes because it’s trying to allocate some garbage value. It would be entirely random what garbage value it’s reading - so that could be part of it. Also if Communicator is a UOBJECT with no one holding on to it, it could be getting GC’d.

If you are using a Singleton pattern, I would highly suggest leveraging the Subsystem interface that went in with 4.22, much better way to guarantee lifetime.

Yes my Communicator class is a singleton. I will look into Subsystem interface thanks.

But Communicator class is not a UCLASS and variables it is holding are not UPROPERTY either. Can this be the problem?

I have tried forcing garbage collection to see if GC deletes some objects, but it had no effect on crashes I have experienced.

Doesn’t look like you’re storing UObjects so it should be fine (maybe not best practice, but still fine).

The problem is definitely that assignment operation:



    // Save equipments
    CharDetails.EquippedItems = EquipmentManager->EquippedItems;


Maybe Equipment Manager is null at that point so EquippedItems is trash? No idea, you’re going to have to add some defensive coding (nullptr checks, size checks, etc) and see if anything hits.

I think I fixed the problem.

Error log was saying that my program was crashing at this line:



 CharDetails.SpecialID = SpecialID; 

However, it was crashing 1 line above, which is


 CharDetails.FaceDetails = MorphManager->FaceDetails;  
 

I was trying to forcing garbage collector to see if it was garbage collected and became null or not. Forcing garbage collector has no effect on the problem. However, I realized that I only experience the problem after waiting a while. Probably GC is collecting it somehow? Im not sure.

I solved the problem by marking these variables with UPROPERTY(). I also marked SpecialID as UPROPERTY() aswell.



    UPROPERTY()
    FHumanCharacterDetails CharDetails;
    UPROPERTY()
    UMorphManager* MorphManager;
    UPROPERTY()
    UEquipmentManager* EquipmentManager;
    UPROPERTY()
    TMap<EEquipmentSlots, int32> EquippedItems;


Hi! I just wanted to add to this issue because I got here trough google and maybe some other dork like me can use this. In my case the issue was caused by not having the virtual memory enabled in Windows. I set it to Automatically manage paging file size for all drives and the issue was gone.

I am having the same problem and I have been trying for days to find a sollution. I read from another member that you should do exactly the opposite, meaning that you have to define manually the size of virtual memory and not let windows decide automaticaly. In my case the default setting was to automaticaly define the size so I changed it manually (I have 32GB of RAM). The problem was not solved. I tried to change it again like you suggest , but the problem is still there.
Here are the last lines of the log file.

UATHelper: Packaging (Windows (64-bit)): LogMemory: Warning: Freeing 33554432 bytes from backup pool to handle out of memory.
UATHelper: Packaging (Windows (64-bit)): LogMemory: Warning: MemoryStats:
UATHelper: Packaging (Windows (64-bit)): AvailablePhysical 15481315328
UATHelper: Packaging (Windows (64-bit)): AvailableVirtual 140730124279808
UATHelper: Packaging (Windows (64-bit)): UsedPhysical 2679300096
UATHelper: Packaging (Windows (64-bit)): PeakUsedPhysical 2679304192
UATHelper: Packaging (Windows (64-bit)): UsedVirtual 2856173568
UATHelper: Packaging (Windows (64-bit)): PeakUsedVirtual 3797598208
UATHelper: Packaging (Windows (64-bit)): LogWindows: Error: === Critical error: ===
UATHelper: Packaging (Windows (64-bit)): LogWindows: Error:
UATHelper: Packaging (Windows (64-bit)): LogWindows: Error: Fatal error: [File:D:/Build/++UE4/Sync/Engine/Source/Runtime/Core/Private/GenericPlatform/GenericPlatformMemory.cpp] [Line: 186]
UATHelper: Packaging (Windows (64-bit)): LogWindows: Error: Ran out of memory allocating 18446744072014652240 bytes with alignment 0
UATHelper: Packaging (Windows (64-bit)): LogWindows: Error: [Callstack] 0x00007ffdfd841458 UnrealPak-Core.dll!FGenericPlatformMemory::OnOutOfMemory() ]
UATHelper: Packaging (Windows (64-bit)): LogWindows: Error: [Callstack] 0x00007ffdfd8baa6d UnrealPak-Core.dll!FMallocTBB::Malloc() ]
UATHelper: Packaging (Windows (64-bit)): LogWindows: Error: [Callstack] 0x00007ffe08627115 UnrealPak-PakFileUtilities.dll!FMemoryCompressor::FMemoryCompressor() ]
UATHelper: Packaging (Windows (64-bit)): LogWindows: Error: [Callstack] 0x00007ffe08631718 UnrealPak-PakFileUtilities.dll!FCompressedFileBuffer::CompressFileToWorkingBuffer() ]
UATHelper: Packaging (Windows (64-bit)): LogWindows: Error: [Callstack] 0x00007ffe08631375 UnrealPak-PakFileUtilities.dll!CreatePakFile'::2’::FAsyncCompressor::Compress() ]
UATHelper: Packaging (Windows (64-bit)): LogWindows: Error: [Callstack] 0x00007ffe08632e60 UnrealPak-PakFileUtilities.dll!CreatePakFile() ]
UATHelper: Packaging (Windows (64-bit)): LogWindows: Error: [Callstack] 0x00007ffe0863e06e UnrealPak-PakFileUtilities.dll!ExecuteUnrealPak() ]
UATHelper: Packaging (Windows (64-bit)): LogWindows: Error: [Callstack] 0x00007ff6f013a5c7 UnrealPak.exe!wmain() ]
UATHelper: Packaging (Windows (64-bit)): LogWindows: Error: [Callstack] 0x00007ff6f013b338 UnrealPak.exe!__scrt_common_main_seh() ]
UATHelper: Packaging (Windows (64-bit)): LogWindows: Error: [Callstack] 0x00007ffe5a377034 KERNEL32.DLL!UnknownFunction ]
UATHelper: Packaging (Windows (64-bit)): LogWindows: Error: [Callstack] 0x00007ffe5b0fcec1 ntdll.dll!UnknownFunction ]
UATHelper: Packaging (Windows (64-bit)): LogWindows: Error:
UATHelper: Packaging (Windows (64-bit)): LogWindows: Error:
UATHelper: Packaging (Windows (64-bit)): LogWindows: Error:
UATHelper: Packaging (Windows (64-bit)): LogWindows: Error:
UATHelper: Packaging (Windows (64-bit)): UnrealPak terminated with exit code 3
UATHelper: Packaging (Windows (64-bit)): ERROR: UnrealPak failed
UATHelper: Packaging (Windows (64-bit)): (see C:\Users\User\AppData\Roaming\Unreal Engine\AutomationTool\Logs\E+UE_4.25\Log.txt for full exception trace)
UATHelper: Packaging (Windows (64-bit)): AutomationTool exiting with ExitCode=1 (Error_Unknown)
UATHelper: Packaging (Windows (64-bit)): BUILD FAILED
PackagingResults: Error: Unknown Error
LogSlate: Took 0.000138 seconds to synchronously load lazily loaded font ‘…/…/…/Engine/Content/Slate/Fonts/DroidSansMono.ttf’ (77K)

Does it matter that the project is in C: and I am packaging in the E: drive?
Also, my free space in C: is kinda limited (about 40GB). Does this matter?
Any help would be highly appreciated.

That’s one kind of problem that can appear when your save system is running recursive functions and/or causing C undefined behavior, e.g: returning a reference to value of a variable declared locally in body of a function or another thread.

There are situations with save systems where you should get a copy of a value instead, but it’s difficult to determine clear rules because serialization can be achieved in so many different ways, some paths leading to this…

Thank you for your reply, but I have to admit that my programming skills are not enough to clearly understand what you are saying. Do you imply that a variable or a function is causing this? My project uses the default VR template (motion controller) but I have changed the way the VR pawn moves. It does not teleport but uses the thumbsticks to move in every direction. I am also using 6 identical landscapes 4033x4033 in one level without level streaming (I know that it is wrong but there is a reason for that). I am also using a pretty big light importance volume and about 40 post process volumes. Could these probably cause the problem?

No, I was talking about original post.
Your problem is actually not related to op.

You seem to have a hardware issue that leads to the same assertion failure.

I was having this same error, I just unchecked “Create compresed cooking package” and “exclude editor content when cooking” within the project settings> Packaging, so everything went back to normal