I’ll definitely take a look at the profiler.
Thank you very much for open my mind on that issue!
[import]uid: 116181 topic_id: 34969 reply_id: 139089[/import]
Thanks for the suggestion!
Here are a few tips about finding memory leaks in general. The first thing you should do is make sure you have a memory leak in the first place. It can be difficult to tell if an increase in memory is a leak or just an innocent memory allocation. In order for it to be a leak, the memory must increase reproducibly. An easy way to think of it is to picture your app as a series of events. Naturally, there are events that should “undo” eachother in a memory standpoint. A common one is transitioning between scenes or going from the game scene to the main menu scene. The first few times the event occurs the memory will increase due to allocation but if you go back and forth a few times the memory should remain constant. If there is a leak, you should see a linear increase even after say 5 repetitions.
So now you have found a memory leak between the main menu and scene A. This is where profiler can help you the most. All you would need to do is call diffsnapshot() at some point during the transition from main menu to scene A. This means you can put it when scene A loads or when scene A unloads. Now, with mode 4 running you repeat the steps illustrated above and after a few repetitions a steady state emerges. Mode 4 will tell you the exact tables that are increasing from snapshot to snapshot so it should help you narrow down the problem.
Finding and fixing leaks is a difficult process and sometimes you need to ask yourself: How long will my program even be running? Obviously if you are making a web server you cant have even a single byte of leaking but for a game that the user plays for 20 minutes it might not be much of an issue for a small leak.
Regards,
M.Y. Developers [import]uid: 55057 topic_id: 34969 reply_id: 139094[/import]
@M.Y.developers: Magnificent clarification!!
I am very persistent and I always like to dig deep until i understand what’s going on, because i don’t want to produce a low quality / buggy app, like any of you, I guess 
PS:
I will buy the profiler =P [import]uid: 116181 topic_id: 34969 reply_id: 139107[/import]
I think the problem you are running into is that you’re not giving the garbage collector time to work. I don’t think in the real world you’re going to be allocating 1000 display objects. Each one returns a pointer to a table of information. When you call the objects removeSelf() function, the texture memory is deallocated, but the table containing the X, Y of the object remains. This is why you have to “nil” the object.
But in your test you are immediately (within microseconds) overwriting that nil and when the GC runs, the pointer is only nil for the last object. This will slow your loop down considerably put force a garbage collection immediately after you nil the object. [import]uid: 199310 topic_id: 34969 reply_id: 139108[/import]
I could be wrong - but it seems as though your texture memory (display objects) is 0 as expected. It’s your system memory that is holding onto objects, but seeing as you’ve got the monitorMem() function in use then that would be expected. [import]uid: 33275 topic_id: 34969 reply_id: 139007[/import]
Yes, the texture memory is 0 as expected. But I do not understand why the system memory increases.
I just did another test:
main.lua
[lua]local function memMonitor()
collectgarbage()
print(string.format(“Memory Usage: %.2f KB”, collectgarbage(“count”)))
local texMem = system.getInfo(“textureMemoryUsed”) * 0.000001
print(string.format(“Texture Memory: %.2f MB”, texMem))
end
local image
for i = 1, 1000 do
image = display.newImageRect(“image.png”, 32, 52)
image:removeSelf()
image = nil
end
local number
for i = 1, 10000 do
number = 5
end
timer.performWithDelay(1000, memMonitor, 0)[/lua]
Output:

