Lua Stack and Memory Limits

2018-05-02 12:12Edit this page

When using dynamically generated Lua code, you often need to be aware of Lua’s stack and memory limits. Here are the conclusions first:

Conclusions are based on actual runs of Lua 5.1.5 (Ubuntu official distribution) on Ubuntu 18.04

  • A Lua file cannot exceed 262144 constants
  • A control structure cannot exceed 32895 stacks
  • The number of upvalues cannot exceed 60
  • Each function in Lua cannot have more than 200 local variables

This article uses tt2 technology, and uses the command line tpage provided by Template::Tools::tpage to generate Lua code

A Lua file cannot exceed 262144 constants

Where 262144 = 2^18^

tt2 code

[% SET array = [1..262144] %]

local a = {
    "[% array.join("\", \"") %]"
}

Lua code

Generated Lua code gist link (large file): https://gist.github.com/xiaocang/cd947e57cdb6d16b83d7bdc9c4d0cecd#file-too_much_constant-lua

Execution result

$ lua too_much_constant.lua
lua: constant table overflow

Source code location

TODO


A control structure cannot exceed 32895 stacks

Where 32895 = 2^15^ + 2^7^ - 1

tt2 code

[% DEFAULT max = 32895 %]

if
[% FOREACH i IN [1..max] -%]
    ([% i %] > 0) [% IF i < max %] and [% END %]
[%- IF i % 10 == 0 %]
[% END -%]
[% END %]
then
    print("ok")
end

Lua code

Generated Lua code gist link (large file): https://gist.github.com/xiaocang/99a5b636694bab0fd870c17ef97fa472#file-control_pattern_too_long-lua

Execution result

$ lua control_pattern_too_long.lua
control_pattern_too_long.lua:3297: control structure too long near '<eof>'

Source code location

TODO


The number of upvalues cannot exceed 60

tt2 code

[% FOREACH i IN [1..200] -%]
local a_[% i %]
[% END %]

function closure()
    [% FOREACH i IN [1..61] -%]
    a_[% i %] = 0
    [% END %]
end

Lua code

Generated Lua code gist link (large file): https://gist.github.com/xiaocang/baf52c6e7fffa31d684ad55ddfc47867#file-too_much_upvalue-lua

Execution result

$ lua too_much_upvalue
lua: stdin:264: function at line 203 has more than 60 upvalues

Source code location

TODO


Each function in Lua cannot have more than 200 local variables

tt2 code

[% FOREACH i IN [1..201] -%]
local a_[% i %]
[% END %]
print("ok")

Lua code

Generated Lua code gist link (large file): https://gist.github.com/xiaocang/f4fd0b56bbdec00e4794e661b0ffd994#file-variables_in_function-lua

Execution result

$ lua variables_in_function.lua
main function has more than 200 local variables

Source code location

from lua5.2.2 src/lparser.c line 30-32

/* maximum number of local variables per function (must be smaller
   than 250, due to the bytecode format) */
#define MAXVARS     200

Unless otherwise stated, articles on this blog are licensed under the Creative Commons Attribution 4.0 International License. Please credit the original author and source when sharing.


Tags: lua

No comments yet

Leave a comment

Creative Commons © 2013 — 2026 xiaocang | Theme based on fzheng.me & NexT | Hosted by Netlify