HUGE Memory Leakage Problem In Code

Hello All,

So i am a newbie coder and i just make games for fun.

However, on this latest idea of mine, I’m having a lot of trouble… 

I am making a primitive evolution simulator (to be improved, just trying to get it to work first) where when it starts it spawns random creatures that are trying to go as far as possible, and the best are “breeded” together to make the next batch, and so on…

However, after the end of the first batch’s run, even after i tried to remove every single variable i could think of between spawns, the memory usage still goes up by about 80kb per spawn!! I don’t get how such a big leakage can come from something i have been trying so hard to delete entirely…

I have already tried reading guides on how to properly delete objects etc…

Please help, it would be really useful :slight_smile:

Heres my code:

display.setStatusBar( display.HiddenStatusBar ) -------------------------------------------------------------------- --REQUIRE----------------------------------------------------------- local d = require( "data" ) local physics = require( "physics" ) local composer = require( "composer" ) local scene = composer.newScene( "main" ) physics.start() physics.setDrawMode( "hybrid" ) -------------------------------------------------------------------- --BASE VARIABLES---------------------------------------------------- d.h = display.contentHeight d.w = display.contentWidth local i = 0 local highestFitness = -1 local coefficient = 1 local lowestFitness = 1 local generation = 1 local batchSize = 60 local gpsPause = 0 local done = 0 local sky = display.newImage( "sky.png", d.w/2, d.h/2 ) local trees = display.newImage( "trees.png", d.w/2, d.h/2 ) local grass = display.newImage( "grass.png", d.w/2, d.h/1.2 ) local grassBottom = display.newImage( "grassBottom.png", d.w/2, d.h/1.05 ) local start = display.newImage( "start.png", d.w/2, d.h/1.35 ) local grassFront = display.newImage( "grass.png", d.w/2, d.h/1.2 ) local grassPhysics = display.newRect( d.w/2, d.h/0.88, d.h\*100, d.w ) local grassCollisionFilter = { categoryBits=2, maskBits=1 } local creatureCollisionFilter = { categoryBits=1, maskBits=2 } local genderCollisionFilter = { categoryBits=1, maskBits=2 } local scrollRect = display.newRect( d.w/2, d.h/1.08, 138, 90 ) local statsContrast = display.newImage( "statsContrast.png", d.w/2, d.h/2.15 ) local breedTimer = display.newImage( "timer.png", 1040, d.h ) local TdampRateA = {} local TangDamping = {} local Tfreq = {} local TgrvityA = {} local TdnsityA = {} local TfrixonA = {} local TbouncyA = {} local TjointLeng = {} local TmotSpe = {} local Tcolr = {} local TangDampingA = {} local Tgrvity = {} local Tdnsity = {} local Tfrixon = {} local Tbouncy = {} local TshapeArm = {} local TshapeMain = {} local TcolrA = {} local creature = {} local gender = {} local arm = {} local motor = {} local distance = {} local allCreatures = {} local grassLong = {} local scoresMen = {} local scoresWomen = {} local allMen = {} local allWomen = {} local pos = {} local moveGroup = display.newGroup() local sheetDataLoad = { width=102, height=102, numFrames=8, sheetContentWidth=816, sheetContentHeight=102 } local dataLoad = { {name = "load", start=1, count=8, time=500, loopCount = 0 } } local mySheetLoad = graphics.newImageSheet( "loading.png", sheetDataLoad ) local loading = display.newSprite( mySheetLoad, dataLoad ) local countdown = display.newText( "", d.w/2, d.h/3, "fontBasic.ttf", 180 ) local texts = {} dens = display.newText( "", d.w/2, d.h/3, "fontBasic.ttf", 30 ) table.insert( texts, dens ) fric = display.newText( "", d.w/2, d.h/3, "fontBasic.ttf", 30 ) table.insert( texts, fric ) boun = display.newText( "", d.w/2, d.h/3, "fontBasic.ttf", 30 ) table.insert( texts, boun ) grav = display.newText( "", d.w/2, d.h/3, "fontBasic.ttf", 30 ) table.insert( texts, grav ) angDamp = display.newText( "", d.w/2, d.h/3, "fontBasic.ttf", 30 ) table.insert( texts, angDamp ) shap = display.newText( "", d.w/2, d.h/3, "fontBasic.ttf", 30 ) table.insert( texts, shap ) motSpeed = display.newText( "", d.w/2, d.h/3, "fontBasic.ttf", 30 ) table.insert( texts, motSpeed ) lengJoint = display.newText( "", d.w/2, d.h/3, "fontBasic.ttf", 30 ) table.insert( texts, lengJoint ) species = display.newText( "", d.w/2, d.h/3, "fontBasic.ttf", 30 ) table.insert( texts, species ) gend = display.newText( "", d.w/2, d.h/3, "fontBasic.ttf", 30 ) table.insert( texts, gend ) armDens = display.newText( "", d.w/2, d.h/3, "fontBasic.ttf", 30 ) table.insert( texts, armDens ) armFric = display.newText( "", d.w/2, d.h/3, "fontBasic.ttf", 30 ) table.insert( texts, armFric ) armBoun = display.newText( "", d.w/2, d.h/3, "fontBasic.ttf", 30 ) table.insert( texts, armBoun ) armGrav = display.newText( "", d.w/2, d.h/3, "fontBasic.ttf", 30 ) table.insert( texts, armGrav ) armAngDamp = display.newText( "", d.w/2, d.h/3, "fontBasic.ttf", 30 ) table.insert( texts, armAngDamp ) armShap = display.newText( "", d.w/2, d.h/3, "fontBasic.ttf", 30 ) table.insert( texts, armShap ) fitness = display.newText( "", d.w/2, d.h/3, "fontBasic.ttf", 30 ) table.insert( texts, fitness ) gener = display.newText( "", d.w/2, d.h/3, "fontBasic.ttf", 30 ) table.insert( texts, gener ) frequencyText = display.newText( "", d.w/2, d.h/3, "fontBasic.ttf", 30 ) table.insert( texts, frequencyText ) dampRateText = display.newText( "", d.w/2, d.h/3, "fontBasic.ttf", 30 ) table.insert( texts, dampRateText ) highestFit = display.newText( "", d.w+200, d.h/25, "fontBasic.ttf", 60 ) lowestFit = display.newText( "", -200, d.h/25, "fontBasic.ttf", 50 ) loading:play() for k,v in pairs( texts ) do v.anchorX = 0 if k \< 10 then v.x = d.w/2-250 v.y = d.h/5 + k\*28 else v.x = d.w/2+50 v.y = d.h/5 + (k-9)\*28 end end -------------------------------------------------------------------- --SETTINGS---------------------------------------------------------- grassPhysics.alpha = 0 grassFront.alpha = 0.3 grassFront:toFront() statsContrast:toFront() moveGroup:insert( start ) moveGroup:insert( grassFront ) for k,v in pairs(texts) do v:toFront() end statsContrast.alpha = 0 grassBottom.yScale = 3 sky.xScale = 1.65 sky.yScale = 1.85 statsContrast.xScale = 1.55 statsContrast.yScale = 1.75 trees.xScale = 1.55 trees.yScale = 1.75 grass.xScale = 1.55 grass.yScale = 1.75 grassFront.xScale = 1.55 grassFront.yScale = 1.75 scrollRect.alpha = 0.5 loading.xScale = 0.3 loading.yScale = 0.3 breedTimer.anchorX = 1 loading:setFillColor( 0, 0, 255 ) loading.x = d.w/2 loading.y = d.h/1.06 moveGroup:insert( grass ) ------------------------------------------------------------------- --PHYSICS BODIES--------------------------------------------------- physics.addBody( grassPhysics, "static", { filter=grassCollisionFilter, density = 2.65, friction = 0.55, bounce = 0.1 } ) ------------------------------------------------------------------- --FUNCTIONS-------------------------------------------------------- function spawn(i) shape = shapeMain armShape = shapeArm creature[i] = display.newImage( shape..".png", d.w/2, d.h/1.25 ) gender[i] = display.newImage( "gender.png", d.w/2, d.h/1.25 ) creature[i].bounce = bouncy creature[i].friction = frixon creature[i].density = dnsity creature[i].gravity = grvity creature[i].angularDamp = angDamping creature[i].motorSp = motSpe creature[i].jointLen = joinLeng creature[i].shape = shape creature[i].i = i creature[i].fitness = 0 arm[i] = display.newImage( armShape..".png", creature[i].x + creature[i].jointLen, creature[i].y ) arm[i].bounce = bouncyA arm[i].friction = frixonA arm[i].density = dnsityA arm[i].gravity = grvityA arm[i].angularDamp = angDampingA arm[i].dampRatio = dampRateA arm[i].freq = freq arm[i].shape = armShape arm[i].i = i arm[i]:addEventListener( "touch", stats ) creature[i]:addEventListener( "touch", stats ) gender[i]:addEventListener( "touch", stats ) creature[i].color = colr creature[i]:setFillColor( unpack(creature[i].color) ) arm[i].color = colrA arm[i]:setFillColor( unpack(arm[i].color) ) if i \>= batchSize/2 then creature[i].gender = "female" gender[i]:setFillColor( 1 ) table.insert( allWomen, creature[i] ) elseif i \<= batchSize/2 then creature[i].gender = "male" gender[i]:setFillColor( 0 ) table.insert( allMen, creature[i] ) end table.insert( allCreatures, creature[i] ) creature[i].alpha = 1 gender[i].i = i addPhysics( creature[i], arm[i], gender[i], i ) moveGroup:insert( creature[i] ) moveGroup:insert( gender[i] ) moveGroup:insert( arm[i] ) grassFront:toFront() statsContrast:toFront() for k,v in pairs(texts) do v:toFront() end return true end function addPhysics( c, a, g, i ) outline = graphics.newOutline( 1, a.shape..".png" ) physics.addBody( a, "dynamic", { outline = outline, filter = creatureCollisionFilter, density = a.density, friction = a.friction, bounce = a.bounce } ) outline = graphics.newOutline( 1, c.shape..".png" ) physics.addBody( c, "dynamic", { outline = outline, filter = creatureCollisionFilter, density = c.density, friction = c.friction, bounce = c.bounce } ) outline = graphics.newOutline( 1, "gender.png" ) physics.addBody( g, "dynamic", { outline = outline, filter = genderCollisionFilter } ) motor[i] = physics.newJoint( "pivot", g, c, c.x, c.y ) motor[i].isMotorEnabled = true motor[i].maxMotorTorque = 1000000 motor[i].motorSpeed = c.motorSp distance[i] = physics.newJoint( "weld", g, a, g.x, g.y ) distance[i].dampingRatio = a.dampRatio distance[i].frequency = a.freq c.gravityScale = c.gravity c.angularDamping = c.angularDamp a.gravityScale = a.gravity a.angularDamping = a.angularDamp return true end function scrollView( event ) if event.y \> d.h/1.2 then if event.phase == "began" then scrollRect.alpha = 0.5 scrollRect.x = event.x moveGroup.x = (scrollRect.x-d.w/2)\*(-10\*coefficient) scrollRect:toFront() statsContrast:toFront() for k,v in pairs(texts) do v:toFront() end elseif event.phase == "moved" then scrollRect.x = event.x scrollRect.alpha = 0.4 moveGroup.x = (scrollRect.x-d.w/2)\*(-10\*coefficient) scrollRect:toFront() statsContrast:toFront() for k,v in pairs(texts) do v:toFront() end elseif event.phase == "ended" then scrollRect.alpha = 0.3 scrollRect:toFront() statsContrast:toFront() for k,v in pairs(texts) do v:toFront() end end end return true end function gps() for k, v in pairs(allCreatures) do if gpsPause == 0 then if v.x ~= nil then v.fitness = v.x - start.x end end if v.i == fitnessWatch then fitness.text = "DISTANCE (m): "..math.floor(v.fitness)/100 end if math.abs(v.fitness) \< lowestFitness then lowestFitness = v.fitness lowestFit.text = "SMALLEST DISTANCE (m): "..math.floor(v.fitness)/100 elseif math.abs(v.fitness) \> math.abs(highestFitness) then highestFitness = v.fitness highestFit.text = "FARTHEST DISTANCE (m): "..math.floor(v.fitness)/100 end if lowestFitness/100 \< -40 or highestFitness/100 \> 40 then if done == 0 then done = 1 coefficient = 10 transition.to( scrollRect, { time = 1000, width = 20 } ) for i = 1, 7 do grassLong[i] = display.newImage( "grass.png", (i-1)\*-10000-5000, d.h/1.2 ) moveGroup:insert( grassLong[i] ) grassLong[i].anchorX = 1 grassLong[i].xScale = 1.55 grassLong[i].yScale = 1.75 end for i = 1, 7 do grassLong[i] = display.newImage( "grass.png", (i-1)\*10000+5000, d.h/1.2 ) moveGroup:insert( grassLong[i] ) grassLong[i].anchorX = 1 grassLong[i].xScale = 1.55 grassLong[i].yScale = 1.75 end end end end timer.performWithDelay( 100, gps ) return true end function breed(i) for k,v in pairs( top ) do if i == k then jf = pos[v] f = creature[jf] fa = arm[jf] fg = gender[jf] end if i+batchSize/4 == k then jm = pos[v] m = creature[jm] ma = arm[jm] mg = gender[jm] end end if math.random(1,2) == 1 then angDampingA = ma.angularDamp else angDampingA = fa.angularDamp end if math.random(1,2) == 1 then grvityA = ma.gravity else grvityA = fa.gravity end if math.random(1,2) == 1 then dampRateA = ma.dampRatio else dampRateA = fa.dampRatio end if math.random(1,2) == 1 then freq = ma.freq else freq = fa.freq end if math.random(1,2) == 1 then colrA = ma.color else colrA = fa.color end if math.random(1,2) == 1 then dnsityA = ma.density else dnsityA = fa.density end if math.random(1,2) == 1 then frixonA = ma.friction else frixonA = fa.friction end if math.random(1,2) == 1 then bouncyA = ma.bounce else bouncyA = fa.bounce end if math.random(1,2) == 1 then joinLeng = m.jointLen else joinLeng = f.jointLen end if math.random(1,2) == 1 then motSpe = m.motorSp else motSpe = f.motorSp end if math.random(1,2) == 1 then colr = m.color else colr = f.color end if math.random(1,2) == 1 then angDamping = m.angularDamp else angDamping = f.angularDamp end if math.random(1,2) == 1 then grvity = m.gravity else grvity = f.gravity end if math.random(1,2) == 1 then dnsity = m.density else dnsity = f.density end if 1 == 1 then frixon = m.friction else frixon = f.friction end if math.random(1,2) == 1 then bouncy = m.bounce else bouncy = f.bounce end if math.random(1,2) == 1 then shapeArm = ma.shape else shapeArm = fa.shape end if math.random(1,2) == 1 then shapeMain = m.shape else shapeMain = f.shape end table.insert( TcolrA, colrA ) table.insert( TdampRateA, dampRateA ) table.insert( Tfreq, freq ) table.insert( TangDampingA, angDampingA ) table.insert( TgrvityA, grvityA ) table.insert( TfrixonA, frixonA ) table.insert( TbouncyA, bouncyA ) table.insert( TjointLeng, joinLeng) table.insert( TmotSpe, motSpe) table.insert( Tcolr, colr) table.insert( TangDamping, angDamping) table.insert( Tgrvity, grvity) table.insert( Tdnsity, dnsity) table.insert( Tfrixon, frixon) table.insert( Tbouncy, bouncy) table.insert( TshapeArm, shapeArm) table.insert( TshapeMain, shapeMain) table.insert( TdnsityA, dnsityA ) return true end function spawnGenetics() i = i + 1 if i \<= batchSize/4 then countdown.alpha = 1 countdown.text = "3" elseif i \<= batchSize/2 then countdown.text = "2" elseif i \<= (batchSize/4)\*3 then countdown.text = "1" elseif i == batchSize then countdown.text = "GO!!" transition.to( countdown, { time = 500, alpha = 0 } ) end if i \> batchSize then i = 0 refresh(1) gpsPause = 0 gpsStart(10000) else for k,v in pairs( TdampRateA ) do if k == i then dampRateA = v end end for k,v in pairs( TangDamping ) do if k == i then angDamping = v end end for k,v in pairs( Tfreq ) do if k == i then freq = v end end for k,v in pairs( TgrvityA ) do if k == i then grvityA = v end end for k,v in pairs( TdnsityA ) do if k == i then dnsityA = v end end for k,v in pairs( TfrixonA ) do if k == i then frixonA = v end end for k,v in pairs( TbouncyA ) do if k == i then bouncyA = v end end for k,v in pairs( TjointLeng ) do if k == i then joinLeng = v end end for k,v in pairs( TmotSpe ) do if k == i then motSpe = v end end for k,v in pairs( Tcolr ) do if k == i then colr = v end end for k,v in pairs( TangDampingA ) do if k == i then angDampingA = v end end for k,v in pairs( Tgrvity ) do if k == i then grvity = v end end for k,v in pairs( Tdnsity ) do if k == i then dnsity = v end end for k,v in pairs( Tfrixon ) do if k == i then frixon = v end end for k,v in pairs( Tbouncy ) do if k == i then bouncy = v end end for k,v in pairs( TshapeArm ) do if k == i then shapeArm = v end end for k,v in pairs( TshapeMain ) do if k == i then shapeMain = v end end for k,v in pairs( TcolrA ) do if k == i then colrA = v end end spawn(i) timer.performWithDelay( 0.1, spawnGenetics ) end return true end function coupleUp() i = i + 1 if i \<= batchSize/4 then breed( i ) breed( i ) breed( i ) breed( i ) timer.performWithDelay( 0.1, coupleUp) else i = 0 refresh() spawnGenetics() gpsPause = 1 end return true end local function compare1( a, b ) return math.abs(a) \< math.abs(b) end local function compare( a, b ) return math.abs(a) \> math.abs(b) end function sort() physics.pause() generation = generation + 1 for k,v in pairs( allMen ) do pos[v.fitness] = v.i table.insert(scoresMen, v.fitness) end for k,v in pairs( allWomen ) do pos[v.fitness] = v.i table.insert(scoresWomen, v.fitness) end table.sort( scoresMen, compare ) table.sort( scoresWomen, compare1 ) top = {} top = table.copy( scoresWomen, scoresMen ) i = 0 coupleUp() done = 0 fitnessWatch = 70 highestFitness = -1 highestFit.text = "" lowestFitness = 1 lowestFit.text = "" fitness.text = "" coefficient = 1 breedTimer.x = 1040 return true end function gpsStart(timerLength) physics.start() if (loading) then loading:pause() display.remove(loading) mySheetLoad = nil loading = nil dataLoad = nil sheetDataLoad = nil end gps() transition.to( breedTimer, { time = timerLength, x = -400, onComplete = sort } ) transition.to( countdown, { time = 1000, alpha = 0 } ) return true end function statsToggle( event ) transition.to( statsContrast, { time = 50, alpha = 0 } ) for k,v in pairs( texts ) do v.alpha = 0 end fitnessWatch = 70 return true end function stats(event) if event.phase == "ended" then transition.to( statsContrast, { time = 50, alpha = 1 } ) for k,v in pairs(texts) do v:toFront() v.alpha = 1 end c = creature[event.target.i] a = arm[event.target.i] gener.text = "GENERATION: "..generation boun.text = "BOUNCE: "..c.bounce dens.text = "DENSITY: "..c.density fric.text = "FRICTION: "..c.friction armFric.text = "ARM FRICTION: "..a.friction armBoun.text = "ARM BOUNCE: "..a.bounce armAngDamp.text = "ARM ANGULAR DAMPING: "..a.angularDamp armDens.text = "ARM DENSITY: "..a.density angDamp.text = "ANGULAR DAMPING: "..c.angularDamp grav.text = "GRAVITY SCALE: "..c.gravity armGrav.text = "ARM GRAVITY SCALE: "..a.gravity gend.text = "GENDER: "..c.gender motSpeed.text = "MOTOR SPEED: "..c.motorSp lengJoint.text = "ARM LENGTH: "..c.jointLen fitnessWatch = event.target.i armShap.text = "ARM SHAPE: "..a.shape shap.text = "SHAPE: "..c.shape frequencyText.text = "ARM SUSPENSION: "..a.freq if c.shape == "pentagon" then species.text = "SPECIES: P" elseif c.shape == "rectangle" then species.text = "SPECIES: R" elseif c.shape == "triangle" then species.text = "SPECIES: T" elseif c.shape == "star" then species.text = "SPECIES: S" else species.text = "SPECIES: " end if a.shape == "Pentagon " then species.text = species.text.."1" elseif a.shape == "Rectangle " then species.text = species.text.."2" elseif a.shape == "Triangle " then species.text = species.text.."3" elseif a.shape == "Star " then species.text = species.text.."4" else species.text = "SPECIES: " end end return true end local function intro() i = i + 1 colr1 = {255,255,0} colr2 = {0,191,255} colr3 = {220,0,40} colr4 = {0,255,0} num = math.random(1,4) if num == 1 then colrA = colr1 elseif num == 2 then colrA = colr2 elseif num == 3 then colrA = colr3 else colrA = colr4 end dampRateA = math.random( 0, 100 )/100 angDampingA = math.random( 0, 100 )/100 grvityA = math.random( 70, 200 )/100 dnsityA = math.random( 0, 10000 )/100 frixonA = math.random( 0,10000 )/100 bouncyA = math.random( 0, 70 )/100 freq = math.random( -500, 2000 )/100 num = math.random(1,4) if num == 1 then colr = colr1 elseif num == 2 then colr = colr2 elseif num == 3 then colr = colr3 else colr = colr4 end joinLeng = math.random( -150, 150 ) motSpe = math.random( -1000, 1000 ) angDamping = math.random( 0, 100 )/100 grvity = math.random( 50, 200 )/100 dnsity = math.random( 0, 10000 )/100 frixon = math.random( 0, 10000 )/100 bouncy = math.random( 0, 90 )/100 shapeArm1 = "Triangle " shapeArm2 = "Rectangle " shapeArm3 = "Pentagon " shapeArm4 = "Star " shape1 = "triangle" shape2 = "rectangle" shape3 = "pentagon" shape4 = "star" num = math.random(1,4) if num == 1 then shapeArm = shapeArm1 elseif num == 2 then shapeArm = shapeArm2 elseif num == 3 then shapeArm = shapeArm3 else shapeArm = shapeArm4 end num = math.random(1,4) if num == 1 then shapeMain = shape1 elseif num == 2 then shapeMain = shape2 elseif num == 3 then shapeMain = shape3 else shapeMain = shape4 end if i \<= batchSize/4 then countdown.text = "3" elseif i \<= batchSize/2 then countdown.text = "2" elseif i \<= (batchSize/4)\*3 then countdown.text = "1" elseif i == batchSize then countdown.text = "GO!!" transition.to( countdown, { time = 500, alpha = 0 } ) end physics.pause() if i \<= batchSize then spawn(i) timer.performWithDelay( 0.1, intro) else gpsStart(5000) end return true end local function checkMemory() collectgarbage("collect") print( string.format( "MEMORY = %.5f KB", collectgarbage( "count" ) ) ) end function refresh(after) if after ~= 1 then for k,v in pairs(allCreatures) do id = v.i display.remove( v ) display.remove( arm[id] ) display.remove( gender[id] ) v = nil creature[id] = nil arm[id] = nil gender[id] = nil end for k,v in pairs(texts) do v.text = "" end statsContrast.alpha = 0 local allCreatures = nil local allCreatures = {} for k,v in pairs( allMen ) do pos[v.fitness] = nil v = nil end local allMen = nil local allMen = {} for k,v in pairs( allWomen ) do pos[v.fitness] = nil v = nil end local allWomen = nil local allWomen = {} for k,v in pairs( top ) do v = nil end local top = nil local top = {} for k,v in pairs( scoresWomen ) do v = nil end local scoresWomen = nil local scoresWomen = {} for k,v in pairs( scoresMen ) do v = nil end local scoresMen = nil local scoresMen = {} local creature = nil local gender = nil local arm = nil local motor = nil local distance = nil local pos = nil local creature = {} local gender = {} local arm = {} local motor = {} local distance = {} local pos = {} local num = nil local colr1 = nil local colr2 = nil local colr3 = nil local colr4 = nil local shapeArm1 = nil local shapeArm2 = nil local shapeArm3 = nil local shapeArm4 = nil local shape1 = nil local shape2 = nil local shape3 = nil local shape4 = nil else for k,v in pairs( TdampRateA ) do v = nil end local TdampRateA = nil local TdampRateA = {} for k,v in pairs( TangDamping ) do v = nil end local TangDamping = nil local TangDamping = {} for k,v in pairs( Tfreq ) do v = nil end local Tfreq = nil local Tfreq = {} for k,v in pairs( TgrvityA ) do v = nil end local TgrvityA = nil local TgrvityA = {} for k,v in pairs( TdnsityA ) do v = nil end local TdnsityA = nil local TdnsityA = {} for k,v in pairs( TfrixonA ) do v = nil end local TfrixonA = nil local TfrixonA = {} for k,v in pairs( TbouncyA ) do v = nil end local TbouncyA = nil local TbouncyA = {} for k,v in pairs( TjointLeng ) do v = nil end local TjointLeng = nil local TjointLeng = {} for k,v in pairs( TmotSpe ) do v = nil end local TmotSpe = nil local TmotSpe = {} for k,v in pairs( Tcolr ) do v = nil end local Tcolr = nil local Tcolr = {} for k,v in pairs( TangDampingA ) do v = nil end local TangDampingA = nil local TangDampingA = {} for k,v in pairs( Tgrvity ) do v = nil end local Tgrvity = nil local Tgrvity = {} for k,v in pairs( Tdnsity ) do v = nil end local Tdnsity = nil local Tdnsity = {} for k,v in pairs( Tfrixon ) do v = nil end local Tfrixon = nil local Tfrixon = {} for k,v in pairs( Tbouncy ) do v = nil end local Tbouncy = nil local Tbouncy = {} for k,v in pairs( TshapeArm ) do v = nil end local TshapeArm = nil local TshapeArm = {} for k,v in pairs( TshapeMain ) do v = nil end local TshapeMain = nil local TshapeMain = {} for k,v in pairs( TcolrA ) do v = nil end local TcolrA = nil local TcolrA = {} end return true end ------------------------------------------------------------------ --LISTENERS------------------------------------------------------- intro() statsContrast:addEventListener( "touch", statsToggle ) grassBottom:addEventListener( "touch", scrollView ) Runtime:addEventListener( "enterFrame", checkMemory ) ------------------------------------------------------------------ return scene

