Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
Akuukis

Memory management

Recommended Posts

Hello all,

As we know, robots (and computers) have low memory, therefore I wanted to find out how much RAM.

Everything below is empirically tested on 1MB (=1024KB) robot with OpenOS installed, the following script (http://pastebin.com/Fn34HXLr)

 

I found out, that

  • 10 is a magic number. If you use os.sleep(0) once it doesn't guarantee that garbage collector will run, and even for 9 times garbage collector may not run
  • OpenOS uses approximatelly 210KB of RAM (+/- 20KB)
  • Booleans, numbers and strings uses 9 bytes of RAM. At some size numbers become floats instead of doubles, but I cannot find the max size for string.
  • Pointer to function uses 9 bytes of RAM.
  • Empty function (function() end) uses 93 bytes of RAM, function (function() return true end) uses 102 bytes of RAM.
  • Pointer to table uses 9 bytes of RAM.
  • Empty table uses 36 bytes of RAM plus more memory depending on its size, and tables don't shrink - if they have less elements later, they don't use less memory.
    • 0.009 KB of RAM for table with 0-1 elements
    • 0.018 KB of RAM for table with 2 elements
    • 0.036 KB of RAM for table with 3-4 elements
    • 0.071 KB of RAM for table with 5-8 elements
    • 0.142 KB of RAM for table with 9-16 elements
    • 0.284 KB of RAM for table with 17-32 elements
    • 0.569 KB of RAM for table with 33-64 elements
    • 1.138 KB of RAM for table with 65-128 elements
    • 2.276 KB of RAM for table with 129-256 elements
    • 4.551 KB of RAM for table with 257-512 elements
    • 9.102 KB of RAM for table with 513-1024 elements
    • 18.204 KB of RAM for table with 1025-2048 elements
    • 36.409 KB of RAM for table with 2049-4096 elements
    • 72.818 KB of RAM for table with 4097-8192 elements
    • ...
  • More to come.

What does that changes?

  • Now you know how much does it cost to have redundant variables or tables.
  • Always use local, those free up space when not used anymore but globals hang around forever (unless you make local environment).
  • It is a good idea to consider to serialize information.. For example table with 3 variables (like coordinates) uses 8 times more RAM than one number.
  • If you cap table size (like maximum history entries for console), cap it at number that's a power of 2 to use memory more efficiently.
  • If you have deep recursions or long iterations, put in "os.sleep(0)" or something that yields here and there, it forces garbarage collection

Thanks for extra info to:

* Sangar

Link to post
Share on other sites

function freeMemory()
  local result=0
  for i=1,10 do
    result = result + c.freeMemory()/10
  end
  return result
end
No yield / sleep in the loop => not guaranteed to run GC. Also, better use math.max: gets you the most free memory observed.

function freeMemory()
  local result = 0
  for i = 1, 10 do
    result = math.max(result, c.freeMemory())
    os.sleep(0)
  end
  return result
end
Regarding os.sleep and garbage collection, see the relevant line in the kernel.

Other random things I remember about Lua internals that may or may not be related / helpful:

  • Short strings are 'interned', i.e. only one immutable copy of the actual string is stored (short = LUAI_MAXSHORTLEN = 40).
  • Tables' index-data never shrinks. I.e. when you push 1000 objects into a table and then remove them again, the table will still consume more memory than before.
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.