19、 Lua garbage collection of enterprise rapid development platform spring cloud+spring boot+mybatis+elementui

Microservice spring cloud 2022-06-24 07:47:19 阅读数:856


 Lua Garbage collection

Lua Automatic memory management . This means that you don't have to worry about how to allocate the memory required by newly created objects , It doesn't matter how to free up the memory occupied by objects after they are no longer used .

Lua Running a Garbage collector To collect all Dead man  ( That is to say Lua Objects that can no longer be accessed in ) To complete automatic memory management . Lua All memory used in , Such as : character string 、 surface 、 User data 、 function 、 Threads 、 Internal structure, etc , Are subject to automatic management . 

Lua Implements an incremental tag - Scan collector . It uses these two numbers to control the garbage collection cycle : Garbage collector intermittent rate and garbage collector step rate . Both figures are in percentages ( for example : value 100 Indicate internally 1 ).

The garbage collector intermittent rate controls how long the collector needs to wait before starting a new cycle . Increasing this value will reduce the enthusiasm of the collector . When this value is higher than 100 When I was a child , The collector will not wait before starting a new cycle . This value is set to 200 This causes the collector to wait until the total memory usage reaches It's twice as long before starting a new cycle .

The garbage collector step rate controls the ratio of the collector operation speed to the memory allocation speed . Increasing this value will not only make the collector more active , It also increases the length of each incremental step . Don't set this value less than 100 , In that case, the collector will work too slowly to finish a cycle . The default value is 200 , This indicates that the collector allocates in memory " twice as much " Work fast .

If you set the step rate to a very large number ( Larger than the number of bytes your program may use 10% ), The collector behaves like a stop-the-world The collector . Then if you set the interval rate to 200 , The behavior of the collector is the same as in the past Lua It's the same version : Every time Lua When the memory used doubles , Just do a complete collection .

Garbage collector function

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

  • collectgarbage("collect"):  Do a complete garbage collection cycle . Through parameters opt It provides a different set of functions :

  • collectgarbage("count"):  With K Returns... In bytes Lua Total memory used . This value has a decimal part , So just multiply by 1024 You can get Lua The exact number of bytes used ( Unless it spills ).

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

  • collectgarbage("setpause"):  take arg Set as the of the collector Intermittent rate . return Intermittent rate The previous value of .

  • collectgarbage("setstepmul"):  return Step rate The previous value of .

  • collectgarbage("step"):  Step through the garbage collector . step " size " from arg control . Pass in 0 when , Collector step ( Indivisible ) a step . Afferent non 0 value , Collector collection is equivalent to Lua Distribute these more (K byte ) Memory works . If the collector ends a loop, it will return true .

  • collectgarbage("stop"):  Stop the garbage collector . Before calling restart , The collector will only run because of an explicit call .

The following demonstrates a simple example of garbage collection :


mytable = {"apple", "orange", "banana"}
mytable = nil

Execute the above procedure , The output is as follows ( Note the changes in memory usage ):


copyright:author[Microservice spring cloud],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/175/202206240255124771.html