Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Memory leak in timer.performWithDelay
Started by stanga Apr 20 2017 06:44 AM

- - - - -
5 replies to this topic
timer performwithdelay memory
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

stanga

[GLOBAL: userInfoPane.html]
stanga
  • Contributor

  • 189 posts
  • Corona SDK

I noticed something strange with timer.performWithDelay and the app's memory usage. 

 

I monitored the memory usage with the following code:

local function checkMemory()
    collectgarbage("collect")
    local memUsageStr = string.format("MEMORY = %.3f KB", collectgarbage("count"))
    local textureStr = "TEXTURE = " .. (system.getInfo("textureMemoryUsed") / (1024 * 1024))
    print(memUsageStr, textureStr)
end
timer.performWithDelay(1000, checkMemory, 0)

And when running the following code:

local bounceUp

local rect = display.newRect(0, 0, 20, 20)

local transitionTimer = timer.performWithDelay(100, _bounceUp)

local function bounceOnComplete()
    transitionTimer = timer.performWithDelay(3000, bounceUp)
end

local function bounceDown()
    transition.to(rect, { time=300, y=0, transition=easing.inCirc, onComplete=bounceOnComplete })
end

bounceUp = function()
    --timer.cancel(transitionTimer) -- I tried to add this line, but it did not help
    transitionTimer = nil
    collectgarbage()
    transition.to(rect, { time=300, y=-50, transition=easing.outCirc, onComplete=bounceDown })
end
I see the memory count keeps rising up.
It increments significantly, then decrements a little bit, over and over, so overall the usage is growing continuously. 
 
It makes sense for the memory to rise, since I keep creating new timers, but it should also free the same amount when these timers end.  
 
Also, I tried the following approach to see if it helps:
local bounceUp

local rect = display.newRect(0, 0, 20, 20)

local transitionTimer = timer.performWithDelay(3000, _bounceUp, 0)

local function bounceDown()
    transition.to(rect, { time=300, y=0, transition=easing.inCirc })
end

bounceUp = function()
    transition.to(rect, { time=300, y=-50, transition=easing.outCirc, onComplete=bounceDown })
end

But to my surprise, the memory usage stayed the same. 

It keeps creeping up, no matter what. 

 

What is going on?

Why isn't the memory released after the timer ends?



[TOPIC: post.html]
#2

Sphere Game Studios

[GLOBAL: userInfoPane.html]
Sphere Game Studios
  • Contributor

  • 970 posts
  • Corona SDK

set transitionTimer = nil and call collectgarbage() at the end of your timer to deallocate memory.



[TOPIC: post.html]
#3

stanga

[GLOBAL: userInfoPane.html]
stanga
  • Contributor

  • 189 posts
  • Corona SDK

I tried it, but there were no changes to the result - every time a new timer was set, the memory consumption jumped, decreased a bit after, but over all the memory consumption keeps growing and growing. 

(I edited the code to reflect the change)



[TOPIC: post.html]
#4

stanga

[GLOBAL: userInfoPane.html]
stanga
  • Contributor

  • 189 posts
  • Corona SDK

Any update on this?

We use timers a lot, and that means we loose  lot of memory for no reason...

Can you please investigate?



[TOPIC: post.html]
#5

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 21,094 posts
  • Corona Staff

Can you put together a demo project that shows the issue? It must be a complete project: main.lua, config.lua, build.settings and any other assets/files needed so we can just load it and run it and see the problem. Put that in a .zip file (no other format please) and attach it to an email sent to support AT coronalabs.com along with a good description of the problem and the steps needed to reproduce it.

 

Thanks

Rob



[TOPIC: post.html]
#6

stanga

[GLOBAL: userInfoPane.html]
stanga
  • Contributor

  • 189 posts
  • Corona SDK

I found the memory leak, it was in an internal function that wraps all "transitions.to" calls. 

So the timer was wrongfully accused :)

 

Thanks for the help!




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: timer, performwithdelay, memory