View Single Post
11-23-16, 06:41 AM   #57
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,326
The frequency in which code is run doesn't automatically make it bad at memory management. It just makes it more important to watch what you do in your code. First of all is limiting dynamic creation of functions and tables in code that runs repeatedly. These are the most memory-expensive objects in Lua and their misuse can cause your memory usage to skyrocket.

To a lesser extent, but nonetheless does contribute to memory issues are string operations. Strings as a data type are handled internally in a unique way in which strings containing the same sequence of bytes share the same memory space. This means 100 strings of "something" takes up the same space as a few of them. However, when you start concatenating numbers and other data to them, they suddenly have different contents and are reallocated to a new space in memory.

For example:
Lua Code:
  1. for i=1,100 local str="Something"; end-- Generates only one string in memory containing "Something"
  2. for i=1,100 local str="Something"..i; end-- Generates 100 strings in memory containing "Something1" through "Something100"
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote