use a lot of memory

Hi! i´m a newbie corona developer and working in my first game but my game use lots of memory and when i try in android device, the game “crash” when i passed the  640.000 KB. i don´t know if it is because of sprites or images. I removed everything it’s possible to remove in the images and sprites images/animations. i´m using storyboard scenes and i had many classes for many different things for a best organization but i seen that use lot of memory and now i have only the classes if really needed and i put the rest in only .lua file ( sceneGame.lua ).

I use this function to see the texture memory and memory in KB:

[lua]local function checkMemory()
collectgarbage( “collect” )
local memUsage_str = string.format( “MEMORY = %.3f KB”, collectgarbage( “count” ) )
print( memUsage_str, "TEXTURE = "…(system.getInfo(“textureMemoryUsed”) / (1024 * 1024) ) )
end
timer.performWithDelay( 1000, checkMemory, 0 )[/lua]

and returns this: MEMORY = 619.774 KB    TEXTURE = 59.587890625

so like that the android device opens the game app and it works but i need put more images, functions and events to finished the game.

Can someone help me, what i have to do, if exist some limit value for memory or something?

Thanks and sucess for your app´s :slight_smile:

Memory cannot be problem because < 1 MB ‘ram’ is nothing. Textures may be considered more, because it’s closer to weakest devices memory but still shouldn’t be such problem.

Try this (it’s obviously strange but once worked for me): try to call first collectgarbage() without ‘collect’ argument. I know it is the same but give it a try :slight_smile:

Memory cannot be problem because < 1 MB ‘ram’ is nothing. Textures may be considered more, because it’s closer to weakest devices memory but still shouldn’t be such problem.

Try this (it’s obviously strange but once worked for me): try to call first collectgarbage() without ‘collect’ argument. I know it is the same but give it a try :slight_smile:

If the problem is also because of the texture memory, how can I solve?

Can lead to that value had to remove some animated sprites but the application closes by itself

Thanks :wink:

If you do the reverse math (59.587890625 * 1024 x 1024) on your texture memory, you’re currently using 62.482MB of texture memory. That has to be your hog. Is this right at the launch of the app or does the texture memory continue to grow as you switch through scenes? If it’s happening as you switch scenes, you may not be removing display items and your app just keeps piling them on top of each other.

Hi @ricardo.andronico,

Please review the guide on Performance and Optimization below. This discusses texture memory and some tips on how to keep it as low as possible.

http://docs.coronalabs.com/guide/basics/optimization/index.html

Sincerely,

Brent Sorrentino

now the error is: “function at line 536 has more than 60 upvalues” how to avoid this: 

Be Careful…

Local references in functions are nil’ed out when the function ends. That is, they only live for the time that the function is executing. These will be cleaned up for you.

Where you can create a small memory leak is if you do this:

local enemy

local function spawnEnemy()
enemy = display.newImageRect( “badguy.com”, 64, 64 )
return enemy
end

function scene:createScene( event )

local group = scene.view
local background = display.newRect( 0, 0, display.contentWidth, display.contentHeight )
group:insert( background )
local doSomething = display.newImageRect( “button.png”, 64, 32 )
group:insert( doSomething )

for i = 1, 100 do
group:insert( spawnEnemy() )
end
end

Similar to the first example, the enemies will all be cleaned up when Storyboard destroys the scene, however the “enemy” variable will stick around until the scene is “un-required.”

The table returned by spawnEnemy() is cleaned up because the group retains a reference to it as a remnant copy of the last enemy allocated. Of course if you re-enter the scene, this block will continue to persist, it probably should get nil‘led out, but its not that much memory and it’s not going to grow.

use a classe with function to create the objects? how i put many stuffs on “createScene”?

sorry

This is my game scene lua File. where i´m wrong? the return values of memory is: MEMORY = 592.017 KB    TEXTURE = 39.66796875

I managed to remove much of the texture memory :slight_smile:

http://snk.to/f-c7p3g3ic

Memory cannot be problem because < 1 MB ‘ram’ is nothing. Textures may be considered more, because it’s closer to weakest devices memory but still shouldn’t be such problem.

Try this (it’s obviously strange but once worked for me): try to call first collectgarbage() without ‘collect’ argument. I know it is the same but give it a try :slight_smile:

Memory cannot be problem because < 1 MB ‘ram’ is nothing. Textures may be considered more, because it’s closer to weakest devices memory but still shouldn’t be such problem.

Try this (it’s obviously strange but once worked for me): try to call first collectgarbage() without ‘collect’ argument. I know it is the same but give it a try :slight_smile:

If the problem is also because of the texture memory, how can I solve?

Can lead to that value had to remove some animated sprites but the application closes by itself

Thanks :wink:

If you do the reverse math (59.587890625 * 1024 x 1024) on your texture memory, you’re currently using 62.482MB of texture memory. That has to be your hog. Is this right at the launch of the app or does the texture memory continue to grow as you switch through scenes? If it’s happening as you switch scenes, you may not be removing display items and your app just keeps piling them on top of each other.

Hi @ricardo.andronico,

Please review the guide on Performance and Optimization below. This discusses texture memory and some tips on how to keep it as low as possible.

http://docs.coronalabs.com/guide/basics/optimization/index.html

Sincerely,

Brent Sorrentino

now the error is: “function at line 536 has more than 60 upvalues” how to avoid this: 

Be Careful…

Local references in functions are nil’ed out when the function ends. That is, they only live for the time that the function is executing. These will be cleaned up for you.

Where you can create a small memory leak is if you do this:

local enemy

local function spawnEnemy()
enemy = display.newImageRect( “badguy.com”, 64, 64 )
return enemy
end

function scene:createScene( event )

local group = scene.view
local background = display.newRect( 0, 0, display.contentWidth, display.contentHeight )
group:insert( background )
local doSomething = display.newImageRect( “button.png”, 64, 32 )
group:insert( doSomething )

for i = 1, 100 do
group:insert( spawnEnemy() )
end
end

Similar to the first example, the enemies will all be cleaned up when Storyboard destroys the scene, however the “enemy” variable will stick around until the scene is “un-required.”

The table returned by spawnEnemy() is cleaned up because the group retains a reference to it as a remnant copy of the last enemy allocated. Of course if you re-enter the scene, this block will continue to persist, it probably should get nil‘led out, but its not that much memory and it’s not going to grow.

use a classe with function to create the objects? how i put many stuffs on “createScene”?

sorry

This is my game scene lua File. where i´m wrong? the return values of memory is: MEMORY = 592.017 KB    TEXTURE = 39.66796875

I managed to remove much of the texture memory :slight_smile:

http://snk.to/f-c7p3g3ic