Managing Memory : does it need to go back to the initial state ?

Hi there !

I’ve been working on my game for a little time now and I’ve come to the point where I really need to take care of how I manage the memory in order to prevent memory leaks.

In order to clearly understand what I’d have to do, I’ve made few tests with the famous monitorMem() function in a new main.lua file :

[lua]local rectangle;

local function createRectangle()
     rectangle = display.newRect(0,0,100,100);
     rectangle:setFillColor(255,255,255,255)
end

local function removeRectangle()
     rectangle:removeSelf();

     rectangle = nil;
end

local timer2 = timer.performWithDelay(1000, createRectangle)
local timer = timer.performWithDelay(2000, removeRectangle)

local monitorMem = function()
     collectgarbage()
     print("MemUsage: " … collectgarbage(“count”))

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

end

Runtime:addEventListener(“enterFrame”, monitorMem) [/lua]

From what I’ve seen, when I launch the app, the memusage starts at 106.75, when the rectangle is created, it goes up to 106.39 and then, once the rectangle is being deleted, it goes down to 105.875…

So… here are my questions :

  • Am I doing my test right ? Because I don’t understand the logic : when there is nothing loaded, the mem usage seems to be highter than once display objects are displayed.

  • When I remove display objects and clean my variables, is the memory supposed to go back exactly at its initial test ?

  • Bonus question : when I’m using the storyboard API, once a scene is purged, is the memory usage supposed to go back at its initial state ? Because right now, in my game, even if I remove all the displayed object, remove all the event listener, nil all the variables, it does reduce the memory usage, but there’s still a little bit more memory than before.

Thank you in advance, and sorry if I’m not clear enough !

Hi there,

In general I wouldn’t worry if your memory usage fluctuates up or down slightly versus its previous value when you add and later clean up objects, change scenes, etc.  There are things going on in Corona and Lua internally that may cause the memory usage to not fall back to the exactly the previous value.  What you need to watch out for are situations when your memory usage is consistently and repeatedly climbing, i.e., as the game is being played, or as you switch back and forth between scenes.

  • Andrew

Hello there ! Thank you for your answer !

I actually have a few memory leaks and what you said helped me spotting them (since the memory usage fluctuates a lot, I needed to see if it was actually increasing over time while navigating through my scenes !).

So, thanks again !

Hi there,

In general I wouldn’t worry if your memory usage fluctuates up or down slightly versus its previous value when you add and later clean up objects, change scenes, etc.  There are things going on in Corona and Lua internally that may cause the memory usage to not fall back to the exactly the previous value.  What you need to watch out for are situations when your memory usage is consistently and repeatedly climbing, i.e., as the game is being played, or as you switch back and forth between scenes.

  • Andrew

Hello there ! Thank you for your answer !

I actually have a few memory leaks and what you said helped me spotting them (since the memory usage fluctuates a lot, I needed to see if it was actually increasing over time while navigating through my scenes !).

So, thanks again !