Trouble with memory optimization

Hi! I’m working in a game with asteroids.

I have a problem with the memory usage: My game spawn around 50 asteroids at once. Then, when I “Restart” the game, I delete the asteroids by:

aux = 0

for i = 1, cuentaasteroides do

aux = aux +1

display.remove(asteroide[aux])

asteroide[aux] = nil

end

But when appear other 50 asteroids (in the “restarted” game), other 70 kb of memory are used, and so on. If I change 50 asteroids to 500, that extra is of 700 kb. That’s why I think that the trouble are the asteroids. 

If I continue the game become slow almost to collapse

Note: I check the memory using this function, that I found in the Corona Tutorials:

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

darmemoria =timer.performWithDelay( 1000, checkMemory, 0 )

Can someone help me? Thanks in advance!

try this.

for i = 1, cuentaasteroides do

     asteroide[i]:removeSelf()

     asteroide[i] = nil

end

Thanks, but the same problem occurs .
Any other ideas?

The easiest way to find out what is sticking around is to create a dummy lua file that is nothing but an empty scene then instead of doing your restart load the lua file, if your textureMemory drops to zero then you know you have unloaded everything correctly.

Alot of memory and textureMemory issues are because of using “global” variables or for example loading up an imagesheet and not disposing when you exit etc.

It is hard to say what your exact problem is with the little for loop, but the for loop is removing the asteroide from memory so I doubt that is where your problem is.

Hi @facuala,

Add the indicated line directly after your removal loop, and tell me the result:

[lua]

for i = 1, #asteroide do

   display.remove(asteroide[i])

   asteroide[i] = nil

end

–ADD THIS:

print( #asteroide )

[/lua]

If this number is not 0, then you need to loop backwards through the table. This is a loop-indexing thing with Lua, such that, if you loop forward while also nil-ing out the index count on each iteration, you’ll only remove every other item in the table. I could explain this in more detail if you want, but first, just see what the added code line reveals.

Take care,

Brent

Thanks for your answers!

I found that the trouble with the memory was the transition of the asteroids (they have a transition to rotate), then I create asteroide[i].transicion = transition to solve that. Then I remove the transition and after the asteroid.

The bad side, is that the game is still becoming slower when I restart him around 20 times. 

But now, MEMORY = 431.394 KB      TEXTURE = 58.93…

(MEMORY increases only a little and TEXTURE remains the same)

Any Idea? Thanks!

You still have a memory leak somewhere, unfortunately.

What was the result of that print() statement I suggested? Was it 0?

It is a variable that counts the quantity of asteroids. I made that you said, and it shows “74” (that is the correct quantity of asteroids)
The code is:

aux = 0
for i = 1, cuentaasteroides do

aux = aux + 1
if asteroide[aux] ~= nil then
transition.cancel( asteroide[aux].transicion)
display.remove(asteroide[aux])
asteroide[aux] = nil
end
end
print(cuentaasteroides)

Is it correct?

Hi @facuala,

Well, it’s easier if you just check the count of asteroids in the table, instead of using so many variables (these aren’t really necessary here). So, loop from 1 to the number of objects inside the “asteroide” table as I showed in my code a few responses back. Then, tell me what the number of the “print()” statement is.

Brent

Ok, thanks! Now I understood :slight_smile:
I’ll do that tomorrow, because it’s too late and I have things to do.
In 8 or 9 hours I’ll post the result.

Anyway (maybe it doesn’t mind), I never know the exact number of asteroids, it depends of a random that decides if in a line appear 1 or 2 asteroids at the same time.
A lot of thanks!

The result of print(#asteroide) is 0. :confused:   

Hi @facuala,

Unfortunately, it seems like you have a memory leak somewhere else. I would like to help you further, but finding memory leaks can only successfully be done on your side, by carefully examining every aspect of your code, and following along, tracking variables and the number of items in tables, etc. We all struggle with it sometimes, and it requires persistence to locate the source of the issue.

Best of luck,

Brent

I couldn’t work in that today. But testing the game I discovered that after 20,or 30 restarts the colours become worst (some lines disappear, and the game is more dark).

Another thing that misleads me is that before I found the error of the transitions, game becomes slow when Memory Usage reaches 2500 kb.

But now, memory usage increases only 1 kb (average) per restart, and become slower at 520kb (after 20 restarts, like before).

And also appeared the problem that detract colors, I think that the two are related.

Thanks for your support!

Resolved: apparently I’m not very smart .

It was true that there were problems with memory, and thanks to this conflict, I solved them.
The low framerate was generated by a practically invisible background that I put with a listener to avoid conflicts with multitouch.

I forgot to remove it and that’s why the colors became opaque.

Thanks for your support!

try this.

for i = 1, cuentaasteroides do

     asteroide[i]:removeSelf()

     asteroide[i] = nil

end

Thanks, but the same problem occurs .
Any other ideas?

The easiest way to find out what is sticking around is to create a dummy lua file that is nothing but an empty scene then instead of doing your restart load the lua file, if your textureMemory drops to zero then you know you have unloaded everything correctly.

Alot of memory and textureMemory issues are because of using “global” variables or for example loading up an imagesheet and not disposing when you exit etc.

It is hard to say what your exact problem is with the little for loop, but the for loop is removing the asteroide from memory so I doubt that is where your problem is.

Hi @facuala,

Add the indicated line directly after your removal loop, and tell me the result:

[lua]

for i = 1, #asteroide do

   display.remove(asteroide[i])

   asteroide[i] = nil

end

–ADD THIS:

print( #asteroide )

[/lua]

If this number is not 0, then you need to loop backwards through the table. This is a loop-indexing thing with Lua, such that, if you loop forward while also nil-ing out the index count on each iteration, you’ll only remove every other item in the table. I could explain this in more detail if you want, but first, just see what the added code line reveals.

Take care,

Brent

Thanks for your answers!

I found that the trouble with the memory was the transition of the asteroids (they have a transition to rotate), then I create asteroide[i].transicion = transition to solve that. Then I remove the transition and after the asteroid.

The bad side, is that the game is still becoming slower when I restart him around 20 times. 

But now, MEMORY = 431.394 KB      TEXTURE = 58.93…

(MEMORY increases only a little and TEXTURE remains the same)

Any Idea? Thanks!

You still have a memory leak somewhere, unfortunately.

What was the result of that print() statement I suggested? Was it 0?