[Tutorial] How to measure a table's memory byte size

Hello,

Recently I’ve had to deal with memory constraints with tables for storing data efficiently. So here is what you should do.

The method works in both client and server. It consists of encoding to a json string, and then using buffer to get the precise byte size:

local httpService = game:GetService("HttpService")

-- example table (must be encoded into json later on)
local tableExample = {"c21", "9v9", "v8", "c", "ezeae"}
local encodedJson = httpService:JSONEncode(tableExample)

-- size in bytes
local size = #encodedJson

The result:
image
(30 bytes)

And when testing on another tool, the result is accurate (30 B).

This method can be useful for API or Datastore requests, so use it as you need, as it has low overhead too.

Now this is a pretty short resource and tutorial, so please let me know if you have feedback.

2 Likes

Fyi you don’t need to use a buffer, you can just #encodedJson since each character is a byte anyway

Also, the actual runtime size of a table can’t be measured like this, you have to account for alignment bytes tag bytes yadayadayada but if you just want to measure the size your table would be in a datastore/for http requests this is actually a good method since datastores use json for storage and http requests use json just in general

(Also also, you should put this in Community Tutorials since it’s a tutorial)

1 Like

Hello, thanks for your insight. I thought it was worth doing that because I was scared of the byte size per character for things like emojis but after checking it looks like it doesn’t matter.
Thanks

1 Like