5

This might be sort of a strange question, but curiosity got the best of me when I ended up getting a memory error after filling up a table with 14 million+ items.

Is there a sort-of set memory limit for Lua tables, or is it somewhat dynamic at all? I figured Lua itself would allocate so much memory in general, and the error would be thrown when that ran out, but that's just a wild guess. Anyone know for sure? Thanks.

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
Stephen Leitnick
  • 120
  • 1
  • 10
  • 1
    I have no sources, but as far as I know, Lua will take as much memory as it needs and can get. However, array-table allocation is to the power of 2, so you actually had an array-table with 14m items, it actually was 2^24 long – dualed Jul 10 '13 at 20:21
  • Source: http://www.lua.org/gems/sample.pdf page 19 – dualed Jul 10 '13 at 20:33
  • What error message did you get? – lhf Jul 10 '13 at 20:39

1 Answers1

7
t={}
for i=1,176000000 do t[#t+1]=i end

Tried out with a lua windows 64 bit binary - works fine.

a large lua state

A 64 bit luajit binary crashed. Seems like a bug of luajit.

Dmitry Ledentsov
  • 3,620
  • 18
  • 28
  • after 4gb lua64 bit stopped growing the table gracefully with a message `not enough memory` – Dmitry Ledentsov Jul 11 '13 at 06:59
  • 2
    [here](https://github.com/neomantra/lds)'s a reference that lua tables are subject to 4gb memory limit. In the [source](http://www.lua.org/source/5.1/ltable.c.html): `max size of array part is 2^MAXBITS` – Dmitry Ledentsov Jul 11 '13 at 07:11