Lua Basics Tutorial
- Lua - Error Handling
- Lua - File I/O
- Lua - Coroutines
- Lua - Metatables
- Lua - Modules
- Lua - Tables
- Lua - Iterators
- Lua - Arrays
- Lua - Strings
- Lua - Functions
- Lua - Decision Making
- Lua - Loops
- Lua - Operators
- Lua - Data Types
- Lua - Variables
- Lua - Basic Syntax
- Lua - Environment
- Lua - Overview
Lua Advanced
- Lua - Game Programing
- Lua - Database Access
- Lua - Web Programming
- Lua - Object Oriented
- Lua - Garbage Collection
- Lua - Debugging
Lua Libraries
Lua Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Lua - Garbage Collection
Lua uses automatic memory management that uses garbage collection based on certain algorithms that is in-built in Lua. As a result of automatic memory management, as a developer −
No need to worry about allocating memory for objects.
No need to free them when no longer needed except for setting it to nil.
Lua uses a garbage collector that runs from time to time to collect dead objects when they are no longer accessible from the Lua program.
All objects including tables, userdata, functions, thread, string and so on are subject to automatic memory management. Lua uses incremental mark and sweep collector that uses two numbers to control its garbage collection cycles namely garbage collector pause and garbage collector step multipper. These values are in percentage and value of 100 is often equal to 1 internally.
Garbage Collector Pause
Garbage collector pause is used for controlpng how long the garbage collector needs to wait, before; it is called again by the Lua s automatic memory management. Values less than 100 would mean that Lua will not wait for the next cycle. Similarly, higher values of this value would result in the garbage collector being slow and less aggressive in nature. A value of 200, means that the collector waits for the total memory in use to double before starting a new cycle. Hence, depending on the nature and speed of apppcation, there may be a requirement to alter this value to get best performance in Lua apppcations.
Garbage Collector Step Multipper
This step multipper controls the relative speed of garbage collector to that of memory allocation in Lua program. Larger step values will lead to garbage collector to be more aggressive and it also increases the step size of each incremental step of garbage collection. Values less than 100 could often lead to avoid the garbage collector not to complete its cycle and its not generally preferred. The default value is 200, which means the garbage collector runs twice as the speed of memory allocation.
Garbage Collector Functions
As developers, we do have some control over the automatic memory management in Lua. For this, we have the following methods.
collectgarbage("collect") − Runs one complete cycle of garbage collection.
collectgarbage("count") − Returns the amount of memory currently used by the program in Kilobytes.
collectgarbage("restart") − If the garbage collector has been stopped, it restarts it.
collectgarbage("setpause") − Sets the value given as second parameter spanided by 100 to the garbage collector pause variable. Its uses are as discussed a pttle above.
collectgarbage("setstepmul") − Sets the value given as second parameter spanided by 100 to the garbage step multipper variable. Its uses are as discussed a pttle above.
collectgarbage("step") − Runs one step of garbage collection. The larger the second argument is, the larger this step will be. The collectgarbage will return true if the triggered step was the last step of a garbage-collection cycle.
collectgarbage("stop") − Stops the garbage collector if its running.
A simple example using the garbage collector example is shown below.
mytable = {"apple", "orange", "banana"} print(collectgarbage("count")) mytable = nil print(collectgarbage("count")) print(collectgarbage("collect")) print(collectgarbage("count"))
When we run the above program, we will get the following output. Please note that this result will vary due to the difference in type of operating system and also the automatic memory management feature of Lua.
23.1455078125 149 23.2880859375 295 0 22.37109375 380
You can see in the above program, once garbage collection is done, it reduced the memory used. But, it s not mandatory to call this. Even if we don t call them, it will be executed automatically at a later stage by Lua interpreter after the predefined period.
Obviously, we can change the behavior of the garbage collector using these functions if required. These functions provide a bit of additional capabipty for the developer to handle complex situations. Depending on the type of memory need for the program, you may or may not use this feature. But it is very useful to know the memory usage in the apppcations and check it during the programming itself to avoid undesired results after deployment.
Advertisements