Question regarding modules and memory usage

I have a question regarding system memory usage when using composer and modules, like in the following example:

I have a mainmenu and a level as different scenes.

I use a lot of modules which are required outside the composer functions, like this:

A sound module:

-- the sound.lua module code local mod={} local createAllSounds = function() -- code to create (load) all sounds here end mod.createAllSounds=createAllSounds return mod

Module sample 2:

-- the module1.lua local mod={} local createAllGraphics = function() -- code to create all Graphics here end mod.createAllGraphics=createAllGraphics return mod

Now for example I get to my mainmenu scene I have a system memory of about 2 MB in use.

When I get to my level.lua where I require the modules I then have in level.lua about 10 MB of system memory used.

Require sample in the level.lua file:

-- the level lua -- requiring outside the composer functions: local mygraphics=require ("module1") local mysounds=require("sound") ... composer scenes and level code here...

Now when getting back from level.lua to the mainmenu I still have a lot of system memory in use, like for example about 8MB

When going to the level.lua again and then back to the mainmenu and continuing to do so, the system memory stays at 8MB which now let me wonder if this is because of the very first requiring of the modules?

Are they still kept in memory all the time?

What about requiring the same modules in different other scenes and even other modules? What does this mean for the memory usage?

this has nothing to do with the modules themselves.  (because i can guarantee you that the _ code _ for those two modules is _ not _ what is consuming the 8M difference - the code shown isn’t more than a few hundred _ bytes _ compiled)

rather, it’s probably the loaded sound files (sounds will occupy system memory, whereas loaded graphics occupy texture memory)

try creating a “disposeAllSounds()” method, call it when leaving “level.lua”, THEN see what your memory usage is.

and always be sure to sprinkle in a call to collectgarbage(“collect”) before calling collectgarbage(“count”)

then also start monitoring texture memory, create a disposeAllGraphics()" method, call it after level.lua, see what you memory usage is.

thanks! One question regarding sound disposal:

When removing sound for this:

local sfx = {blackhole,click,cannonshot1,...}

is this the correct way?:

for k,v in pairs (sfx) do         if v then             audio.dispose( v )             v=nil         end     end

You don’t need to set v to nil here because it’s just a local within the scope of the loop.

You may set sfx to nil after the loop (or set the entries to nil within the loop, i.e. sfx[k] = nil after your dispose call)

this has nothing to do with the modules themselves.  (because i can guarantee you that the _ code _ for those two modules is _ not _ what is consuming the 8M difference - the code shown isn’t more than a few hundred _ bytes _ compiled)

rather, it’s probably the loaded sound files (sounds will occupy system memory, whereas loaded graphics occupy texture memory)

try creating a “disposeAllSounds()” method, call it when leaving “level.lua”, THEN see what your memory usage is.

and always be sure to sprinkle in a call to collectgarbage(“collect”) before calling collectgarbage(“count”)

then also start monitoring texture memory, create a disposeAllGraphics()" method, call it after level.lua, see what you memory usage is.

thanks! One question regarding sound disposal:

When removing sound for this:

local sfx = {blackhole,click,cannonshot1,...}

is this the correct way?:

for k,v in pairs (sfx) do         if v then             audio.dispose( v )             v=nil         end     end

You don’t need to set v to nil here because it’s just a local within the scope of the loop.

You may set sfx to nil after the loop (or set the entries to nil within the loop, i.e. sfx[k] = nil after your dispose call)