memory leak out of nowhere

Hello everybody,

 

This is my very first question in the forums.

 

I’ve done my best to avoid leaks for an on going project I have.

I followed every suggestion on this post:

http://developer.coronalabs.com/forum/2012/05/21/guide-findingsolving-memory-leaks 

 

But I still had a constant memory leak that I couldn’t resolve.

 

I started ripping down everything from my project one by one, then had the idea to try out: a simple four line main.lua file and nothing else. You can try it yourself, just build a simple main.lua file consisting of these four lines in a new folder.

function printMemory(event) print(collectgarbage('count')) end Runtime:addEventListener('enterFrame', printMemory)

And even still with this empty file, I am seeing minor increase in the memory count, it’s around 4 kilobytes a second, but the raise is constant (until the gc of course).

Is this normal? If it is, Danny from the Corona Staff says in the post from the link above, that he only has 1kb of leak for every 3-4 scene change. How could this be possible when I’m leaking 3-4kb’s a second.

A part of the output to the four line code is below, am I reading it wrong? Am I missing something?

Dec 11 22:28:08.772: 303.1953125 Dec 11 22:28:08.801: 303.41015625 Dec 11 22:28:08.835: 303.6259765625 Dec 11 22:28:08.868: 303.84375 Dec 11 22:28:08.901: 304.056640625 Dec 11 22:28:08.934: 304.2734375 Dec 11 22:28:08.967: 304.48828125 Dec 11 22:28:08.999: 304.7041015625 Dec 11 22:28:09.033: 304.921875 Dec 11 22:28:09.066: 305.1357421875 Dec 11 22:28:09.099: 305.353515625 Dec 11 22:28:09.132: 305.5703125 Dec 11 22:28:09.165: 305.78515625 Dec 11 22:28:09.197: 306.0009765625 Dec 11 22:28:09.231: 306.21875 Dec 11 22:28:09.264: 306.431640625 Dec 11 22:28:09.296: 306.6484375 Dec 11 22:28:09.329: 306.86328125 Dec 11 22:28:09.362: 307.0791015625

Thanks in advance, to anyone who takes the time to even read this.

Cheers.

Hi @cihanbilir,

There’s not a memory leak in your test code. First of all, when testing for memory usage, you should only practically monitor memory perhaps on 1 second increments, not during Runtime. Memory will fluctuate a little, up and down, and if you continue running your 4 lines of code, you’ll notice that the memory eventually “stabilizes”.

So, it’s natural to have memory levels swing around a little, going up and down slightly, even if nothing else is going on. Where you would need to be alarmed is if you’re seeing constant upward memory creep, and it never goes back down. That would indicate a clear memory leak.

Hope this helps,

Brent

@cihanbilir,

I agree with Brent 100%, but want to add a little to what he said.  If you modify your code to force a collection before reading memory, you’ll have a better idea of whether you’re leaking or not.  The code below should show solid memory usage from reading to reading as long as you don’t make new Lua or Corona objects (including strings, functions, etc.).

Any kind of code executing at all, may result in slight rises followed by falls (over a period of seconds) in memory usage.  You’re fine as long as this isn’t an unbounded climb (ever increasing usage with no fall). See video below.

Note: I modified this code to run once per second.

function printMemory(event) collectgarbage('collect') print(collectgarbage('count')) end timer.performWithDelay(1000,printMemory,-1)

https://www.youtube.com/watch?v=XIpZ3mQsbz4

https://www.youtube.com/watch?v=cueWYzq0qCY

Thank you Brent,

Thank you roaminggamer.

This was very helpful.

Your memory usage will go up and down cause that’s what gc does. If you want to force it this is what I added to my line of code :
1. collectgarbage(“setpause”,100) in initialization

  1. collectgarbage(“step”,4) on mainloop. I found 4 is the perfect balance and my test shows minimal performance loss

 

Hi @cihanbilir,

There’s not a memory leak in your test code. First of all, when testing for memory usage, you should only practically monitor memory perhaps on 1 second increments, not during Runtime. Memory will fluctuate a little, up and down, and if you continue running your 4 lines of code, you’ll notice that the memory eventually “stabilizes”.

So, it’s natural to have memory levels swing around a little, going up and down slightly, even if nothing else is going on. Where you would need to be alarmed is if you’re seeing constant upward memory creep, and it never goes back down. That would indicate a clear memory leak.

Hope this helps,

Brent

@cihanbilir,

I agree with Brent 100%, but want to add a little to what he said.  If you modify your code to force a collection before reading memory, you’ll have a better idea of whether you’re leaking or not.  The code below should show solid memory usage from reading to reading as long as you don’t make new Lua or Corona objects (including strings, functions, etc.).

Any kind of code executing at all, may result in slight rises followed by falls (over a period of seconds) in memory usage.  You’re fine as long as this isn’t an unbounded climb (ever increasing usage with no fall). See video below.

Note: I modified this code to run once per second.

function printMemory(event) collectgarbage('collect') print(collectgarbage('count')) end timer.performWithDelay(1000,printMemory,-1)

https://www.youtube.com/watch?v=XIpZ3mQsbz4

https://www.youtube.com/watch?v=cueWYzq0qCY

Thank you Brent,

Thank you roaminggamer.

This was very helpful.

Your memory usage will go up and down cause that’s what gc does. If you want to force it this is what I added to my line of code :
1. collectgarbage(“setpause”,100) in initialization

  1. collectgarbage(“step”,4) on mainloop. I found 4 is the perfect balance and my test shows minimal performance loss