Memory usage of a function

Hi guys, 

I have bulit a little function for my game that shows a ‘start screen’ before the game starts. 

After implementing the function, the memory  usage is about 120kB higher then without the function. (I imagine the performance is also worse) 

Is that normal or do I have a menory leak? 

Here is the function: 

function StartScreen() --vis = true local highsore = GetHighscore() local SS = display.newGroup( ) local clouds = display.newImage(SS, "StartScreen/clouds.png", ScreenW/4, 400 ) local head = display.newImage(SS, "StartScreen/head\_ed.png", ScreenW/4, 200 ) local ground = display.newImage(SS, "StartScreen/ground.png", ScreenW/4, ScreenH-100 ) local cat = display.newImage(SS, "StartScreen/cat.png", ScreenW/4+200, ScreenH-350 ) local bird = display.newImage(SS, "StartScreen/bird.png", ScreenW/4-200, ScreenH/2-100 ) local highsoreText = display.newBitmapText(SS, "Highscore: ".. highsore, 50, ScreenH - 500, "23/font", 35 ) local rankText = display.newBitmapText( SS, "Rank: - ", 50, ScreenH - 425, "23/font", 40 ) highsoreText:setAnchor(0,0.5) rankText:setAnchor(0,0.5) local Button1 local B1image local Button2 local B2image local function handleButtonEvent( event ) if ( "ended" == event.phase ) then vis = false SS:removeSelf( ) SS= nil GameStart() end end local function handleButtonEvent2( event ) if ( "ended" == event.phase ) then showLeaderboards( event ) end end local BH = 80 local B1x = 160 local B2x = 350 local B1y = ScreenH - 250 -- Create the widget Button1 = widget.newButton( { x = B1x, y = B1y, shape = "circle", radius = 55, defaultFile = "play.png", fillColor = { default={RGB(240,135,0)}, over={RGB(240,135,0)}}, strokeColor = { default={0,0,0}, over={0,0,0} }, strokeWidth = 4, onEvent = handleButtonEvent } ) SS:insert(Button1) Button2 = widget.newButton( { x = B2x, y = B1y, shape = "circle", radius = 55, defaultFile = "play.png", fillColor = { default={RGB(240,135,0)}, over={1,0.1,0.7,0.4} }, strokeColor = { default={0,0,0}, over={0,0,0} }, strokeWidth = 4, onEvent = handleButtonEvent2 } ) SS:insert(Button2) B1image = display.newImage(SS,"play.png" , B1x, B1y) B1image:scale( 0.8, 0.8 ) B2image = display.newImage(SS, "list2.png" , B2x, B1y) B2image:scale( 0.8, 0.8 ) B1image:toFront( ) --GetRankGPGS(SS, 7) end

The function you listed is defined as a global:

function StartScreen() ... end

If you don’t need it to be global, do this:

local function StartScreen() ... end

As far as a leak, sorta.  I consider accidental global creation to be a leak.  In your case, the memory for that function will never be freed up.

On the other hand, functions do take up memory, so if you define functions and they don’t get destroyed (on purpose) that is not a leak.  

As far as performance.  The difference will be negligible.  You’ll get a small cost to define the function, the a super-super-tiny single time cost for looking up the function when you call it, then the body will cost would it normally would if you didn’t have it in a function.

PS - The cost to define the function will mostly go away in the compiled version.

Hey roaminggamer,

thank you for the help, but that was not the main problem.

I figured out that the newBitmap function brings a new Bitmap font in the game which reserved a lot of memory. 

The function you listed is defined as a global:

function StartScreen() ... end

If you don’t need it to be global, do this:

local function StartScreen() ... end

As far as a leak, sorta.  I consider accidental global creation to be a leak.  In your case, the memory for that function will never be freed up.

On the other hand, functions do take up memory, so if you define functions and they don’t get destroyed (on purpose) that is not a leak.  

As far as performance.  The difference will be negligible.  You’ll get a small cost to define the function, the a super-super-tiny single time cost for looking up the function when you call it, then the body will cost would it normally would if you didn’t have it in a function.

PS - The cost to define the function will mostly go away in the compiled version.

Hey roaminggamer,

thank you for the help, but that was not the main problem.

I figured out that the newBitmap function brings a new Bitmap font in the game which reserved a lot of memory.