Causes of high lua heap?

Causes of high lua heap? On an empty baseplate you will only get 1mb of lua heap, there is 20mb in my game what are the causes and how can I reduce this?

Depends on what exactly you have in your game

Is it the same as the Baseplate template?

1 Like

I have a full game of scripts on an empty baseplate.

1 Like

O h g o s h

I’d personally recommend using as minimal scripts as possible, as those can effect performance, especially if they’re being fired all at once doing different actions

1 Like

Every Script creates its own local stack, over time this will build up into your LuaHeap

A few tricks you can do to reduce memory is:

  • Minimise Scripts at any cost, you can do this by iterating over models with the same functionality and apply accordingly
  • Optimise, optimise, optimise.
  • Be careful of memory leaks. Destroy does not remove the object until its references are removed.

If you need an explanation, here’s a small piece of code you can use

local Part = workspace.Baseplate
Part:Destroy()

wait(10)
print(Part) --> Baseplate. Even though the object was destroyed, it hasn’t been removed from memory
4 Likes

Okay so destroying things will not remove any connections?
I would need to remove them first then destroy the parent?

What would I need to do to remove all references

Destroy does the following

  1. Call Destroy on all children if it has any
  2. Parent the instance to nil, and lock the Parent property
  3. Disconnect all events

It does not however, remove any strong references, meaning Lua wont garbage collect it, and it will remain in memory.

2 Likes

Do I need to set the Part to Nil afterwards?

So just
local Part = workspace.Baseplate
Part:Destroy()
Part = nil

Not here, since the thread the script is under has died and all references are considered unneeded.

However, if you’re working with a global inside a loop that proceeds to become unused, yes.

Sorry for the technical explanation.

2 Likes

Thanks a lot it helped!
’ Every Script creates its own local stack, over time this will build up into your LuaHeap’
Does this include modules right now I have a serverscript that accesses many different modules to use an ability, what would you suggest?

Modules have a local stack, they also contain a cache of the value returned, so if you return a table. It will always return the same table.

1 Like

If my memory is right, if you want to use any code afterwards without having the part variable take memory, you can wrap that part of your code in do. Like this.

And then the Part variable should be garbage collected after and no extra memory to worry about

do
    local Part = workspace.Baseplate
    Part:Destroy()

    wait(10)
    print(Part) 
end
--// Other stuff
4 Likes