Should memory go back to exactly where it was when going back to a previous scene?

Okay, so I’ve read all about memory and how to go about removing objects etc etc, I think I have a pretty good understanding of it now and have been going through my lua files with a fine comb toothbrush!

Here’s the thing, if I start my app the console outputs memory as approximately 246kb, once I tap a button to go to the next scene it increases to 390kb but when I tap to go back to the first scene it stays at 390kb. I can go backwards and forwards like this and it always stays around the 390kb mark, it never seems to go up or down (it can vary by say 10kb but always stays within a similar region)

My texture memory however does fluctuate as I would expect, it goes up when I move to scene two as I would expect and back down to where it was if I go back to scene one.

Would I be right in saying that the memory should go back to where it was (246kb) if I am at scene 2 and press to go back to scene 1? [import]uid: 107915 topic_id: 35348 reply_id: 335348[/import]

Technically, yes. But sdks and libraries also allocate things as components are used as well.

If you’re talking kbytes, no worries. And especially if if stays static at the higher number even if you go back and forth between modules (instead of always going up by 150k each time, for example).

If it stays steady, it was probably something permanent that was allocated when the module was called (file io buffers, a pool of handles, etc). If it always grows, it’s more likely a leak.

I never really checked if all corona lua code allocation is dynamic, but it probably is… If so, It could possibly just be static variables instantiated in your second module (globals, intialized once when it is loaded - or it could possibly be the sdk simply allocating space for your function names in namespace). 150k is a pretty small amount

That’s my two cents anyways. [import]uid: 79933 topic_id: 35348 reply_id: 140501[/import]

Thanks for that, seems to make sense.

I have noticed another odd thing though, if I go to say scene 3, the memory goes up again (as to be expected) but if I go back from scene 3 to scene 1 the memory yet again stays the same? In other words it seems like my memory allocations are just accumulating.

For arguments sake if all my scenes were 100kb this is in a nutshell what happens:
Scene 1 - 100kb to Scene 2 - 200kb to Scene 3 - 300kb and back to Scene 1 - 300kb

Now surely that can’t be right?

Here’s what I don’t understand. You could say well you obviously have some memory leaks, that would kinda make sense BUT when I go to a new scene I can clearly see how far the memory has gone up so therefore I can see how much memory that scene is using. When I then go back to scene 1 the memory stays exactly the same, I could understand if I accidentally missed removing some transitions or timers but for the memory to stay the same means I’m not removing anything…not even I’m that stupid!!

I know my ‘remove’ function is being called as well because I can print it to the console…it’s very odd!

[import]uid: 107915 topic_id: 35348 reply_id: 140503[/import]

When ever you go to a new scene, some memory is allocated for loading the module itself. Changing back to a scene doesn’t remove that module’s overhead. Going back to scene 2 would not increase memory because that module is loaded. Purging a storyboard scene also does not remove all of the local variables allocated outside of the storyboard scene functions (removing a scene will).
[import]uid: 199310 topic_id: 35348 reply_id: 140505[/import]

I’ve added storyboard.removeAll() in the enterScene function and yet still the memory stays exactly where it is, it never decreases.

My concern is that currently I only have 3 levels, I intend to have 60 so at some point the memory will have to decrease otherwise I’ll probably be stuffed by the time I get to level 10! [import]uid: 107915 topic_id: 35348 reply_id: 140512[/import]

60 scenes at 100K is slightly more memory than one 1024x1024 texture image. It’s probably not something to concern yourself with.

But you also need to give garbage collection time to run too. Calling storyboard.removeAll() won’t free the memory right away.

[import]uid: 199310 topic_id: 35348 reply_id: 140513[/import]

I think my main concern and how this all came about was that after 15-20 minutes of play on my iPhone 4S I noticed a considerable frame rate drop so concluded that I must have memory leaks somewhere and that has led me to where I am now.

I’m currently using the following in my main.lua file:

local function memPrinting() garbage = collectgarbage("count") print ("GARBAGE = ",garbage) collectgarbage("collect") local memUsage\_str = string.format( "MEMORY = %.3f KB", collectgarbage( "count" ) ) print( memUsage\_str, "TEXTURE = "..(system.getInfo("textureMemoryUsed")/1000000) ) print( memUsage\_str ) end timer.performWithDelay( 5000, memPrinting, 0 )

So obviously from that I can see garbage count along with memory and texture. The garbage count goes up and down as I expect so that’s good news. My texture memory also goes up and down as I expect it to, it’s the memory part that I’m finding disturbing, I don’t understand why it never goes down.

I’m not totally convinced it’s why I’m having frame rate problems but it’s the only thing I can pinpoint currently. [import]uid: 107915 topic_id: 35348 reply_id: 140514[/import]

You said:

Scene 1 - 100kb to Scene 2 - 200kb to Scene 3 - 300kb and back to Scene 1 - 300kb

Does it do this:

Scene 1 - 100kb to Scene 2 - 200kb to Scene 3 - 300kb and back to Scene 1 - 300kb
To Scene 2 - 300kb to Scene 3 - 300kb and back to Scene 1 - 300kb

If that is what happens, then the allocation increasing is likely just static allocation that happens once when the modules load either due to the modules globals (including function names), or other static allocations the modules cause when first loading (libraries referenced by require might allocate things the first time they are loaded).

If instead, this happens:
Scene 1 - 100kb to Scene 2 - 200kb to Scene 3 - 300kb and back to Scene 1 - 300kb
To Scene 2 - 400kb to Scene 3 - 500kb and back to Scene 1 - 500kb

Then you’re probably leaking, as it is not likely caused by allocating static variables the first time modules are loaded.

I would guess with these tiny numbers, there is no direct connection to your frame rate issue. [import]uid: 79933 topic_id: 35348 reply_id: 140516[/import]

