Memory Leak in My Game?

I am currently in the process of creating a game that I hope one day will be
released to the app store. I have come across a problem where every time I
press the new game button, memory is leaked.

This is how I determine how much memory my program is using:
[blockcode]
local function monitorMem(e)

collectgarbage()
print( "MemUsage: " … collectgarbage(“count”) )

local textMem = system.getInfo( “textureMemoryUsed” ) / 1000000
print( "TexMem: " … textMem )

end

local memoryTimer = timer.performWithDelay(1, monitorMem, 0)
[/blockcode]

When I first run my program this is the result:
MemUsage: 315.283203125
TexMem: 19.188736

After I press newGame the result is always very close to:
MemUsage: 368.228515625
TexMem: 20.826112

I have been struggling to fix this problem for over a week and was wondering if anyone has any
suggestions or has come across a problem like this of their own.

I will post code if necessary, but I want to make sure it isn’t some bug or that someone knows the solution
based on their own experiences.

Thanks in advance,
David

[import]uid: 152065 topic_id: 32153 reply_id: 332153[/import]

Hi David,

It would help if you posted some code for your newGame function, but in the meantime, two common reasons to have a memory leak are:

  • Creating a display object or group and not calling [lua]obj:removeSelf()[/lua] and [lua]obj == nil[/lua] when you are done using it

  • Storing a [lua]timer.performWithDelay[/lua] in a variable and not setting that variable to nil when the timer is complete

  • Andrew [import]uid: 109711 topic_id: 32153 reply_id: 128037[/import]

@David,

After I press newGame the result is always very close to:

It seems that this could just be normal memory allocation. What you should try is this:

1)Enter main menu
2)Measure memory
3) Enter newgame
4) Measure memory
5) Exit game to main menu
6)Enter newgame again
7)Measure memory and compare to previous measurement.
8) Repeat steps 5-7 and see if the memory goes up consistently

After you repeat this about 4 or 5 times you can call it steady state. Any memory increase at this point is a leak. You may find that memory actually stays constant after the second time around and this is totally normal. If not then you should try looking into Andrew’s suggestions.

Regards,
M.Y. Developers
[import]uid: 55057 topic_id: 32153 reply_id: 128044[/import]

@David,

You may have already figured your leak out, but you should go here and grab the M.Y. Developers profiler:

http://www.mydevelopersgames.com/site/

This is a great tool.

-Ed

[import]uid: 110228 topic_id: 32153 reply_id: 128049[/import]

Hi David,

It would help if you posted some code for your newGame function, but in the meantime, two common reasons to have a memory leak are:

  • Creating a display object or group and not calling [lua]obj:removeSelf()[/lua] and [lua]obj == nil[/lua] when you are done using it

  • Storing a [lua]timer.performWithDelay[/lua] in a variable and not setting that variable to nil when the timer is complete

  • Andrew [import]uid: 109711 topic_id: 32153 reply_id: 128037[/import]

@David,

After I press newGame the result is always very close to:

It seems that this could just be normal memory allocation. What you should try is this:

1)Enter main menu
2)Measure memory
3) Enter newgame
4) Measure memory
5) Exit game to main menu
6)Enter newgame again
7)Measure memory and compare to previous measurement.
8) Repeat steps 5-7 and see if the memory goes up consistently

After you repeat this about 4 or 5 times you can call it steady state. Any memory increase at this point is a leak. You may find that memory actually stays constant after the second time around and this is totally normal. If not then you should try looking into Andrew’s suggestions.

Regards,
M.Y. Developers
[import]uid: 55057 topic_id: 32153 reply_id: 128044[/import]

@David,

You may have already figured your leak out, but you should go here and grab the M.Y. Developers profiler:

http://www.mydevelopersgames.com/site/

This is a great tool.

-Ed

[import]uid: 110228 topic_id: 32153 reply_id: 128049[/import]

Thanks for all the great feedback!

@ Andrew
Most likely one of your suggestions is causing the leak.
I don’t think that I will post code just because I would pretty much have to post my whole project in order to find the leak. I will try to find the leak and i have a suspicion it is a few display objects. Thanks a lot for the advice.

@ M.Y. Developers
I followed your steps and, if I am understanding your comment correctly, each time I start a new game the memory usage goes up by 40, I reduced it down to 15 now, so it is consistently leaking memory if that makes any sense. Thanks for your detailed response.

@ Ed
That tool looks very useful and I may have to try it out. Thanks for the help.

David [import]uid: 152065 topic_id: 32153 reply_id: 128367[/import]

Thanks for all the great feedback!

@ Andrew
Most likely one of your suggestions is causing the leak.
I don’t think that I will post code just because I would pretty much have to post my whole project in order to find the leak. I will try to find the leak and i have a suspicion it is a few display objects. Thanks a lot for the advice.

@ M.Y. Developers
I followed your steps and, if I am understanding your comment correctly, each time I start a new game the memory usage goes up by 40, I reduced it down to 15 now, so it is consistently leaking memory if that makes any sense. Thanks for your detailed response.

@ Ed
That tool looks very useful and I may have to try it out. Thanks for the help.

David [import]uid: 152065 topic_id: 32153 reply_id: 128367[/import]