TArray memory not freed in editor only actor

Hi Kehel,

I’m still struggling with it. Here’s the smallest example I have made that demonstrates the problem.

struct FMyDataStruct
{
	TArray<float> ArrayOfFloats;
	
	FMyDataStruct()
	{

	}
};

//CODE 1: This part DOES empty the RAM when run (ie: Run on main thread)
	/*for (int32 Index = 0; Index < 50000000; Index++)
	{
		FMyDataStruct MyDataStruct;
		MyDataStruct.ArrayOfFloats.Add(FMath::Rand());
		MyDataStruct.ArrayOfFloats.Add(FMath::Rand());
		MyMainArray.Add(MyDataStruct);
	}*/

	//CODE 2: This does NOT empty the RAM when run (The two floats * 50,000,000) are left in system memory after the actor is deleted)
	auto Result = Async(EAsyncExecution::Thread, [&]()
		{
			for (int32 Index = 0; Index < 50000000; Index++)
			{
				FMyDataStruct MyDataStruct;
				MyDataStruct.ArrayOfFloats.Add(FMath::Rand());
				MyDataStruct.ArrayOfFloats.Add(FMath::Rand());
				MyMainArray.Add(MyDataStruct);
			}
		});

Main array is defined in the actors header file as:

TArray<FMyDataStruct> MyMainArray;

I’m sure its something to do with Pointers/References/TSharedPtr/TSharedRef and the array or structs that the array contains going out of scope but I’m lacking in knowledge to know how to correctly fix it.

I’ve looked at and tried FRunnable, FNonAbandonableTask and now TFuture/Async. All show the same RAM not clearing problem.

I have tried implementing a TSharedPtr and TSharedRef to pass the main actors “MyMainArray” through to the Async code but have always run into problems which I do not fully understand.

All the articles I have seen online regarding Async code seem old, fragmented in their explanation or I just don’t understand them :confused:

Also: Yes I tried changing the code to AddZeroed etc but still the same problem :frowning:

Thanks for your help.

Kehel,

Just thought I would let you know that Bruno on the Unreal forum said to change the following:

struct FMyDataStruct
{
    ////TArray<float>ArrayOfFloats;
    TArray< float , TInlineAllocator<32> > ArrayOfFloats;

    FMyDataStruct(){}
};

Assigning the TInlineAllocator<32> seems to fix it. I changed it to 2 for testing.

I hope this helps someone looking.

Cheers :slight_smile: