Good/Bad Memory handling in Corona

Hej, 

I read a lot about this topic now, but am kinda confused by what is a memory leak and how to prevent it.

Whenever I test my app with a memory monitor like “RG Super Meter” or just a "collectgarbage() ,collectgarbage(“count”) "  I see that, when I change a scene with the “slim director class”, the memory is increasing but hitting a peak, when I just change between two scenes .

I thought that every scene has its memory consumption and would go back to the amount of used memory, when I changed to the same scene again.

For example.

Screen A needs 200kb of memory.

Screen B needs 340kb of memory. 

But when I go back to Screen A my memory just goes back to 300kb instead of 200kb.

Is this already a memory leak?

If yes, then I am not sure, what I am doing wrong.

I tried to make everything local, except of just a few globals, that I create in the main.lua.

I even collect all the transitions and timers and delete them manually like jon bebe suggested.

I use modules in the local way, like this : 

local M = {}

local function name ()

       local localGroup = display.newGroup()  

       […]

       return localGroup 

end

M.name = name

return M

what else is there to do?

By the way, what is a good memory usage? What is the memory and videomemory limit of devices like 

Iphone 3s, Iphone 4, Iphone 4s and Iphone5?

thanks for the help!

Cheers Philip

Let’s say you start out with Scene A and you expect it to use 200K then Scene B you expect to use 400K (I’m going to use round numbers), and when you go back to Scene A memory jumps to 300K.  Let’s not call it a memory leak yet.  Lets go back and forth a few times:

No Leak         Leak

A: 200            A:200

B: 400            B:400

A: 300            A:300

B: 500            B:500

A: 300            A:400

B: 500            B:600

A: 300            A:500

B: 500            B:700

See how in the Leak column, the  amounts continue to go up, yet in the first one, on the first 2nd time visitng each seen there wa a one time increase.  We don’t know what Director is doing under the hood.  It may be keeping a fragment of the scene in memory, or have some table that grows based on the number of scenes.  I would only worry if the numbers keep climbing over time.

As far as memory limits, I would say on the iPhone 3s/iPad 1, you probably don’t want to get much over 50MB of texture (video) memory.  In the grand scale of things the Lua memory is tiny compared to texture memory, but that said, a lot of lua memory could indicate inefficient code.  It’s hard to provide guidance there because we don’t know much about your data needs.

The iPhone 4, 4s, I would say in the 100-150mb range is pretty safe.  There are many factors involved.  The iPhone 5 and the Retina iPads have 1GB of RAM so they probably could afford to give you 200-300mb to play with.  Of course the texture memory on the @4x graphics is 4 x as much as the 2x graphics so being frugal is always good.

thanks so much, that makes everything so much clearer :slight_smile:

Let’s say you start out with Scene A and you expect it to use 200K then Scene B you expect to use 400K (I’m going to use round numbers), and when you go back to Scene A memory jumps to 300K.  Let’s not call it a memory leak yet.  Lets go back and forth a few times:

No Leak         Leak

A: 200            A:200

B: 400            B:400

A: 300            A:300

B: 500            B:500

A: 300            A:400

B: 500            B:600

A: 300            A:500

B: 500            B:700

See how in the Leak column, the  amounts continue to go up, yet in the first one, on the first 2nd time visitng each seen there wa a one time increase.  We don’t know what Director is doing under the hood.  It may be keeping a fragment of the scene in memory, or have some table that grows based on the number of scenes.  I would only worry if the numbers keep climbing over time.

As far as memory limits, I would say on the iPhone 3s/iPad 1, you probably don’t want to get much over 50MB of texture (video) memory.  In the grand scale of things the Lua memory is tiny compared to texture memory, but that said, a lot of lua memory could indicate inefficient code.  It’s hard to provide guidance there because we don’t know much about your data needs.

The iPhone 4, 4s, I would say in the 100-150mb range is pretty safe.  There are many factors involved.  The iPhone 5 and the Retina iPads have 1GB of RAM so they probably could afford to give you 200-300mb to play with.  Of course the texture memory on the @4x graphics is 4 x as much as the 2x graphics so being frugal is always good.

thanks so much, that makes everything so much clearer :slight_smile: