CollectGarbage_The Collector

CollectGarbage_The CollectorCollectgarbage- ItdoeswhatitsaysitdoesDefinitioncollectgarbage([opt[,arg]])Thisfunctionisagenericinterfacetothegarbagecollector.Itperformsdifferentfunctionsaccording

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全家桶1年46,售后保障稳定

Collectgarbage – It does what it says it does

Definition

collectgarbage ([opt [, arg]])This function is a generic interface to the garbage collector. It performs different functions according to its first argument, opt:

  • “collect”: performs a full garbage-collection cycle. This is the default option.
  • “stop”: stops the garbage collector.
  • “restart”: restarts the garbage collector.
  • “count”: returns the total memory in use by Lua (in Kbytes).
  • “step”: performs a garbage-collection step. The step “size” is controlled by arg (larger values mean more steps) in a non-specified way. If you want to control the step size you must experimentally tune the value of arg. Returns true if the step finished a collection cycle.
  • “setpause”: sets arg as the new value for the pause of the collector. Returns the previous value for pause.
  • “setstepmul”: sets arg as the new value for the step multiplier of the collector. Returns the previous value for step.

For now, let’s just focus on the collect and count options.

Let’s look at a few examples

We’ll just try a few things out and see how they’ll run. Remember to click on the green button to see what acctually happens with the code.

Counting memory usage

To find the number of kilobytes currently in use by Lua, you can simply make a call with count.

1 print(collectgarbage("count")*1024)
2 a = "123"
3 print(collectgarbage("count")*1024)

What you see in the console is the memory usage before and after the assignment. You should see around \sim 21 – 22 bytes of memory used up by the assignment.

Forcing a full run of garbage collection

If we don’t specify an option, then Lua will perform a full collection

1 a = {
1,2,3}
2 a = nil
3 collectgarbage()

Found in the wild

To top it off, let’s look at some real world uses of collectgarbage.

Keeping track of memory usage

This is usually used during “profiling”, or checking the performance of your code. For the most part, keeping an eye on the memory usage may ensure that your code doesn’t go through some crazy mood cycle and decide to blow up on you at the most unexpected time. For the most part however, since Lua still automates collection and keeps track of every by itself, this is unnecessary.

When you create new objects instead of reusing older ones

Since the notion of memory seems like such a faraway concept in Lua, it is easy to often forget that creating new objects take up memory, and certain patterns of coding could end up using up memory and then immediately discarding it faster than Lua can reclaim the wasted space.

In one extremely contrived example, there’s a good analogy of that new kid at school who wants to pretend to be cool, so he drew a mustache on his face on his first day; in the marginally more mature world of programming, that new kid is the guy who just read the wikipedia article on “lambda-calculus” (haven’t heard of it yet? don’t worry, you’re not going to need it unless you want to be a theoretical computer scientist) and decides to rub his knowledge in on /r/learnprogramming.

Now, this guy wants to implement the number one million through one million function calls to stay true to his “hip”ness, so he decides to write the following function to do just that. (Don’t worry about a function calling itself, it usually does what you think it will do)

01 function f(y)
02     local x = y + 1
03     return function() return f(x) end
04 end
05  
06 collectgarbage('stop')
07 g = f(0)
08 for i=1,10000 do
09     g = g()
10 end
11  
12 print(collectgarbage('count'))
13 collectgarbage('restart')

(Note: be very cautious when running that code, it does take a fair bit of memory and time so it may freeze your browser up)

It might not make sense that just calling functions and immediately discarding them could incur up so much memory usage, but just for the sake of illustration, just pretend that each function is equivalent to a table and that each function call is equivalent to looking up some element within that table that also turns out to be a table. In this case, we only care about the millionth inner nested table and none of the other ones; but in order to get to that inner nested table, we still need to keep the other tables in memory. Now to make matters worse, in the case of functions, we’re creating those tables on the go.

Anyways, do another collection and see how much memory was freed.

id=”dsq-2″ data-disqus-uid=”2″ allowtransparency=”true” frameborder=”0″ scrolling=”no” tabindex=”0″ title=”Disqus” width=”100%” src=”http://disqus.com/embed/comments/?base=default&disqus_version=28061892&f=theluatutorial&t_i=assert&t_u=http%3A%2F%2Fluatut.com%2Fcollectgarbage.html&t_d=The.Lua.Tutorial%20%C2%BB%20collectgarbage()&t_t=The.Lua.Tutorial%20%C2%BB%20collectgarbage()&s_o=default#2″ horizontalscrolling=”no” verticalscrolling=”no” style=”margin: 0px; padding: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; width: 620px; border-style: none !important; overflow: hidden !important; height: 75px !important;”>

Disqus seems to be taking longer than usual. Reload?

本文出自:http://luatut.com/collectgarbage.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/213486.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • GPU利用率低的解决办法

    GPU利用率低的解决办法watch-n0.1-dnvidia-smi#检查GPU利用率参数解决办法:1.dataloader设置参数2.增大batchsize3.减少IO操作,比如tensorboard的写入和打印。4.换显卡

    2022年6月30日
    72
  • Quartus-II 13 和Modelsim的安装「建议收藏」

    目录一、QuartusII的下载1、下载2、安装三、QuartusII的注册四、安装完成二、ModelsimSE的下载安装与注册一、下载二、安装三、ModelsimSE的注册四、安装完成一、QuartusII的下载1、下载百度网盘下载安装包链接:https://pan.baidu.com/s/1a9d-bq9RZmWrRV542X4IEA提取码:ifte2、安装复制这一串ID三、QuartusII的注册注册器下载:https://pan.baidu.

    2022年4月16日
    61
  • sql去掉重复的行_select去掉重复记录

    sql去掉重复的行_select去掉重复记录有重复数据主要有一下几种情况:1.存在两条完全相同的纪录这是最简单的一种情况,用关键字distinct就可以去掉example:selectdistinct*fromtable(表名)where(条件)2.存在部分字段相同的纪录(有主键id即唯一键)如果是这种情况的话用distinct是过滤不了的,这就要用到主键id的唯一性特点及groupby分组example:select*…

    2022年10月2日
    2
  • 舆情监测分析系统_舆情监测系统

    舆情监测分析系统_舆情监测系统一、引言1.1目的  编写此文档的目的是确认舆情分析系统的需求及系统边界,指导系统的设计。1.2项目信息项目名称:舆情分析系统项目提出者:指导教师开发者:东北大学软件学院大数据班T09实训项目组(lzf、lcx)用户:舆情分析员、系统管理员1.3缩写说明1.4术语定义1.5参考资料新浪舆情通:https://yqt.mdata.net/二、舆情分析系统概述2.1舆情分析系统介绍  我们的舆情分析系统主要包括舆情总缆分析、舆情搜索、文章分析、文章评论分析、事件

    2022年9月14日
    2
  • BIOS 和UEFI的区别

    BIOS 和UEFI的区别BIOS 和UEFI的区别

    2022年4月22日
    44
  • Django(22)Django执行SQL语句[通俗易懂]

    Django(22)Django执行SQL语句[通俗易懂]前言Django在查询数据时,大多数查询都能使用ORM提供的API方法,但对于一些复杂的查询可能难以使用ORM的API方法实现,因此Django引入了SQL语句的执行方法,有以下三种执行方式ext

    2022年7月28日
    6

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号