RealDocs

TArray::CountBytes

function Core Since 4.0
#include "Containers/Array.h"
Access: public Specifiers: const

Description

Reports the array's current and peak memory usage to an accounting archive by calling Ar.CountBytes with the used and allocated byte counts. Used by the engine's memory profiling infrastructure.

Caveats & Gotchas

  • CountBytes reports sizeof(ElementType) * ArrayNum as used and sizeof(ElementType) * ArrayMax as allocated — it does not account for heap allocations made by the elements themselves (e.g. FString members inside a TArray<FString>).
  • This function is only meaningful when called with an FArchive that implements CountBytes accounting (such as FArchiveCountMem). Calling it with a serialization archive is a no-op or undefined behaviour.

Signature

UE_NODEBUG void CountBytes(FArchive& Ar) const

Parameters

Name Type Description Default
Ar FArchive& Archive used for memory accounting; must support CountBytes() (e.g. FArchiveCountMem).

Return Type

void

Example

Measuring array memory usage C++
TArray<FVector> Positions;
Positions.Reserve(1000);

FArchiveCountMem MemAr(nullptr);
Positions.CountBytes(MemAr);
UE_LOG(LogTemp, Log, TEXT("Used: %llu bytes, Reserved: %llu bytes"),
    MemAr.GetNum(), MemAr.GetMax());

Version History

Introduced in: 4.0

Version Status Notes
5.6 stable

Feedback

Was this helpful?

Suggest an edit

Select a field above to begin editing.