电晕:内存问题?
实际上我在这里有几个问题,希望有人能帮助我。
1)
print(collectgarbage("count"))
和
print(system.getInfo( "textureMemoryUse" ))
2)我有一个游戏,当你完成它后,它会跳回第一页,你可以再玩一次,我尝试输出textureMemoryUse,它不会显示内存泄漏,但是每次玩游戏时游戏都会变得更加滞后,除了内存泄漏之外还有其他原因会导致游戏滞后吗?
3)移除物体的正确方法是什么?我尝试做的是:
object:removeSelf()
object = nil
但是如果我使用collectgarbage(“count”)输出,它在使用之前和之后不会显示任何不同。
每次玩游戏都会卡顿,这已经让我痛苦了两天了,实在找不到我的代码有什么问题,希望能在这里得到一些帮助。
删除所有内存的代码
--remove all the transition that store in a table
local k, v
for k,v in pairs(transitionTable) do
--print(k)
timer.cancel( v )
v = nil; k = nil
end
transitionTable = nil
transitionTable = {}
-- remove movie clip
playButtonAnim:removeSelf()
playButtonAnim = nil
-- remove displayGroup and it's child
displayGroup.x = display.contentHeight + 10
displayGroup.x = display.contentWidth + 10
if(displayGroup ~= nil and displayGroup.numChildren ~= nil) then
for i=1,displayGroup.numChildren do
print("child on display group : "..displayGroup.numChildren)
displayGroup:remove(1)
end
displayGroup:removeSelf()
end
这是我尝试
collectgarbage("collect")
print( "collectgarbage is " .. collectgarbage("count") )
:在每一行上,发现删除影片剪辑部分会释放任何内存,我认为这不是删除它的正确方法?
actually i'm having few question here, hope someone can help me out.
1) what is the different between
print(collectgarbage("count"))
and
print(system.getInfo( "textureMemoryUse" ))
2) i have a game that after u finish it, it will jump back to 1st page and you can just play it again, i try to out put textureMemoryUse it doesn't show me memory leak, but yet the game become more lag each time i play it, is there any other reason would make it lag other than memory leak ?
3) what is the correct way to remove an object ? what i try to do is :
object:removeSelf()
object = nil
but if i out put by using collectgarbage("count") it doesn't show any different before and after i use it.
game being lagging for each time playing have been suffer me for 2 days, really can't find what's wrong in my code, hope to get some help here.
here is my code on remove all the memory
--remove all the transition that store in a table
local k, v
for k,v in pairs(transitionTable) do
--print(k)
timer.cancel( v )
v = nil; k = nil
end
transitionTable = nil
transitionTable = {}
-- remove movie clip
playButtonAnim:removeSelf()
playButtonAnim = nil
-- remove displayGroup and it's child
displayGroup.x = display.contentHeight + 10
displayGroup.x = display.contentWidth + 10
if(displayGroup ~= nil and displayGroup.numChildren ~= nil) then
for i=1,displayGroup.numChildren do
print("child on display group : "..displayGroup.numChildren)
displayGroup:remove(1)
end
displayGroup:removeSelf()
end
i try to :
collectgarbage("collect")
print( "collectgarbage is " .. collectgarbage("count") )
on each line and found that remove movie clip part din release any memory, i think it is not a proper way to remove it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
collectgarbage("count")
返回 Lua VM 分配的内存量。system.getInfo("textureMemoryUse")
返回分配的纹理内存量。任意数量的原因,包括代码中的
sleep(number_of_replays)
。 ;-)要从内存中完全删除对象,请在删除对象后多次运行
collectgarbage("collect")
(在某些情况下一次可能还不够)。话虽这么说,通常在每个帧上运行
collectgarbage("step")
会更好。 (不过,您可能需要调整 GC 步骤并暂停。)The
collectgarbage("count")
returns the amount of memory, allocated by Lua VM. Thesystem.getInfo("textureMemoryUse")
returns the amount of allocated texture memory.Any number of reasons, including
sleep(number_of_replays)
in the code. ;-)To completely remove an object from the memory, run
collectgarbage("collect")
several times after it is deleted (one may not be enough in certain cases).That being said, usually it is much better to run
collectgarbage("step")
on each frame. (You may want to tune GC step and pause though.)为了使用 collectgarbage 查看内存使用情况的变化,您需要在调用
collectgarbage("count")
之前运行collectgarbage("collect")
In order to see a change in memory use using collectgarbage, you need to run
collectgarbage("collect")
before callingcollectgarbage("count")
1)正如Alexander所解释的,collectgarbage("count")是Lua虚拟机使用的内存,而system.getInfo("textureMemoryUse")是使用的纹理内存。换句话说,前者是 Corona 使用了多少主内存,而后者是 Corona 在 GPU 上使用了多少内存。
2)很多因素都会导致延迟;我们需要了解的不仅仅是问题的一般描述。也就是说,根据您描述的症状,内存泄漏是我首先要寻找的。
3)这才是正确的做法。请注意,该命令返回的数字可能会波动,并且内存使用量在开始时不断增加是正常的,直到垃圾收集器最终赶上并且内存使用量下降。您不想在单独的行之后检查内存使用情况,而是想在程序运行时观察内存使用情况。
为了进行调试,您可以将该命令放入 EnterFrame 侦听器中以连续观察内存使用情况,然后在完成后注释掉该部分。
顺便说一句,我在 Corona 论坛上发布了关于删除对象如何工作的非常全面的解释:
http ://developer.anscamobile.com/forum/2011/01/14/how-do-i-tell-if-my-game-leaking#comment-16568
1) As Alexander explains, collectgarbage("count") is the memory used by the Lua virtual machine, while system.getInfo( "textureMemoryUse" ) is the texture memory used. Put another way, the former is how much main memory Corona is using, while the latter is how much memory Corona is using on the GPU.
2) Many things could cause lag; we'd need to know a lot more than just a general description of the problem. That said, with the symptoms you describe a memory leak is the first thing I'd look for.
3) That is the correct way. Note that the numbers returned by that command can fluctuate, and it's normal for the memory usage to continually increase at the beginning until the garbage collector eventually catches up and memory usage drops. You don't so much want to check memory usage after an individual line, so much as watch your memory usage over time as the program is running.
For debugging you can put that command in an enterFrame listener to watch memory usage continuously, and then comment that part out when you're done with it.
Incidentally, on the Corona forum I posted a pretty thorough explanation of how removing objects works:
http://developer.anscamobile.com/forum/2011/01/14/how-do-i-tell-if-my-game-leaking#comment-16568
有点晚了,但你永远不知道:
你忘记将对你的显示组的引用设置为零。
顺便说一句,如果您销毁一个组并且没有对子级的引用,则不必明确销毁子级。
不正确地清洁显示对象会使电晕变慢,这很可能是导致延迟的原因。
a bit late but you never know:
you forgot to set the reference to your displayGroup to nil.
btw, if you destroy a group and have no references to the children, you don't have to destroy the children explicitely.
not properly cleaning display objects makes corona slower, that's most probably the cause of your lag.