It is working the way you described in your first example, it’s not increasing any further, it stays pretty much static after I have visited all the scenes and I can go backwards and forwards and it won’t increase after that.

I think for the time being I’m just going to have to accept that it’s happening, if it’s not effecting my game then I guess it’s no problem. I’ll have to see how it pans out as I start creating more and more levels. [import]uid: 107915 topic_id: 35348 reply_id: 140527[/import]

Technically, yes. But sdks and libraries also allocate things as components are used as well.

If you’re talking kbytes, no worries. And especially if if stays static at the higher number even if you go back and forth between modules (instead of always going up by 150k each time, for example).

If it stays steady, it was probably something permanent that was allocated when the module was called (file io buffers, a pool of handles, etc). If it always grows, it’s more likely a leak.

I never really checked if all corona lua code allocation is dynamic, but it probably is… If so, It could possibly just be static variables instantiated in your second module (globals, intialized once when it is loaded - or it could possibly be the sdk simply allocating space for your function names in namespace). 150k is a pretty small amount

That’s my two cents anyways. [import]uid: 79933 topic_id: 35348 reply_id: 140501[/import]

Thanks for that, seems to make sense.

I have noticed another odd thing though, if I go to say scene 3, the memory goes up again (as to be expected) but if I go back from scene 3 to scene 1 the memory yet again stays the same? In other words it seems like my memory allocations are just accumulating.

For arguments sake if all my scenes were 100kb this is in a nutshell what happens:
Scene 1 - 100kb to Scene 2 - 200kb to Scene 3 - 300kb and back to Scene 1 - 300kb

Now surely that can’t be right?

Here’s what I don’t understand. You could say well you obviously have some memory leaks, that would kinda make sense BUT when I go to a new scene I can clearly see how far the memory has gone up so therefore I can see how much memory that scene is using. When I then go back to scene 1 the memory stays exactly the same, I could understand if I accidentally missed removing some transitions or timers but for the memory to stay the same means I’m not removing anything…not even I’m that stupid!!

I know my ‘remove’ function is being called as well because I can print it to the console…it’s very odd!

[import]uid: 107915 topic_id: 35348 reply_id: 140503[/import]

When ever you go to a new scene, some memory is allocated for loading the module itself. Changing back to a scene doesn’t remove that module’s overhead. Going back to scene 2 would not increase memory because that module is loaded. Purging a storyboard scene also does not remove all of the local variables allocated outside of the storyboard scene functions (removing a scene will).
[import]uid: 199310 topic_id: 35348 reply_id: 140505[/import]

I’ve added storyboard.removeAll() in the enterScene function and yet still the memory stays exactly where it is, it never decreases.

My concern is that currently I only have 3 levels, I intend to have 60 so at some point the memory will have to decrease otherwise I’ll probably be stuffed by the time I get to level 10! [import]uid: 107915 topic_id: 35348 reply_id: 140512[/import]

60 scenes at 100K is slightly more memory than one 1024x1024 texture image. It’s probably not something to concern yourself with.

But you also need to give garbage collection time to run too. Calling storyboard.removeAll() won’t free the memory right away.

[import]uid: 199310 topic_id: 35348 reply_id: 140513[/import]

I think my main concern and how this all came about was that after 15-20 minutes of play on my iPhone 4S I noticed a considerable frame rate drop so concluded that I must have memory leaks somewhere and that has led me to where I am now.

I’m currently using the following in my main.lua file:

local function memPrinting() garbage = collectgarbage("count") print ("GARBAGE = ",garbage) collectgarbage("collect") local memUsage\_str = string.format( "MEMORY = %.3f KB", collectgarbage( "count" ) ) print( memUsage\_str, "TEXTURE = "..(system.getInfo("textureMemoryUsed")/1000000) ) print( memUsage\_str ) end timer.performWithDelay( 5000, memPrinting, 0 )

So obviously from that I can see garbage count along with memory and texture. The garbage count goes up and down as I expect so that’s good news. My texture memory also goes up and down as I expect it to, it’s the memory part that I’m finding disturbing, I don’t understand why it never goes down.

I’m not totally convinced it’s why I’m having frame rate problems but it’s the only thing I can pinpoint currently. [import]uid: 107915 topic_id: 35348 reply_id: 140514[/import]

You said:

Scene 1 - 100kb to Scene 2 - 200kb to Scene 3 - 300kb and back to Scene 1 - 300kb

Does it do this:

Scene 1 - 100kb to Scene 2 - 200kb to Scene 3 - 300kb and back to Scene 1 - 300kb
To Scene 2 - 300kb to Scene 3 - 300kb and back to Scene 1 - 300kb

If that is what happens, then the allocation increasing is likely just static allocation that happens once when the modules load either due to the modules globals (including function names), or other static allocations the modules cause when first loading (libraries referenced by require might allocate things the first time they are loaded).

If instead, this happens:
Scene 1 - 100kb to Scene 2 - 200kb to Scene 3 - 300kb and back to Scene 1 - 300kb
To Scene 2 - 400kb to Scene 3 - 500kb and back to Scene 1 - 500kb

Then you’re probably leaking, as it is not likely caused by allocating static variables the first time modules are loaded.

I would guess with these tiny numbers, there is no direct connection to your frame rate issue. [import]uid: 79933 topic_id: 35348 reply_id: 140516[/import]

It is working the way you described in your first example, it’s not increasing any further, it stays pretty much static after I have visited all the scenes and I can go backwards and forwards and it won’t increase after that.

I think for the time being I’m just going to have to accept that it’s happening, if it’s not effecting my game then I guess it’s no problem. I’ll have to see how it pans out as I start creating more and more levels. [import]uid: 107915 topic_id: 35348 reply_id: 140527[/import]