4.29. Lua garbage collection

发布时间 : 2025-10-25 13:34:07 UTC      

Page Views: 9 views

Lua automatic memory management is adopted. This means that you don’t have to worry about how to allocate the memory needed by newly created objects, or how to free up the memory occupied by objects after they are no longer in use.

Lua garbage collector is run to collect all dead objects that is, in the Lua objects that can no longer be accessed in to complete automaticmemory management. Lua all memory used in, such as strings, tables, user data, functions, threads, internal structures, etc., are subject to automatic management.

Lua incremental mark-scan collector is implemented. It uses these twonumbers to control the garbage collection cycle: the garbage collector intermittent rate and the garbage collector step rate. Both numbers use percentages (for example, a value of 100 represents 1 internally).

The intermittent rate of the garbage collector controls how long the collector has to wait before starting a new cycle. Increasing this value reduces the enthusiasm of the collector. When this value is less than 100, the collector will not wait before starting a new loop. Setting this value to 200 causes the collector to wait until the total memory usage reaches twice as much as before before starting a new loop.

The garbage collector step magnification controls the ratio of the collector’s operating speed to the memory allocation speed. Increasing this value not only makes the collector more active, but also increases the length of each incremental step. Do not set this value to less than 100, so the collector will work so slowly that it will never finish a cycle. The default value is 200, which means that the collector works at “twice” the speed of memory allocation.

If you set the step magnification to a very large number (10% more than the number of bytes your program might use), the collector behaves like a stop-the-world collector. Then if you set the intermittent rate to 200, the behavior of the collector will be the same as in the past. Lua the version is the same: every time Lua when you double the amount of memory used, do a complete collection.

4.29.1. Garbage collector function #

The following Lua functions are provided collectgarbage ([opt [,arg]]) used to control automatic memory management:

  • collectgarbage("collect") : Do a complete garbage collection cycle pass parameter opt , it provides a different set of functions:

  • collectgarbage("count") : returned in K bytes Lua total amount ofmemory used. This value has a decimal part, so you only need to multiply itby 1024. Lua exact number of bytes used (unless overflowed).

  • collectgarbage("restart") : Restart the automatic operation of the garbage collector.

  • collectgarbage("setpause") : set arg to the intermittent rateof the collector. Returns the previous value of the intermittent rate.

  • collectgarbage("setstepmul") : Returns the previous value of the step magnification.

  • collectgarbage("step") : Run the garbage collector step by step The stepsize is determined by arg control. When 0 is passed in, the collector takes a (indivisible) step. Pass in a non-zero value, and the collector collects the equivalent of Lua work of allocating this much (K-byte) memory. If the collector ends a loop, it returns true .

  • collectgarbage("stop") : Stop the garbage collector from running The collector runs only because of explicit calls before the call is restarted.

The following demonstrates a simple example of garbage collection:

Example #

mytable = {"apple", "orange", "banana"} print(collectgarbage("count")) mytable = nil print(collectgarbage("count")) print(collectgarbage("collect")) print(collectgarbage("count")) 

Execute the above program, and the output is as follows (note the change in memory usage):

20.9560546875 20.9853515625 0 19.4111328125 
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。