Where do we go when we AssetDatabase.LoadAssetAtPath

I have a class (not a monobehaviour) which needs to access a bunch (1000+) assets, fetch a component off each one read some data from them which gets written to a json file.

My current system does this but its generating a lot of garbage… Some of the assets are over 1gb (volumetric point cloud captures) and so once the process finishes Unity is holding 20gb+ of memory which it gives back after I restart the editor.

I’m tracking down spots where I can optimise the process and I was thinking that possibly my use of AssetDatabase.LoadAssetAtPath could be causing issues:

foreach (var prefabPath in prefabPaths)
{
    if (prefabPath.Contains(".meta")) continue;
    var go = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(GameObject)) as GameObject;

    var contentData = go.GetComponent<IContentData>();
    if (contentData == null)
    {
        EditorUtility.DisplayDialog("Warning",
            $"Couldn't find a valid ContentID script on {go.name}. Skipping it.", "Ok");
        continue;
    }
   
    AA_Tools.AssignPrefabToAddressableGroup(prefabPath, addressableGroup, false);

    m_contentCatalogListings.AddContentToCatalogListing(contentData);
}

When I use this call, I’m loading the prefab into memory (I’m assuming here) but then perhaps it only releases that memory after the for loop is completed? or maybe not until the editor is restarted?

Is there a way that I can manually release the object (I tried GameObject.Destroy(go); but this did not work, all I got was 1000+ errors at the end saying I couldn’t do that).

Give this a whirl: Unity - Scripting API: EditorUtility.UnloadUnusedAssetsImmediate

1 Like

Ah thank you!

Should I be running this after each asset is unneeded? or should I batch it to run after maybe every 10 assets have been processed. The documentation mentions “after walking the whole game object hierarchy”, this sounds like a time consuming process…