- Sky
- Blueberry
- Slate
- Blackcurrant
- Watermelon
- Strawberry
- Orange
- Banana
- Apple
- Emerald
- Chocolate
- Charcoal
-
Content Count
2 -
Joined
-
Last visited
Posts posted by Akuukis
-
-
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
Memory management
in Tutorials
Posted
Thanks Sangar!
See the updated first post.