WoW:Memory usage

Revision as of 10:18, 20 January 2007 by WoWWiki>VoidvectorBot (Robot: converting/fixing HTML)


Some information on the memory usage of data in the WoW LUA environment (Expanding upon a forum posting).

Data

There are two basic classes of data that can be assigned to variables, passed around, etc, in LUA.

Values

Simple data types like numbers and booleans are represented as their actual values, and are passed around as such.

References

Complex data types (Userdata, Strings, and Tables) are represented as references to their actual value. Each reference fits in the same amount of memory as a value, but then the actual object that is referenced takes up additional space.

Strings

The contents of strings are immutable, so once created a string cannot be changed, just replaced with another string. This allows LUA to maintain a pool of every distinct string value in use, such that multiple occurrences of the same string are all in fact references to the same unchangeable string object.

If you have a table containing 30,000 entries of "Visit http://www.wowwiki.com/ to find out how to get more out of the World of Warcraft", there's actually only one instance of the text itself. Each occurrence of the string is a reference to that shared instance.

Tables

Unlike strings, tables can be altered, and may grow as entries are added to them. A table is made up of 3 parts:

  • The table itself - 32 bytes
  • 'List' entries - Those with consecutive integer indexes starting at 1 - 16 bytes each
  • 'Map' entries - Those with non-consecutive integer indexes, and string indexes.- 80 bytes each

An empty table is just the minimum 32 bytes. As you start adding entries then it will begin to accumulate memory in which to store those. Memory accumulation isn't linear however, instead there is an exponental algorithm, so that as the table gets larger, incrementally larger numbers of elements are pre-allocated.

For example, the size of a list of numbers increases as follows as new values are inserted: 32, 48, 64, 96, 96, 160, 160, 160, 160, etc..

Tables which mix both forms of values (list entries, and map entries) use a mix of the two entry types

Local Variables

Locally scoped variables in functions are stored on the stack, rather than being part of the garbage collected memory heap, however if the variable contains a reference type, then the actual data referenced is not on the stack. (Though it's not actually a stack, per se, see the LUA source code for details)