System Memory Fluctuations

Hey guys, I am using a performance meter (tells memory usage and fps) from Corona archives to diagnose some memory problems in my project:

http://developer.coronalabs.com/code/easy-use-performance-meter-memory-texture-memory-fps

I used a blank project to test this performance meter, and I noticed something confusing. The project starts at 200 kbs of system memory, however, even if i do nothing, the system memory will rise up to around 350 kbs of system memory in a few seconds, then suddenly drop back down to 200. This was a vicious cycle that continued until I terminated the program. I suspect that this may be a memory leak on the part of the performance meter? However, I could not find the source of the leak and I’m also confused what prompted the memory to drop back to normal.

Tldr Here are my questions:

1.What is causing the memory leak in the following code?

  1. Why is the system memory rising and falling cyclically (around every 1/2 minute) even when I’m not doing anything?

Here is the code to the simple performance meter:

[lua]

–THIS IS WORK AND CODE CREATED BY LERG FOR CORONA SDK/ LUA USE. I DID NOT ADD ANYTHING TO THIS BUT TO UPDATE THE MODULE FOR GFX2. ALL CREDIT GOES TO LERG FOR CREATING THE ORIGINAL CODE.

local _M = {}
 
local mFloor = math.floor
local sGetInfo = system.getInfo
local sGetTimer = system.getTimer
 
local prevTime = 0
_M.added = true
local function createText()
    local memory = display.newText(‘00 00.00 000’,10,0, ‘Helvetica’, 18);
    --memory:setFillColor(255,53,247)
    memory:setFillColor(1,1,1,1)
    memory.anchorY = 1
    memory.x, memory.y = display.contentCenterX, display.contentHeight - 5
    function memory:tap ()
        collectgarbage(‘collect’)
        if _M.added then
            Runtime:removeEventListener(‘enterFrame’, _M.labelUpdater)
            _M.added = false
            memory.alpha = .5
        else
            Runtime:addEventListener(‘enterFrame’, _M.labelUpdater)
            _M.added = true
            memory.alpha = 1
        end
    end
    memory:addEventListener(‘tap’, memory)
    return memory
end
 
function _M.labelUpdater(event)
    local curTime = sGetTimer()
    _M.text.text = tostring(mFloor( 1000 / (curTime - prevTime))) … ’ ’ …
            tostring(mFloor(sGetInfo(‘textureMemoryUsed’) * 0.0001) * 0.01) … ’ ’ …
            tostring(mFloor(collectgarbage(‘count’)))
    _M.text:toFront()
    prevTime = curTime
end
 
function _M:newPerformanceMeter()
    self.text = createText(self)
    Runtime:addEventListener(‘enterFrame’, _M.labelUpdater)
end
 
return _M
 

[/lua]

Speaking as the person that uploaded it (though not the creator, as the comment notes) there isn’t anything wrong with the performance meter code. Could you post the sample project name (and code, if it’s not delivered by Corona). You would also be best served by looking over the basics of optimization and the memory management tutorial.

That is by definition not a memory leak.  A memory lead would be if it went up to 350, then went up to 700, then went up and went up and went up.  The fact that it returns to 200 all the time means there is no leak and that memory is working as it should.

So if it is not a memory leak, what is happening? The memory normalizes after 30 seconds, so does this mean that the garbage collector running every 30s. I was under the impression (I forget where I read this) that the garbage collector runs one cycle every 500 ms.

It could be something in the module you are using that’s creating objects, changing the size of text fields, etc. and freeing things up.  I don’t believe it’s on a set schedule.  The way I read the Lua manual, it’s based on a couple of criteria:  how much memory is being allocated and how fast it’s being allocated.  It would make sense if your app is doing nothing but running the FPS module, it’s allocating memory at a fixed pace so for that memory usage foot print, it might be 30 seconds.

Rob

Okay, I understand what you are saying, I just have a few final questions to clarify things

  1. Does the garbage collection system run at random (not fixed) intervals?

  2. In what situations is it appropriate to call collectgarbage(‘collect’), instead of letting lua auto collect the garbage?

Thanks for the help!

1.  It’s not random, but it’s not fixed intervals either.  It’s based on how fast and how much memory is being churned.  If it helps you can consider it at random because I don’t think you can predict when it’s going to run without a lot of work and understanding of the internals of Lua.

  1. If you get a low memory warning, you might want to call GC.  If you dump a big scene then you might want to call it.  If you find times where the memory is getting too high from normal usage, then you might want to call it to try and reclaim things.  But you’re better off putting your energy in making sure you don’t get into those situations in the first place.

Rob

Speaking as the person that uploaded it (though not the creator, as the comment notes) there isn’t anything wrong with the performance meter code. Could you post the sample project name (and code, if it’s not delivered by Corona). You would also be best served by looking over the basics of optimization and the memory management tutorial.

That is by definition not a memory leak.  A memory lead would be if it went up to 350, then went up to 700, then went up and went up and went up.  The fact that it returns to 200 all the time means there is no leak and that memory is working as it should.

So if it is not a memory leak, what is happening? The memory normalizes after 30 seconds, so does this mean that the garbage collector running every 30s. I was under the impression (I forget where I read this) that the garbage collector runs one cycle every 500 ms.

It could be something in the module you are using that’s creating objects, changing the size of text fields, etc. and freeing things up.  I don’t believe it’s on a set schedule.  The way I read the Lua manual, it’s based on a couple of criteria:  how much memory is being allocated and how fast it’s being allocated.  It would make sense if your app is doing nothing but running the FPS module, it’s allocating memory at a fixed pace so for that memory usage foot print, it might be 30 seconds.

Rob

Okay, I understand what you are saying, I just have a few final questions to clarify things

  1. Does the garbage collection system run at random (not fixed) intervals?

  2. In what situations is it appropriate to call collectgarbage(‘collect’), instead of letting lua auto collect the garbage?

Thanks for the help!

1.  It’s not random, but it’s not fixed intervals either.  It’s based on how fast and how much memory is being churned.  If it helps you can consider it at random because I don’t think you can predict when it’s going to run without a lot of work and understanding of the internals of Lua.

  1. If you get a low memory warning, you might want to call GC.  If you dump a big scene then you might want to call it.  If you find times where the memory is getting too high from normal usage, then you might want to call it to try and reclaim things.  But you’re better off putting your energy in making sure you don’t get into those situations in the first place.

Rob