Need help with memory leak issue...

Hello all,

so i have been making this simple car game where the player controls a car and (for now) has to run over zombies that are randomly spawned…

however, I noticed that the memory increases after a new zombie replaces one that has been killed (when a zombie is hit by the car, i remove it and replace it to keep a constant flow of zombies)

also, i am using a function to create skid marks behind the car’s tires…

But even after looking all around the web and trying different things out i cant get the memory loss to stop happening…

I know (after having commented out several parts of code and testing) that the memory issue comes from the deletion of the skid marks and the deletion of zombies…

here is my simplified code:

(keep in mind this is a pre pre pre pre alpha or what so it is not the most beautiful piece of code)

display.setStatusBar( display.HiddenStatusBar ) local physics = require( "physics" ) physics.start() physics.setGravity(0,0) local d = require( "data" ) d.w = display.viewableContentWidth d.h = display.viewableContentHeight local perspective = require( 'perspective' ) local camera = perspective.createView() local zc = 0 local totalZoms = 0 local sheetDataMove = { width=64, height=64, numFrames=4, sheetContentWidth=256, sheetContentHeight=64 } local dataMove = { {name = "zombies", start=1, count=2, time=500, loopCount = 0 } } local mySheetMove = graphics.newImageSheet( "zombie.png", sheetDataMove ) local ZOMBIE\_COUNT = 40 car = display.newImage("jeep.png", d.w\*0.5, d.h) physics.addBody( car,"dynamic", physicsData:get( "carPhys" ) ) camera:add(background, 5) camera:add(car) camera:setFocus(car) camera:track() function spawnZombies() if zc \< ZOMBIE\_COUNT then totalZoms = totalZoms + 1 zombies[totalZoms] = display.newSprite( mySheetMove, dataMove ) zombies[totalZoms].x, zombies[totalZoms].y = math.random(car.x-d.w, car.x+d.w), math.random(car.y-d.h, car.y+d.h) physics.addBody( zombies[totalZoms], "dynamic", physicsData:get( "zombie" ) ) table.insert( zom, zombies[totalZoms] ) zombies[totalZoms]:play() camera:add( zombies[totalZoms] ) transition.to( zombies[totalZoms], { time = 500, alpha = 1 } ) zc = zc + 1 end return zombies[totalZoms] end local function hit(event) if event.phase == "began" then camera:remove( event.other ) table.remove( zom, event.other.num ) display.remove( event.other ) event.other = nil zc = zc - 1 end return true end function skidMark(motor) mark = display.newImage( "skid.png", motor.x, motor.y ) transition.to(mark, { delay = 1000, time = 1000, alpha = 0 } ) table.insert( skidTable, mark ) camera:add( mark, 5) return mark end function particle() skids = skids + 1 skid[skids] = skidMark( motor3 ) skid[skids\*-1] = skidMark( motor2 ) skids = skids + 1 timer.performWithDelay( 10, particle ) return skid[skids] or skid[skids\*-1] or true end function deleteParticle() for k,v in pairs( skidTable ) do if v.alpha == 0 then table.remove( skidTable, k ) camera:remove( v ) display.remove( v ) v = nil k = nil end end timer.performWithDelay( 1000, deleteParticle ) end local function fasterFunction() for k,v in pairs( zom ) do camera:remove( v ) table.remove( zom, k ) display.remove(v) v = nil k = nil zc = zc - 1 end end end timer.performWithDelay( 50, fasterFunction ) return true end local function checkMemory() collectgarbage( "collect" ) local memUsage\_str = string.format( "MEMORY = %.3f KB", collectgarbage( "count" ) ) print( memUsage\_str, "TEXTURE = "..(system.getInfo("textureMemoryUsed") / (1024 \* 1024) ) ) timer.performWithDelay( 1000, checkMemory ) end spawnZombies() deleteParticle() particle() fasterFunction() checkMemory() return scene

hi,

on a quick glance…

i notice the spelling in your ipairs loops are “pears” instead of “pairs” (lines 114,127)

also notice you are calling “remove()” which afaik isnt a command (example line 134) but you usually follow up with variable=nil so not sure about this one.

you may also want to use display.remove(object) instead of object:removeSelf()

oh yeah hahah sorry i forgot, i localized all the functions i use often, but i’ll modify it

hi,

on a quick glance…

i notice the spelling in your ipairs loops are “pears” instead of “pairs” (lines 114,127)

also notice you are calling “remove()” which afaik isnt a command (example line 134) but you usually follow up with variable=nil so not sure about this one.

you may also want to use display.remove(object) instead of object:removeSelf()

oh yeah hahah sorry i forgot, i localized all the functions i use often, but i’ll modify it