Need help with tracking down "out of memory" bugs

, ,

Hey everyone! I'm trying to track down an out of memory problem in the game I'm working on. Do y'all have any tips for hunting down memory issues? some context:

It seems to be appearing at a specific spot, but only after going through a bunch of levels.

I do gfx.removeAll() when we do a scene change, so it seems unlikely that it's a sprite issue

i use an image cache so if we're asking for images of the same size or path as one already made, we supply that one instead.

local function getImageFromPath(path, id)
  if imageMap[id] then
    return imageMap[id];
  else
    local img = gfx.image.new(path);
    imageMap[id] = img;
    return img;
  end
end

local function getImage(width, height, id)
  if imageMap[id] and poolImages then
      gfx.lockFocus(imageMap[id]);
      gfx.clear(gfx.kColorClear)
      gfx.unlockFocus();
      return imageMap[id]
  else
    local img = gfx.image.new(width, height);
    imageMap[id] = img;
    return img;
  end
end

i use the fileplayer for playing music and ambience

self.player:stop();
self.player:load(self.songs[self.songToPlay])
self.player:play(0);
self.player:setVolume(self.volume, self.volume, self.fadeNextAmount)`

and samples for sfx

function SoundManager:setup(sounds)
  self.sounds = {}
  for id, path in pairs(sounds) do
      self.sounds[id] = playdate.sound.sample.new(path);
  end
end
function SoundManager:play(sound, volume) 
  return
  self.sounds[sound]:playAt(0, volume);
end

Do these approaches have potential memory issues I should be aware of? Are there other pitfalls I might be running into? I'm a little inexperienced with debugging memory issues. I've tried using the malloc pool, but it doesn't show any sort of leak over time or abnormally high memory usage. Any help would be appreciated!

Lua can keep references to things if you're not careful.

You can track memory usage before and after your releases to make sure they're doing what you expect.

https://devforum.play.date/t/tracking-memory-usage-throughout-your-game/1132