TArray::Shrink
#include "Containers/Array.h"
Access: public
Specifiers: inline
Description
Reallocates the backing buffer so it exactly fits the current number of elements, releasing any excess capacity (slack). Use after a large batch of removals when memory is tight.
Caveats & Gotchas
- • Shrink performs a reallocation which invalidates all raw pointers and references into the array. Do not hold ElementType* pointers across a Shrink call.
- • Many TArray removal operations (RemoveAt, Pop, Reset) have an EAllowShrinking parameter that triggers an automatic shrink when the slack ratio is high. Calling Shrink manually is only necessary when you need a guaranteed 0-slack state.
- • Has no effect if ArrayMax == ArrayNum (already perfectly sized). The check is cheap, so calling it unconditionally after a cleanup pass is safe.
Signature
UE_FORCEINLINE_HINT void Shrink() Return Type
void Example
Trim memory after filtering a large array C++
TArray<FString> Names;
// ... populate with thousands of entries ...
Names.RemoveAll([](const FString& S) { return S.IsEmpty(); });
Names.Shrink(); // release unused capacity after removal pass See Also
Tags
Version History
Introduced in: 4.0
| Version | Status | Notes |
|---|---|---|
| 5.6 | stable | — |
Feedback
Was this helpful?