A brief analysis of Lua memory management _lua

Source: Internet
Author: User
Tags error handling garbage collection lua

Objective

In the long history, a variety of new languages, always accompanied by our programmers, sometimes, the need for work, we have to learn these very bright, very new language. Learning any language (I'm only talking about learning here) is nothing more than learning a few big modules, basic syntax, standard libraries, functions or object-oriented, memory management. As for Lua, I've summed up the previous modules, and today's article is mainly about memory management in Lua.

LUA provides support for these customizations at two levels. At a lower level, you can set up the allocation functions that LUA uses, and at a higher level, you can set some parameters to control the garbage collector or directly control the garbage collector. Let's start this trip now.

assigning functions

LUA completes all memory allocation and release operations through an "allocation function". This function must be supplied when the user creates a LUA state. A lual_newstate helper function is always used in the previous code, and this function creates the LUA state with a default allocation function. The default allocation function, which uses the Malloc-realloc-free function in the C standard library, is sufficient for a normal application, however, it is easy to gain complete control over the LUA memory allocation, simply by using the original lua_newstate to create the state:

Copy Code code as follows:

Lua_state *lua_newstate (Lua_alloc F, void *ud);

This function receives two parameters: assigning functions and user data. The state created in this manner invokes F to complete all memory allocations and releases. Because there are many strategies for allocating memory, and for the Lua_alloc distribution function analysis and explanation, also is not the focus of this article; This article, just a simple description of the LUA memory management, let you know that there is such a thing, so this article is the end of my purpose.

Garbage collector

Lua is a simple "mark-and-clean" garbage collector before version 5.0. Each cycle of this garbage cleanup consists of 4 stages: marking, sorting, cleaning and finishing. Lua sometimes pauses interaction with the main program in order to complete a complete garbage collection cycle. Next, a detailed description of each stage in a garbage cleanup cycle is made.

In the markup phase, LUA first marks objects in the root collection as active. objects in the root collection are objects that LUA can access directly, which are objects in the registry and primary thread objects. Lua then marks the objects that any program can access through the root collection object as active. This will cause all reachable objects to be marked "active."

Before the cleanup phase begins, Lua has to go into the finishing phase. This phase is the "finalization function" and the weak reference table. First, LUA iterates through all the UserData and finds all the userdata that are not tagged and have a –gc method. Then, Mark these userdata as "active" and put them in a separate list. This list will be used in the closing stage. Lua, on the other hand, iterates through all the weak reference table and deletes key and value that are not marked in the weak reference setting.

In the sweep phase, LUA iterates through all the objects. If the object that is currently being traversed is not marked, it is collected. Otherwise, LUA clears its markup to prepare for the next collection cycle.

Finally, the finalization phase, which calls their finalization functions based on the list of UserData generated in the collation phase. These calls are made at the last minute to simplify error handling.

For some APIs of the garbage collector, these APIs are not summarized here. And that's the end of the story. A short cut of the article, just take you over AH Lua's memory management rules, for the details of the problem, and not too much involved in later programming, encountered, and then detailed. The LUA series also has a temporary ending, and will continue to add new LUA articles if you encounter any problems. I hope my LUA series will be of some help to all of you, and I hope you will give me some suggestions.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.