Log In  

I hit an out of memory error and for once I don't know where the leak is...usually I know because the last change I made triggered it, not the case this time (or I just lost track of my shit) - either way, not sure exactly how to debug to trace it down.

Any tips or tricks are appreciated.

P#27506 2016-08-27 23:07 ( Edited 2016-08-28 03:07)

Hey morningtoast!

I'm sure you've probably tried this already, but in cases like these, I believe printh() and stat(0) are your best friend.

frame = 0
_update = function()
  -- log the frame to the console
  printh('frame '..frame)
  -- profile the current mem usage
  printh('mem '..flr((stat(0)/1024)*100)..'%)

  -- [your code here]...

  -- update frame
  frame = frame + 1

Just play your game and see if any action in particular causes memory usage to spike. If you're not finding any obvious leads, you might have to log out a trace() call on each loop as well.

Once you narrow down what your problematic code may be (special attention to any loops that create tables or functions on the fly), you can confirm by commenting it out and seeing if you still crash. Once you confirm, it's obviously just a matter of refactoring that bit.

Sometimes, it can also be easier to work backwards and comment out all of your code to start. Then, uncomment blocks until you find the memory hog.

Hope that helps!

P#27540 2016-08-28 10:23 ( Edited 2016-08-28 14:24)

Thanks for the tips. I'll see if I can make better understanding out of the stat() info. I guess I just don't know what it means outside of "higher is bad" type of thing.

I've been going through my usual process of elimination. I'm sure it's some recursive loop of some sort that I'm missing. Just slow and tedious all the same...grumble grumble...

P#27586 2016-08-28 19:11 ( Edited 2016-08-28 23:11)

also make sure all your variables are local when they should be. a big unused table can just stay there if a global references it, preventing the gc from reclaiming it.

I wish we had a collectgarbage(). calling it right before stat(0) would help a lot.

P#27635 2016-08-29 07:50 ( Edited 2016-08-29 11:50)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2023-01-30 11:24:01 | 0.005s | Q:13