Empty tables consuming memory?

To keep it simple, I have a module that has a table referenced, created with table.create(1000, nil) in this example, I recently started using LuauHeap in the developer console to detect memory leaks, but with this, I notice that the empty table has memory allocated to it, after using table.clear() on it too, memory doesn’t seem to decrease.
image
image

Is this a way to reserve memory that doesn’t actually exist and ends up counting as actual memory, or an actual bug?

1 Like

its probably because if you do table.create(1000) then you should intend to use 1000 values and so they made it allocate the memory, but if you just set it to an empty table {} then you can have sparse indices without using memory

2 Likes

It would make sense. Would it be normal behavior that the allocated memory doesn’t lower after certain time? and would there be another way (besides setting the table to a new one, I assume) to get rid of the allocated memory?

1 Like

The point of table.create is to pre-allocate memory before filling it so that you can avoid unnecessary reallocations.

The reason table.clear doesn’t free the memory is because there is no extra information stored internally with the table about what allocated memory it has (because that would incur a significant cost for all tables).

It should be collected when the next cycle runs afterwards, though.

2 Likes

It should be collected when the next cycle runs afterwards, though.

Surprisingly, it seems that the allocated memory remains present after a while. Could it be that I haven’t reached a critical level of ram usage?

Another reason I can think of is probably because it is referenced in a module? Can’t for sure tell.

1 Like

Not 100%, but I’m pretty sure some sort of event has to occur that runs the garbage collector, which will then free up more memory.

1 Like

The GC runs based off a pacer algorithm that picks which opportune times to sweep and is very fined tuned for different hosts. As far as I know, the specific settings Roblox uses for their VM implementation isn’t documented and could easily change in the future.

Also, for clarity, Luau does not have emergency GC (when the host is running low on memory). That’s for the embedder to take care of.

1 Like

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