# Nested TArrays in structs and memory

**URL:** <https://forums.unrealengine.com/t/nested-tarrays-in-structs-and-memory/2357416>\
**Category:** C++\
**Tags:** unreal-engine, CPP, question\
**Created:** [February 25, 2025, 2:13pm UTC](https://forums.unrealengine.com/t/nested-tarrays-in-structs-and-memory/2357416 "2025-02-25T14:13:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hawksed](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Hawksed](https://forums.unrealengine.com/u/Hawksed)\
**Post date:** [February 25, 2025, 2:13pm UTC](https://forums.unrealengine.com/t/nested-tarrays-in-structs-and-memory/2357416/1 "2025-02-25T14:13:42Z")

</div>

I have a question regarding memory usage of nested TArrays in structs, or just how memory works in general.

The problem i have is that a TArray of a struct that contains a TArray seems to be using much more memory than it should.

Here’s a picture to illustrate the problem:

 ![struct TArray memory](https://d3kjluh73b9h9o.cloudfront.net/original/4X/4/5/8/458d58e0a3bb5fc8ad302b0ae5b4401941ea5b54.jpeg)

Why is the third example using so much more memory than the other two? Is it just because the TArray has to be reallocated so many times?

---

<div class="post-metadata">

**Author:** ![Emaer](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/emaer/32/570877_2.png) [@Emaer](https://forums.unrealengine.com/u/Emaer)\
**Post date:** [February 25, 2025, 7:53pm UTC](https://forums.unrealengine.com/t/nested-tarrays-in-structs-and-memory/2357416/2 "2025-02-25T19:53:47Z")

</div>

A lot depends on the chosen allocator.  
Adding an element to an array can cause memory allocation. Since allocation is a fairly slow process, the allocator usually allocates more memory than is needed at the moment to amortize this cost. Hence the difference between capacity and size. If you allocate and add 100 elements to an array, and then remove 25, the array will still take up memory for 100 elements.

You can check how it behaves:

```cpp
TArray<int32> Arr;
int32 Capacity = Arr.Max();
for (int32 i = 0; i < 65536; ++i) {
  Arr.Add(i);
  if (Arr.Max() != Capacity) {
    Capacity = Arr.Max();
    UE_LOG(LogTemp, Warning, TEXT("Capacity changed %d"), Capacity);
  }
}

```

BTW instead of counting bytes in a loop you can use the `TArray::GetAllocatedSize()` function.

---

<div class="post-metadata">

**Author:** ![rokenrock](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/rokenrock/32/1040844_2.png) [@rokenrock](https://forums.unrealengine.com/u/rokenrock)\
**Post date:** [February 26, 2025, 8:32am UTC](https://forums.unrealengine.com/t/nested-tarrays-in-structs-and-memory/2357416/3 "2025-02-26T08:32:22Z")

</div>

As Emaer said, memory allocation is relatively slow on the heap, so we reserve multiple elements at a time. This is pretty standard with dynamically resizing arrays- ie in the standard library, gcc implements vectors with a 1.5x allocation (so 3-\>4, 6-\>9, 10-\>15, 1000-\>1500) iirc.  
Bringing us back to your third array, the reason it’s so much bigger is because it allocates for 4 elements.

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/8/1/0/81082beb5112ba2e672e2745dfc6efa15a05ef83.png)

By default (ignoring some optimizations), whenever the default sized allocator runs out of space, it will allocate according to this calculation:

```auto
const SIZE_T FirstGrow = 4;
const SIZE_T ConstantGrow = 16;
	
SIZE_T Grow = FirstGrow; // this is the amount for the first alloc

if (!bArrayIsEmpty)
{
	Grow = SIZE_T(NumElements) + 3 * SIZE_T(NumElements) / 8 + ConstantGrow;
}

```

Since this only happens on a resize, you can avoid this by explicitly reserving exactly how much you need, which is a good practice I see you already know of given the Reserve.

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/5/5/c/55ccc461740b716c3b9c35b303f6e495dcb259e5.png)

 ![The array now only holds exactly that one value](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/e/7/ce7a7522f52b319f4211a5e91620bcba07d59cbd.png)

  

> [@Emaer](#):
>
> then remove 25, the array will still take up memory for 100 elements.

I do want to point out that this is actually up to the individual developer in most cases, and the default behavior for most of the remove functions is to shrink the array so it doesn’t hold onto that memory. Sometimes this is a bool, but most of the time it’s an EAllowShrinking.

---

<div class="post-metadata">

**Author:** ![Hawksed](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Hawksed](https://forums.unrealengine.com/u/Hawksed)\
**Post date:** [February 26, 2025, 11:10am UTC](https://forums.unrealengine.com/t/nested-tarrays-in-structs-and-memory/2357416/4 "2025-02-26T11:10:46Z")

</div>

Did not know it would allocate for 4 or more at a time by default - thanks for the detailed explanation.

---

<div class="post-metadata">

**Author:** ![Emaer](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/emaer/32/570877_2.png) [@Emaer](https://forums.unrealengine.com/u/Emaer)\
**Post date:** [February 26, 2025, 1:31pm UTC](https://forums.unrealengine.com/t/nested-tarrays-in-structs-and-memory/2357416/5 "2025-02-26T13:31:02Z")

</div>

> [@rokenrock](#):
>
> I do want to point out that this is actually up to the individual developer in most cases, and the default behavior for most of the remove functions is to shrink the array so it doesn’t hold onto that memory. Sometimes this is a bool, but most of the time it’s an EAllowShrinking.

Of course. Generally if you add and remove elements from the array quite often, then setting shrinking=false would be a better solution. Otherwise, hmm, who cares 🙂

BTW I was honestly a bit surprised that `EAllowShrinking::Yes` is default in UE. It’s the opposite of std.

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [March 28, 2025, 1:31pm UTC](https://forums.unrealengine.com/t/nested-tarrays-in-structs-and-memory/2357416/6 "2025-03-28T13:31:59Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