As you can see system memory remains the same, which leads me to believe that the system memory leaks come from Corona. Texture memory frees it well but not the system.
Does anyone know why this happens? [import]uid: 116181 topic_id: 34969 reply_id: 139008[/import]
Sorry for repeating myself, but won’t the monitorMem() function and the main.lua module will be accounting for the vast majority of that system memory usage?
UPDATE
Just looking at the numbers again, I see system memory increasing despite monitorMem() being the same function each time.
Scrap what I said. [import]uid: 33275 topic_id: 34969 reply_id: 139011[/import]
Sure, the function is the same.
I’m starting a new project and I don’t want to continue until I solve this, because when I add classes that create DisplayObjects eats more memory and I can’t come back to the initial system memory even if i put all to nil and use removeSelf () in all DisplayObjects.
Any solution or advice? [import]uid: 116181 topic_id: 34969 reply_id: 139014[/import]
I am not sure I follow the question exactly. What memory leak? Just loading up an empty main.lua will cause memory to rise. Corona has many libraries that load by default in the background, you will never have 0 memory across the board.
Am I misunderstanding the problem? [import]uid: 56820 topic_id: 34969 reply_id: 139037[/import]
Yeah I couldn’t quite grasp what the issue is - the only time system memory is going to be 0 is when the app isn’t running… [import]uid: 33275 topic_id: 34969 reply_id: 139046[/import]
Yes, you are misunderstanding the problem. I do not expect to have 0 in system memory since that’s impossible as you mentioned.
What I am trying to prove is if I load 1 image and then remove it, the memory usage is 117.97 KB, but if I load 1000 images and remove them, the memory increases to 134.25 KB and never goes down to 117.97 KB again and i can’t remove this extra memory, then i have a huge leak.
Have I explained it correctly? [import]uid: 116181 topic_id: 34969 reply_id: 139045[/import]
Okay, the problem has not been understood as I expected, let’s put it simple:
- The application starts with 150 KB of memory usage
- 1000 DisplayObjects are loaded, memory goes up to 180 KB
- The previous 1000 DisplayObjects are removed, memory usage remains 180 KB.
(The numbers are illustrative)
Should not the memory be 150 KB as in the beginning? [import]uid: 116181 topic_id: 34969 reply_id: 139048[/import]
You are right I did misunderstand.
If you run this slightly changed code it loads the images every 5 seconds. You will see it goes up and down in memory and the numbers will vary slightly, but it never keeps climbing. You never get a “leak”. There is some residual memory from the underlying systems.
What those small tidbits of memory are using images or creating rects etc is unknown to me. It isn’t leaking though. A leak continues to climb even after garbage collecting. I just look at this as overhead.
[code]
local function memMonitor()
collectgarbage()
print(string.format(“Memory Usage: %.2f KB”, collectgarbage(“count”)))
local texMem = system.getInfo(“textureMemoryUsed”) * 0.000001
print(string.format(“Texture Memory: %.2f MB”, texMem))
end
local image
function test()
for i = 1, 1000 do
image = display.newImageRect(“image.png”, 32, 52)
image:removeSelf()
image = nil
end
end
timer.performWithDelay(5000,test,0)
timer.performWithDelay(1000, memMonitor, 0)
[/code] [import]uid: 56820 topic_id: 34969 reply_id: 139050[/import]
I don’t have an exact answer but I think the whole approach of measuring small memory footprint differences in lua is misleading…
lua is a scripting language, even in lower level frameworks like .NET or Java you will not have such control over your system memory consumption.
lua has lots of optimizations within the interpreter to make it as fast as it is. some of these include minor memory consumption but will allow your program to run smoother overtime. For example lua might allocate a bit more memory if it sees your program is a high consumer, or it might even have some optimizations to improve branch prediction of the cpu. it’s really really hard to tell with all the code “hidden” underneath the lua interpreter.
Another possible cause could be that higher level languages (java and .net included) manage their own private memory heap which implements a garbage collection, but if used very rapidly (creating/destroy objects ,especially medium to large objects) you may encounter some memory fragmentation.
Usually this is not an issue overtime, you may see small memory bumps if you stress the heap but when you reach a very large number of objects these usually stops growing. The thing is that in your case all the objects have an identical memory footprint so I’m not sure this could happen to you.
If you want to control your system memory consumption at the 10kb level lua is not suitable. You’ll need C++, or some other lower level language. If you want to measure a real memory leak go to much higher numbers, say 1,000,000, and see if the “leak” is linear, meaning for each X iterations you get the same amount of memory orphaned.
[import]uid: 80469 topic_id: 34969 reply_id: 139053[/import]
First, thanks for your answers 
@anderoth I changed your code a little bit, if you run this one (after a few seconds), you can see that once test() is called you have 2MB+ of memory usage and you can’t delete that memory and never goes down, then you have 2MB of memory wasted and you don’t have any way to delete it, there’s a memory leak definitely.
[lua]local function memMonitor()
collectgarbage()
print(string.format(“Memory Usage: %.2f KB”, collectgarbage(“count”)))
local texMem = system.getInfo(“textureMemoryUsed”) * 0.000001
print(string.format(“Texture Memory: %.2f MB”, texMem))
end
local image
function test()
– load 100000 DisplayObjects
for i = 1, 100000 do
image = display.newImageRect(“image.png”, 32, 52)
image:removeSelf()
image = nil
end
end
– Call test once
test()
timer.performWithDelay(1000, memMonitor, 0)[/lua]
@gtt I don’t want to control my memory consumption at 10KB level, but i’m afraid because this is a simle example and i have leaks, in the game i’m developing, the user can reload when he/she wants and the memory goes up each time the level is restarted and i freed all the memory correctly, i made this little test to see if i did something wrong, but the results are clear.
This code is the same that the code above but loops 1 million times and the DisplayObjects are freed 1 million times, the memory usage after that is 16MB+, i think that this is a lot of memory usage and i don’t know why this memory can’t be released despite what you said about LUA, which i already knew
[lua]local function memMonitor()
collectgarbage()
print(string.format(“Memory Usage: %.2f KB”, collectgarbage(“count”)))
local texMem = system.getInfo(“textureMemoryUsed”) * 0.000001
print(string.format(“Texture Memory: %.2f MB”, texMem))
end
local image
function test()
– load 1000000 DisplayObjects
for i = 1, 1000000 do
image = display.newImageRect(“image.png”, 32, 52)
image:removeSelf()
image = nil
end
end
– Call test once
test()
timer.performWithDelay(1000, memMonitor, 0)[/lua]
So the point is, is this normal? or am I worrying too much? [import]uid: 116181 topic_id: 34969 reply_id: 139063[/import]
I ran a few tests and this I believe it simply the internal table used for the objects you created.
Look at the code below. I changed it over to a table that inserts number. Then I nil the numbers using test2(). Running that with test3() commented out will create a table with 2MB of memory. Now un-comment test3() and it nils the table out and now you have 80k of memory. It’s the empty table consuming memory.
local function memMonitor()
collectgarbage()
print(string.format("Memory Usage: %.2f KB", collectgarbage("count")))
local texMem = system.getInfo("textureMemoryUsed") \* 0.000001
print(string.format("Texture Memory: %.2f MB", texMem))
end
local image = {} -- create empty table
function test()
-- load 100000 table entries
for i = 1, 100000 do
image[i] = 84092034902349
end
end
function test2()
-- nil 100000 table entries
for i = 1, 100000 do
image[i] = nil
end
end
function test3()
-- nil table
image = nil
end
-- Call test once
test()
test2() -- nil each entry
--test3() -- nil table
timer.performWithDelay(1000, memMonitor, 0)
So yes , if you make 100,000 images loaded ALL the time, you will have the 2MB overhead. However as you seen in earlier tests, it does not go up every time you reload a new image. It re-uses the table. I don’t think you have anything to worry about. It isn’t a leak so much as its lua and/or Corona hanging on to tables for future use. I can’t imagine a real game using 100,000 images all at the same time. Anything is possible though and even then it’s 2MB overhead. That would not be the end of the world. [import]uid: 56820 topic_id: 34969 reply_id: 139067[/import]
I run your test and it’s fine … for numbers! If you run the same test with DisplayObjects you are right too, you will have 2MB overhead, but you are wrong when you say that It re-uses the table
I made the same test that you did but once the table is nil i load the SAME images again and then removing them and nil the table, the result is that you get 4MB overhead, which proves that LUA is not reusing the table which would be fine but’s not the case.
[lua]local function memMonitor()
collectgarbage()
print(string.format(“Memory Usage: %.2f KB”, collectgarbage(“count”)))
local texMem = system.getInfo(“textureMemoryUsed”) * 0.000001
print(string.format(“Texture Memory: %.2f MB”, texMem))
end
local image = {} – create empty table
function test()
– load 100000 table entries
for i = 1, 100000 do
image[i] = display.newImageRect(“image.png”, 32, 52)
end
end
function test2()
– nil 100000 table entries
for i = 1, 100000 do
image[i]:removeSelf()
image[i] = nil
end
end
function test3()
– nil table
image = nil
end
test()
test2() – nil each entry
test3() – nil table
image = {} – empty table
test() – reload the SAME image
test2() – nil each entry
test3() – nil table
timer.performWithDelay(1000, memMonitor, 0)[/lua]
I know i’m not going to load 100000 images in my game, but that’s not the problem, as i said to @gtt, the problem is that in my game the player can reload a level several times, and i have tested that when he/she reloads the level, the memory usage increases about 50 KB (i removed all DisplayObjects and groups correctly), if you multiply 50 KB by the number of times the level is restarted you could end with the game crashing (i’ve never reached that point while debugging but it’s possible due to the low RAM of mobile devices and a long period of usage of the game).
After that, i decided to make that simple test to see if i was doing something wrong, but seeing the results, i don’t know how to prevent this problem.
Anyway, thanks a lot for your help and time! =P [import]uid: 116181 topic_id: 34969 reply_id: 139072[/import]
I am not sure why it does it with the first 2 loadings, but it doesn’t continue doing that if you keep loading again and again. If you simplify the code and just keep reloading it over and over and over you see it does stop growing after the second loading. I switched it to 100 images to make it run faster.
Running this code if goes from 86k to 130k then down to 103k. From there it never goes beyond 130k again.
[code]
local function memMonitor()
collectgarbage()
print(string.format(“Memory Usage: %.2f KB”, collectgarbage(“count”)))
local texMem = system.getInfo(“textureMemoryUsed”) * 0.000001
print(string.format(“Texture Memory: %.2f MB”, texMem))
end
local image
function test()
– load 100000 table entries
for i = 1, 1000 do
image[i] = display.newImageRect(“image.png”, 32, 52)
image[i]:removeSelf()
image[i] = nil
end
image = nil
end
function LoadUp()
print ("================================== Loading Images")
image = {} – empty table
test() – reload the SAME image
end
timer.performWithDelay(2000, LoadUp, 0)
timer.performWithDelay(1000, memMonitor, 0)
[/code] [import]uid: 56820 topic_id: 34969 reply_id: 139076[/import]
Also if your using Scene Manager it will probably have internal tables. You did not specify how your loading a level. There could be a leak in Scene Manager. I am just not seeing it with normal creation and removing of display objects.
I also do not see a memory leak in my own game when I clear the game board of 50 images and reset the board. The memory stays flat. But I am not using any modules for scene transitions. My game pieces are actually in there own display group that I just remove and Corona handles removing the images and I recreate the display group and populate it with new pieces. [import]uid: 56820 topic_id: 34969 reply_id: 139080[/import]
I ran your test and yes, after 2 loads the memory never goes up, i don’t know why, it’s strange but i’ll stop testing here and continue with the development.
Returning to the real subject, i made a class that is responsible of drawing a board on the screen, for my tests i’m not using Scene Manager, i’m testing on main.lua to make sure that the memory usage comes from my class. I don’t know why but every time i reload the board calling class:reload() the memory usage goes up 50 KB and i’m getting frustrated because i’m 100% sure the reload process is OK.
I guess i’ll have to live with these “leaks” without knowing how to solve them and never be sure if the reports of memory usage are fine, because the problem is exactly as the tests shown above.
Thanks for your time and patience
[import]uid: 116181 topic_id: 34969 reply_id: 139083[/import]