Hi everyone,
I did a simple test to released memory : Create a square mesh and spawn it for 1000 times, then destroy them all.
Here is the code to create square :
AGenerateMesh::AGenerateMesh()
{
PrimaryActorTick.bCanEverTick = false;
mesh = CreateDefaultSubobject<UProceduralMeshComponent>(TEXT("GeneratedMesh"));
RootComponent = mesh;
}
void AGenerateMesh::BeginPlay()
{
Super::BeginPlay();
CreateSquare();
}
void AGenerateMesh::CreateSquare()
{
TArray<FVector> vertices;
vertices.Add(FVector(0, 0, 0));
vertices.Add(FVector(0, 100, 0));
vertices.Add(FVector(0, 0, 100));
vertices.Add(FVector(0, 0, 100));
vertices.Add(FVector(0, 100, 0));
vertices.Add(FVector(0, 100, 100));
TArray<int32> Triangles;
Triangles.Add(0);
Triangles.Add(1);
Triangles.Add(2);
Triangles.Add(3);
Triangles.Add(4);
Triangles.Add(5);
TArray<FVector> normals;
normals.Add(FVector(1, 0, 0));
normals.Add(FVector(1, 0, 0));
normals.Add(FVector(1, 0, 0));
normals.Add(FVector(1, 0, 0));
normals.Add(FVector(1, 0, 0));
normals.Add(FVector(1, 0, 0));
TArray<FVector2D> UV0;
UV0.Add(FVector2D(0, 0));
UV0.Add(FVector2D(10, 0));
UV0.Add(FVector2D(0, 10));
UV0.Add(FVector2D(0, 10));
UV0.Add(FVector2D(10, 0));
UV0.Add(FVector2D(10, 10));
TArray<FProcMeshTangent> tangents;
tangents.Add(FProcMeshTangent(0, 1, 0));
tangents.Add(FProcMeshTangent(0, 1, 0));
tangents.Add(FProcMeshTangent(0, 1, 0));
tangents.Add(FProcMeshTangent(0, 1, 0));
tangents.Add(FProcMeshTangent(0, 1, 0));
tangents.Add(FProcMeshTangent(0, 1, 0));
TArray<FLinearColor> vertexColors;
vertexColors.Add(FLinearColor(0.75, 0.75, 0.75, 1.0));
vertexColors.Add(FLinearColor(0.75, 0.75, 0.75, 1.0));
vertexColors.Add(FLinearColor(0.75, 0.75, 0.75, 1.0));
vertexColors.Add(FLinearColor(0.75, 0.75, 0.75, 1.0));
vertexColors.Add(FLinearColor(0.75, 0.75, 0.75, 1.0));
vertexColors.Add(FLinearColor(0.75, 0.75, 0.75, 1.0));
mesh->CreateMeshSection_LinearColor(0, vertices, Triangles, normals, UV0, vertexColors, tangents, true);
}
here is the code to spawn square mesh
void ASpawnObject::SpawnObj()
{
for (int i = 0; i < 1000; i++)
{
AActor* SpawnItem = GetWorld()->SpawnActor(AGenerateMesh::StaticClass(), &Pos, (0, 0, 0));
SpawnedActorArray.Add(SpawnItem);
}
}
here is the code to destroy square mesh
void ASpawnObject::DestroyObj()
{
int ItemCnt = SpawnedActorArray.Num();
for (int i = 0; i < ItemCnt; i++)
{
AActor* actor = SpawnedActorArray*;
TArray<UActorComponent*> actorC;
actor->GetComponents(actorC);
for (auto aa : actorC)
{
UProceduralMeshComponent* bb = Cast<UProceduralMeshComponent>(aa);
if (bb)
{
bb->ClearAllMeshSections();
}
}
SpawnedActorArray*->RemoveFromRoot();
SpawnedActorArray*->Destroy();
}
SpawnedActorArray.Empty();
GEngine->ForceGarbageCollection();
}
void ASpawnObject::DestroyObj_GC()
{
int ItemCnt = SpawnedActorArray.Num();
for (int i = 0; i < ItemCnt; i++)
{
SpawnedActorArray*->MarkPendingKill();
SpawnedActorArray*->ConditionalBeginDestroy();
SpawnedActorArray* = nullptr;
}
SpawnedActorArray.Empty();
GEngine->ForceGarbageCollection();
}
I tried 2 way to destroy them and the result is similar.
The memory statistics :
Before spawned : 1011mb,
After spawned:1234mb,
After destroy 10 min: 1090mb
There is 80 mb memory not be released and the wait is too long.
Adjust the parameter in GC can improve it or not?
Is there any way to clear the memory?
Thanks everyone.