How to dispose UObject after using it in the editor.

Hello guys,

I’m experiencing a lot of issues with the UE4 memory management system. I have a couple of C++ scripts in my editor to process StaticMesh such as : Generating LODs, Collision testing and more. I’m processing up to 4000 huge static meshes through a loop but i’m not able to free memory after one is processed.

I tried a lot of things from this very cool tutorial : Assets Streaming

I’m out of RAM after ~400 processes because the meshes are still referenced in memory. I can’t use delete from naked C++ and what i’m trying to do is: Unload asset and call the garbage collector but it doesn’t seem to work.

Here is my c++ code. Thanks in advance :slight_smile:

FAssetRegistryModule& AssetRegistryModule = FModuleManager::LoadModuleChecked<FAssetRegistryModule>("AssetRegistry");
IAssetRegistry& AssetRegistry = AssetRegistryModule.Get();

// prepare filter
FARFilter Filter;
Filter.ClassNames.Add(UStaticMesh::StaticClass()->GetFName());
Filter.PackagePaths.Add(FName(FString::Printf(TEXT("%s"), *PathToEditorAssetsDirectory)));
Filter.bRecursivePaths = true;

// get assets without loading.
TArray<FAssetData> MeshAssetList;
AssetRegistry.GetAssets(Filter, MeshAssetList);

FStreamableManager StreamableManager;

for (FAssetData Data : MeshAssetList)
{			
     TSharedPtr<FStreamableHandle> Handle = 
     StreamableManager.RequestSyncLoad(Data.ToSoftObjectPath());
					
     UStaticMesh* Mesh = Cast<UStaticMesh>(Handle->GetLoadedAsset());
     check(Mesh);

   // use static mesh here !!!

   // try to dispose it here to free memory.
   StreamableManager.Unload(Data.ToSoftObjectPath());
   CollectGarbage(EObjectFlags::RF_NoFlags);
}