Memory Usage question (detecting leaks)

I found some code somewhere and I’m using it to try and track memory leaks.

My game fires up listeners at the beginning and are not re-called. I think I’m cleaning up, so I’m trying to understand the values returned.

collectgarbage("collect")  
 local systemMemory = collectgarbage("count")  
 print("\*\*MEMORY\*\* At Start of Game - System Memory " .. systemMemory .. " Texture Memory: " .. system.getInfo("textureMemoryUsed") .. " level " .. level)  

I call it at the beginning of the game and at the beginning of each time I die.

The good news is that the Texture Memory seems to be the same every round. It starts higher at the beginning of the game (or when the game is over) but drops when it gets to the Start Round function.

What I’m having trouble with is the collectgarbage(count) return value. First what is that measured in? I’m getting values like 248.2919921875. Is that in megabytes? I can’t believe my game is 248M. The Texture memory is reporting in the 18-19mb range (19,034,112). My total compressed app is around 10mb. My audio is around 5mb at most.

Is my collectgarbage(count) way out of line? This is a reasonably small space shooter.

Oh it starts at 243.xxxx and after a few runs its up to 261.xxxx. Since my texture memory isn’t going up. I’m keeping all my sounds around since I use them over and over. I’m not calling my addListener’s more than once. So about all I can think of thats left are timers and tweens and I think I’m doing a good job of cleaning them up.

Rob [import]uid: 19626 topic_id: 9178 reply_id: 309178[/import]

According to Lua, the number returned is in Kilobytes. So 256K + 19M texture memory is probably a fairly small footprint.

So now to find out what’s not getting collected.

Rob [import]uid: 19626 topic_id: 9178 reply_id: 33498[/import]

You can’t track memory leaks inside your code. You have to use Xcode Instruments. [import]uid: 51516 topic_id: 9178 reply_id: 33550[/import]

I found out why I was having problems with Instruments. You have to use a Development provision profile not a distribution profile.

Now its found 3 leaks in my program, but the information doesn’t seem to be exceptionally helpful. I see some are in UIKit, and others in Audio Toolbox.

How do I map this back to Lua/Corona code? When I drill down I get assymbly code but there are no symbols that are any reference back to my Lua code.

Any ideas? [import]uid: 19626 topic_id: 9178 reply_id: 33602[/import]

I ran Instruments again tonight and I see there were quite a few leaks coming from _vasprintf_l. I don’t see how any of my print’s could be leaking unless its a Coronoa/Lua issue. So that leaves my string.format() calls.

I could see where my high score screen might possibly have a problem, but I never loaded the high score screen during my Instruments run, so that leaves code like this:

 local enemy = display.newImage(string.format("enemy\_%02d.png", enemyId))  

but I wouldn’t think that would create a problem in _vasprintf_l.

I’ve got two leaks in AU3DMixerEmbeddedInputElement. I have one stream, and everything else is loaded into a local (to main.lua) variable in the main chunk and I only do audio.play() on those items for the duration of the game except in one place, where I load the sound on the fly, but I think I’m cleaning it up okay.

local function endAlienVoice(event)  
 audio.dispose(event.handle)  
 return true  
end  
  
local function playAlienVoice()   
 if soundOn then  
 local idx = math.random(12)  
 local alienvoice = audio.loadSound(alienSfx[idx])  
 audio.play(alienvoice, { channel=4, onComplete=endAlienVoice })  
 end  
end  

alienSfx is a table of 12 file names of the mp3 files to play.

The others show up in UIKit and I have no clue how to track them down.

Help!
Rob [import]uid: 19626 topic_id: 9178 reply_id: 33662[/import]