Sorry some of the code has nothing to do with it but i didnt wanna make it not work anymore by removing parts of it…

My first step would be to keep a reference every time you call transition.to and timer.performWithDelay, and periodicially remove those manually. In my experience each time you call these without keeping a reference and later cancelling & nilling, you get a small jump in memory usage.

See the timers/transitions section here:

https://jonbeebe.net/2011/05/corona-sdk-memory-management-101/

thanks for the quick reply! I tried this method and i still gain 70-80 kb per spawn though…

@antoine,

Does your memory usage grow without bound or does it plateau?

A small growth that levels out is not a leak.

Please package up your project in a zip file and share it here via a link so we can see this actually happening.  Also, this will make examining the code more reasonable.  

-Ed

PS - Does your program exist in a single file?

My guess here is that you have been putting references into tables then not clearing those references.  Thus the objects can’t be garbage collected.   

ill attach it to this message

also the memory continues to grow until it crashes (20k or more kb is when it starts to crash)

here is the link:

https://drive.google.com/file/d/0Byb00SQq8FeoTkNkcTRRMnJOcTA/view?usp=sharing

but i use a for loop and nil out all the objects in every table, unless thats incorrect…

I looked, but your code is is over-complicated.  

Using tables to manage all of these objects is unnecessary.  If you just want an easy way to purge all of those objects on occasion, put them in a display group and destroy the group.

