Can I measure memory size of array variable at runtime?

I am using a 2d vector array to store the player characters position.

If I understand correctly, a basic int array can hold like 2 billion indexes at maximum. I assume a 2d vector array would be like half of that, right?

So, if I record the players position as a 2d vector each second, and a game session last 60 minutes, I would have 3,600 indexes in the array.

My assumption is that this is practically nothing in terms of memory. But is there a way (in blueprint) that I can watch the size (memory size) of a variable?

I don’t think there will be any issue but for curiosities sake I think it would be useful to see what happens at runtime.

There isn’t anything in BP that will give you the allocated memory size.

Depending on purpose you may consider a buffer approach. Every n sec dump or write to drive. Freeing the buffer. Or you can limit buffer length and write oldest to drive → remove.
This is what Rewind Time Lag Compensation uses for player positions/states etc.

Character Movement Component does a similar process with FSavedMove_Character && SavedMoves.

The Replay system buffers states, position etc in memory, then dumps to drive in a similar way.

1 Like

thank you @Rev0verDrive

Some new terminology for me but I think I get the gist. I’ll take a look at those examples and I probably need to research to find out what dump/write and buffers really mean.

In general terms a Buffer is simply an array of set length. Say 100 (indices 0-99). When the length of the array reaches 100 you extract/remove index 0 (oldest). The array now has a length of 99 (indices 0-98).

The examples I mentioned add an entry every frame. So essentially once they reach their buffers max length they are adding and removing an entry every frame.

Dump == removing an index from the array, thus making room for the next newest entry.

Writing to drive is literally writing the value (removed index value) to the hard drive. On init you’d create a file on the hard drive. With each buffer removal/dump you write the removed value to the file.

You can’t do the create/write etc to file with BP. You’ll have to this with C++.

1 Like

Ah okay, this is similar in theory to something that I’ve done before. I did something that I think you might call a command queue. Not exactly the same, but the idea is you have an array and first value that went in will be the first to go out. I wasn’t doing any sort of memory management at all - just the basic array management is similar I think.

And so, by transferring a value from the array to the hard drive, the idea is just that you are controlling how much memory usage is on the quick accessed memory (RAM?), by pushing incremental bits to the hard drive?

If that is correct, what sort of situations do you use this technique for?

Correct, memory and performance management…though in some situations you will take a perf hit when writing to drive. Say for example using the Replay system which uses DemoNetDriver and Streamers.

Replay needs a copy of every players movement location/rotation, state etc. It also needs the locations/states of all physics actors, and other events/actions that happened during the game. It uses this data to re-enact game play. These files are usually around 10-20 MB.

If you absolutely need the values, then you offload them. Otherwise you just remove them. CMC and Rewind Time Lag Comp dump. There’s no need for the values after a set time threshold.

1 Like