Hi guys,
Does anyone know why when creating an actor with the following test code, the memory starts at 600mb in the editor, then shoots up to 3000mb but when the actor is deleted it only goes down to 1800mb? I would have expected all of the memory to be returned so that we end up with 600mb again
MyActor.h
USTRUCT()
struct FMyStruct
{
GENERATED_USTRUCT_BODY()
UPROPERTY()
TArray<float> MyArray;
FMyStruct()
{
}
~FMyStruct()
{
MyArray.Empty();
}
};
UCLASS()
class TESTPROJECT1_API AMyActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AMyActor();
~AMyActor();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
virtual void BeginDestroy() override;
void OnConstruction(const FTransform& Transform) override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
private:
TArray<FMyStruct> MyStructArray;
void MyFunc(TArray<FMyStruct>& MyArrayRef);
};
MyActor.cpp
AMyActor::AMyActor()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = false;
bIsEditorOnlyActor = true;
#if WITH_EDITOR
bRunConstructionScriptOnDrag = false;
#endif
}
AMyActor::~AMyActor()
{
MyStructArray.Empty();
}
// Called when the game starts or when spawned
void AMyActor::BeginPlay()
{
Super::BeginPlay();
}
void AMyActor::MyFunc(TArray<FMyStruct>& MyArrayRef)
{
for (int32 Index = 0; Index < 10000000; Index++)
{
FMyStruct MyStruct;
for (int32 Index2 = 0; Index2 < 10; Index2++)
{
MyStruct.MyArray.Add(1.f);
}
MyArrayRef.Add(MyStruct);
}
}
void AMyActor::OnConstruction(const FTransform& Transform)
{
MyFunc(MyStructArray);
}
void AMyActor::BeginDestroy()
{
Super::BeginDestroy();
}
// Called every frame
void AMyActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
My thinking is that the destructor of the actor is emptying the MyStructArray and in turn that is then calling the destructor of the struct which is itself emptying its own MyArray of floats. This does not seem to be the case though as the memory is not being entirely returned.
Thanks in advance.