I thought I’d be able to help, but at 1000 lines its beyond my free time helping limit.  Sorry.

sorry, ill work on shortening/optimizing the code

ill post a link when its better (this is really a rough draft version)

local purgeGroup = display.newGroup() display.newCircle( purgeGroup, 10, 10, 10 ) display.newCircle( purgeGroup, 10, 20, 10 ) display.newCircle( purgeGroup, 10, 30, 10 ) display.remove(purgeGroup) -- All 3 circles are gone.

Hey, I understand.  This is what I think of as long-hand or train-of-thought code.  I get it.

It would benefit a lot from modularization and a reduction in the use of tables.  If you don’t need to track an object for updating or examination, don’t put it in a table.  Just put it in a display group.

Then, you can destroy the display group later to destroy all objects in that group.  

Finally, you should be left with one or two tables that are tracking objects.  You can simply set the references to those tables to nil and viola you’re done.

thanks! really helps!! :slight_smile:

My first step would be to keep a reference every time you call transition.to and timer.performWithDelay, and periodicially remove those manually. In my experience each time you call these without keeping a reference and later cancelling & nilling, you get a small jump in memory usage.

See the timers/transitions section here:

https://jonbeebe.net/2011/05/corona-sdk-memory-management-101/

thanks for the quick reply! I tried this method and i still gain 70-80 kb per spawn though…

@antoine,

Does your memory usage grow without bound or does it plateau?

A small growth that levels out is not a leak.

Please package up your project in a zip file and share it here via a link so we can see this actually happening.  Also, this will make examining the code more reasonable.  

-Ed

PS - Does your program exist in a single file?

My guess here is that you have been putting references into tables then not clearing those references.  Thus the objects can’t be garbage collected.   

ill attach it to this message

also the memory continues to grow until it crashes (20k or more kb is when it starts to crash)

here is the link:

https://drive.google.com/file/d/0Byb00SQq8FeoTkNkcTRRMnJOcTA/view?usp=sharing

but i use a for loop and nil out all the objects in every table, unless thats incorrect…

I looked, but your code is is over-complicated.  

Using tables to manage all of these objects is unnecessary.  If you just want an easy way to purge all of those objects on occasion, put them in a display group and destroy the group.

I thought I’d be able to help, but at 1000 lines its beyond my free time helping limit.  Sorry.

sorry, ill work on shortening/optimizing the code

ill post a link when its better (this is really a rough draft version)