如何获得"sizeof"? Lua的桌子吗? [英] How to get "sizeof" a table in Lua?

查看:164
本文介绍了如何获得"sizeof"? Lua的桌子吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在使用Lua涉足Love2D,并且刚刚实现了StateMachine来处理一组状态之间的转换,例如IntroState,MenuState,PlayState等.


I'm dabbling in Love2D using Lua and have just implemented a StateMachine to handle transitions between a set of states e.g. IntroState, MenuState, PlayState etc..

在以前的程序中,我通常释放仅是一次性交易"的对象和/或状态,在应用程序的生存期内,它将仅向播放器显示一次.在C ++中,我使用 sizeof 运算符,该运算符返回传递的对象,只是为了获得有关我在特定点释放多少内存的一些反馈.

In previous programs I usally release objects and/or states that are only a "one-time-deal", iow will only be presented to the player once during the lifetime of the application. In C++ I use the sizeof operator which returns the size in bytes of the passed object, just to get some feedback of how much memory I release at a certain point.

Lua中是否有相应的关键字或技巧来实现这一目标?

Are there any corresponding keyword or trick in Lua to achieve this?

推荐答案

如果您需要细粒度的信息,则可以使用

If you need fine-grained information, you can use getsize as @siffiejoe mentioned in combination with some table traversal to get to all local and global objects. If you need more coarse-grained approach, you can use collectgarbage('count') to get the total memory used by Lua.

此SO答案

This SO answer and this lua discussion on memory tracking may be of some help. Note that you don't have control over memory release as it's handled by the garbage collector (although there are several GC settings you can tweak).

这篇关于如何获得"sizeof"? Lua的桌子吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