[New Wiki] Memory Management, Count References to Any Object, and Know Who is Referring!

Dear Community,

I’ve just released a new wiki that shows you how you can count references to any AActor/UObject yourself!

You can also get an Object list of exactly who is referring to your UObject!

The code is quite simple, and I am using built-in runtime engine tools.

Garbage Collection ~ Count References to Any Object

Enjoy!

Rama

Sample output:



TArray<UObject*> Referencers;
 
GetObjReferenceCount(this,&Referencers);
 
for(UObject* Each : Referencers)
{
	if(Each)
	{
		UE_LOG(YourLog,Warning,TEXT("%s"), *Each->GetName());
	}
}


You are the best!

Thanks Rama, this should come in handy!

I noticed the return value in your example should probably be “ReferredToObjects.Num()” instead of the array pointer.

I changed the name in the code at one point and must have not added that, but it is correct now on the wiki, so all is well :slight_smile:

I hope you are having fun today!

Hee hee!

:heart:

Rama

Thanks Rama! Great stuff!

Thanks again, Rama! This is very useful information.

hey, would it be possible to get an update of this code, with the wiki being shut down and all that?
im getting some really odd issues with GC and something like this would be extremely helpful for telling me what references arnt being resolved.

This seems to be the content of the original link
Garbage Collection ~ Count References To Any Object | Unreal Engine Community Wiki (unrealcommunity.wiki)

1 Like

The link above seems to be about counting the objects that my object references. I needed a way to count all the objects that reference my object instead. This seems to be the first result on Google for that sort of question, so here’s what worked for me in UE 5.3:

TArray<UObject*> GetReferencesToLiveObject(UObject* Obj)
{
    if(!Obj || !Obj->IsValidLowLevelFast())
        return {};

    FReferencerInformationList List;
    IsReferenced(Obj, RF_AllFlags, EInternalObjectFlags::AllFlags, true, &List);
    
    TArray<UObject*> Referencers;
    for (const auto& Reference : List.ExternalReferences)
        Referencers.Add(Reference.Referencer);

    return Referencers;
}

I personally chose to use AllFlags for those parameters because I didn’t want to miss anything, but potentially some of the results could be CDOs, or partway through destruction, or otherwise unhelpful. There might be nicer values that would exclude those.

Also, that Reference variable there has some other nice information in it that I didn’t need but might be useful to you:

  • The number of times the Reference.Referencer object actually references Obj
  • The properties of the Reference.Referencer object that are actually doing the referencing, stored in Reference.ReferencingProperties.
4 Likes