Memory Issue / Game lag

I am using a code that generates slopes just before a man runs onto them. However, after a while the game starts to get really laggy. I used a function to see how much memory is being used and it keeps rising. Is there anyway to stop the memory rising but have the same effect of slopes being generated. The code I am using is:

[code]

slopeshape = { -240,-100, 240,100, -240,100 }

local slope = {}

local function MakeBottom()
mx = man.x
for s = 1,9999 do
if man.x > (s*468) - 468 then
slope[s] = display.newImage(“slope.png”)
physics.addBody(slope[s], “static”, {friction=0.01, density = 1, bounce = 0.1, shape=slopeshape})
slope[s]:setReferencePoint(display.TopLeftReferencePoint)
slope[s].x = (s*468) - 480
slope[s].y = (s*195) - 100
slope[s].material = “ground”
moveGroup:insert(slope[s])
if s > 1 then
slope[s-1]:removeSelf()
end
end
end
end
Runtime:addEventListener (“enterFrame”, MakeBottom)

[/code] [import]uid: 116264 topic_id: 24382 reply_id: 324382[/import]

Well the first problem is that you are never nilling out slopes[s-1] after you remove it. Lua won’t garbage collect it until you do :

slopes[s-1] = nil
You also may not be giving the app time to garbage collect either because that’s a pretty tight loop.
[import]uid: 19626 topic_id: 24382 reply_id: 98557[/import]

thanks, I’ve added the “slopes[s-1] = nil” and changed then runtime event listener to a timer calling it every 100 milliseconds. It has slowed down the memory increase, but it is still rising. Any other suggestions? [import]uid: 116264 topic_id: 24382 reply_id: 98571[/import]

I’ve changed the code again and it is better, but the memory is still increasing:

local ramp = {}  
  
local function MakeBottom()  
 local mx = man.x  
 local bx = math.floor(mx/1879)  
 for r = 1,9999 do  
 if man.x \> (r\*1879) - 3000 then  
 ramp[r] = display.newImage("ramp.png")  
 ramp[r]:setReferencePoint(display.TopLeftReferencePoint)  
 physics.addBody(ramp[r], "static", {friction=0.01, density = 1, bounce = 0.1, shape=rampshape})  
 ramp[r].x = ((r\*1879) - 1879)  
 ramp[r].y = ((r\*684) - 684) + 200  
 ramp[r].material = "ground"  
 moveGroup:insert(ramp[r])  
 end  
 end  
 ramp[bx+1]:removeSelf()  
 ramp[bx+1] = nil  
end  
MakeBottom()  
timer.performWithDelay(5000, MakeBottom, 0)  
  

[import]uid: 116264 topic_id: 24382 reply_id: 98639[/import]

tough one…like Rob said, tight loop…
try

timer.performWithDelay(5000, function() MakeBottom() collectgarbage() end, 0)  

and see if this helps… [import]uid: 21331 topic_id: 24382 reply_id: 98643[/import]

With the new code I’m not seeing where you’re removing everything you’re allocating and I’m still not sure why you’re iterating 10,000 times.

Garbage collection tends to happen at slow times in your game. Also if you have an object, you do a RemoveSelf() on it, set it to nil the immediately re-use it before GC has a chance to run, I’m not 100% certain, but I think that will cause GC to not realize that variable needs collected (not sure on that but it makes sense)

@TheRealTonyK, don’t you need a semi-colon between MakeBottom() and the GC call. [import]uid: 19626 topic_id: 24382 reply_id: 98650[/import]

nope. i never use semi’s…

And I don’t really get the code either…LOL…just trying to help without breaking my melon in half… :slight_smile: [import]uid: 21331 topic_id: 24382 reply_id: 98652[/import]