I have a huge problem with memory allocation for c++ TArrays. If i use blueprints array, it works fast and easy. About 30ms for array of 4 016 016 elements and ~16mb memory.
But it should takes (401601643)+(4016016*2)=56 224 224 bytes.
56Mb is really not enough. But i still cant understand, why UE5 takes 30+ gb for allocation in c++, and only 100 mb in blueprints.
void ATerrainCreatorBase::InitMap()
{
// SetNum = Resizes array to given number of elements.
HeightMap.SetNum(4016016);
}
To:
void ATerrainCreatorBase::InitMap()
{
// Reserve = Reserves memory such that the array can contain at least Number elements.
HeightMap.Reserve(4016016);
}
and UE used 300 Mb for this. But should uses less than 2 mb. For 4016016 it will use 6+ Gb instead of 16 Mb. Still havenât idea where is this crazy memory leak happening.
Unfortunately, i canât use fixed size. For different maps i need different sizes. But blueprint version of Array hasnât problems with memory. I think i have a trouble with declaration or initialization of Array. Itâs still one engine, and there should be same variants for declaration. But I canât find the correct one.
Okay, one step closer. When I removed âUPROPERTYâ it started working much faster. One question - âUPROPERTYâ works bad always or some âPropertiesâ slow it down.
IMO an array of that size shouldnât really be exposed directly to Blueprint at all. Many Blueprint Functions and in some cases the VM itself will frequently copy arrays rather than modifying references, so itâs almost always far worse for performance. Event Functions for example seem to always invoke a copy, even when you pass elements by reference.
Hide it in C++ and provide accessor functions to modify it from BP to make sure you retain efficiency (at the very least, make sure itâs read-only). You have no control over how arrays are allocated in BP, so if you are adding/removing/editing the array frequently you are also reallocating it by default.