Acceptable Memory Usage?

Doing a little optimization and am wondering what level of memory usage is considered “acceptable”.

Is there a some kind of guideline to refer to, to prevent tapping out a specific device?

I suppose I’m looking for a chart that states something like:

Max memory allowed by an app by device:
iPhone: LUA: 2MB / Texture: 12MB
iPad: LUA: 4MB / Texture: 32MB
Android: LUA: 3MB / Texture: 32MB
Nook: LUA: 2MB / Texture: 8MB

… and how does this match up with Lua’s feature of reporting memory usage using:

collectgarbage()  
print( "MemUsage: " .. collectgarbage("count") .. " KB" )  
print( "textureMemoryUsed: " .. (system.getInfo( "textureMemoryUsed" )/1000000) .. " MB" )  

For example, during run-time, my app is currently leveling off at:

MemUsage: 771.3876953125 KB  
textureMemoryUsed: 10.807296 MB  

So how do these numbers correspond to maximum levels allowed / tolerated by devices?
[import]uid: 616 topic_id: 20090 reply_id: 320090[/import]

There isn’t a chart like this that I am aware of, sorry.

The main issue I’ve seen people encounter is on Android, where texture memory limits seem to vary a lot - and on the iPhone3GS where you want to keep texture memory below 25MB.

In my (limited) experience it seems like if you can keep texture memory usage under about 20MB you are unlikely to encounter any issues.

Peach :slight_smile: [import]uid: 52491 topic_id: 20090 reply_id: 78474[/import]

Sorry @mrgoose to be posting into your topic, but I do have the same question as you and so I would like to ask Mrs. Peach how about the LUA Memory?

Does exist somewhere any table telling us more about this subject?
Thanks in advance,
Cheers,
Rodrigo. [import]uid: 89165 topic_id: 20090 reply_id: 78476[/import]

Can not say if true.
“Actually, once you start using over 10mb the iphone starts closing apps and once you hit much over 12mb the OS will close your app.”
http://www.iphonedevsdk.com/forum/iphone-sdk-development/1372-memory-usage.html#post9038 [import]uid: 79135 topic_id: 20090 reply_id: 78479[/import]

My game is working perfectly fine (iPhone4) when memory usage is about max 500.234234 but when memory usage is about 550 and higher the game slows down ???
Texture Memory is about plus minus 10-14 MB
Is it normal ?

but maybe is TextureMemory problem ?? [import]uid: 13156 topic_id: 20090 reply_id: 78484[/import]

Thank you @anddrewscott. I appreciate the info shared. [import]uid: 89165 topic_id: 20090 reply_id: 78582[/import]

Andrew’s link is very useful :slight_smile:

Pitor, that texture memory is fine on an iPhone 4, although if you have a lot else running it might make a difference.

Peach :slight_smile: [import]uid: 52491 topic_id: 20090 reply_id: 78635[/import]

Hi
Most of you probably know that , but in my case the problem wasn’t with system memory or texture memory but with the code which measure this values.
In my game a lot is going on , a lot of objects are removing and creating all the time.
I was using this code

local lastCheck = {sysMem = 0, textMem = 0}  
Runtime:addEventListener('enterFrame', function()  
 -- watch for leaks  
 collectgarbage()  
  
 local sysMem = collectgarbage("count") \* 0.001  
 local textMem = system.getInfo( "textureMemoryUsed" )\*0.000001  
  
 if lastCheck.sysMem ~= sysMem or lastCheck.textMem ~= textMem then  
 lastCheck.sysMem = sysMem  
 lastCheck.textMem = textMem  
  
 print ("mem: " .. math.floor(sysMem\*1000)\*0.001 .. "MB \t " .. math.floor(textMem\*1000)\*0.001 .. "MB")  
 end  
  
end)  

When I remove it no slows down on Iphone 4
On the other hand maybe I am close to end of cpu power, because on Iphone 4s no slows even with this code working. [import]uid: 13156 topic_id: 20090 reply_id: 78681[/import]

That’s a good point Piotr - all debugging code should be removed for production/beta test builds unless still required.

Peach :slight_smile: [import]uid: 52491 topic_id: 20090 reply_id: 78751[/import]

A table like this would be extremely helpful for iOS and Android devices and it would be great if Ansca could compile such a list (as it’s unlikely that an individual developer would have access to enough devices).

I have been developing an app that is quite demanding on texture memory. It is at 2MB memory and 47MB texture memory running on a 3GS without any problems. I did try to stress the 3GS a couple of weeks ago by going higher with texture memory and it still ran ok. Unfortunately I don’t remember what level of texture memory I went up to since my main aim at the time was just to check that I had enough “headroom” and wasn’t at the borderline of crashing the 3GS.

On an iPad 1, I’m at 2MB memory and 73MB texture memory. Again performance is fine and I did go higher just to check that I wasn’t at a borderline condition but can’t remember what I went up to. [import]uid: 29384 topic_id: 20090 reply_id: 78959[/import]

Nice info @ec2. [import]uid: 89165 topic_id: 20090 reply_id: 78964[/import]

Thanks ec2, looks like you’ve set the bar for iphone 3gs and ipad 1.

So far our “acceptable” table looks like:

 LUA Memory Texture Memory  
iPhone 3gs 2 MB 47 MB  
iPad 1 2 MB 74 MB  
...  

If it’s not to much to ask to re-run those stress tests, then we can fill out the table with a “becomes unstable at” column.

Of course, I think a lot of this info would be considered subjective, since the type/size/alpha of graphics, style of coding, background apps, and how intensive the corona/lua “behind the scenes” stuff is called / loaded / running.

But at least we’ll have some kind of a guideline to reference to see if we’re in the ball park.
[import]uid: 616 topic_id: 20090 reply_id: 79078[/import]

Very gOOd information is going here guys. As more we post here about the “memory” subject as more we can get it “crushed”. :slight_smile:

BTW we have to take care and not forget that after ALL the Hardware, Software, System, etc factors, I believe we still must consider the own USER of the iDevice and so the app! Yes, that`s really hard part to tell about BUT imagine someone trying to run any app have gotten the iDevice taskbar bloated of other opened apps?!? WOW, it would make “our” app suffer because as we know: as more running apps (on background or not) it is “system” consuming as well IMHO.

So these info shared here are really good by “parameters” but not even very close to the “real situation”. :\

Cheers,
Rodrigo. [import]uid: 89165 topic_id: 20090 reply_id: 79103[/import]

Just like to chime in regarding above post that stated memory use of over 12mb would close your app.

This may or may not have been the case in the early days, but on 3gs and above (which is now the
Standard) this isnt the case.

I had a game using 120mb of memory with no issues, your milage may vary though. Anything under 100mb should be fine. You could probably get away with more too. [import]uid: 84637 topic_id: 20090 reply_id: 79137[/import]

If i use this function to monitor mem usage, what should i aim for as a highest point of memory?
[lua]local monitorMem = function()

collectgarbage(“collect”)
print( "\nMemUsage: " … collectgarbage(“count”) )
local textMem = system.getInfo( “textureMemoryUsed” ) / 1000000
print( "TexMem: " … textMem )

end[/lua]

i dont know in what values its printed, in kilobytes maybe? [import]uid: 16142 topic_id: 20090 reply_id: 79146[/import]

Hey Alexey,

That would print in MB.

Peach :slight_smile: [import]uid: 52491 topic_id: 20090 reply_id: 79196[/import]

thanks Peach) [import]uid: 16142 topic_id: 20090 reply_id: 79214[/import]

mrgoose,

Yes I’ll do some more stress tests, probably at the weekend.

As you say, depending on the app, the limit of texture memory might vary slightly depending on what’s happening elsewhere in the app. I’m handling 256x256 tiles as part of an app that displays maps and this requires creating new tiles and deleting old tiles as the user pans around the map.

I’ll also try to do some tests on a Samsung Galaxy Tab 8.9" running Android 3.1. [import]uid: 29384 topic_id: 20090 reply_id: 79249[/import]

As a bottom level base-line, looks like Corona is consuming about 80 KB just to get up and running.

Using the following in a blank folder with just a main.lua:

function checkMem()  
 print( collectgarbage("count") )  
end  
Runtime:addEventListener("enterFrame", checkMem)  

… first line is ~ 80 KB, then climbs to a peak of 160 before the garbage collections kicks in and starts re-couping memory.
e.g.
83.0234375
83.1552734375
83.2900390625
83.4248046875
83.5595703125
83.6943359375
83.8291015625

159.9375
160.0673828125
160.203125
160.3349609375
160.470703125
158.4052734375
158.541015625
158.67578125

The GC continues to do it’s job and mem usage climbs down to about 90, then creeps back up to the 160 or more.

I suppose a timing mechanism (or when an array reach a cap or something like that) is part of the GC flow.

As I’m writing this post, I’m running the following:

local peak = 0  
local ave = 0  
local current = 0  
local all = 0  
local count = 1  
  
local round = math.round  
function checkMem()  
 --collectgarbage()  
 current = collectgarbage("count")  
  
 if current \> peak then peak = current end  
  
 all = all + current  
 ave = all / count  
 count = count + 1  
  
 print ("-------------")  
 print ("p: " .. round(peak))  
 print ("a: " .. round(ave))  
 print ("c: " .. round(current))  
  
end  
Runtime:addEventListener("enterFrame", checkMem)  

… and it looks like it’s reporting:

-------------  
p: 181  
a: 136  
c: 113  

So, the bottom line is the Corona seems to consume an average of about 130 KB of memory as a “base level”, with a peak of about 180 KB.

So perhaps as a rule of thumb, we can safely say the “base level average” for a bare minimum app is around 150 KB (keeping a little cushion and making it easier to remember).

NOTE: The code above has the collectgarbage() function commented out. This enables automatic GC to occur. If the function is not commented out, as in:

function checkMem()  
 collectgarbage()  
 current = collectgarbage("count")  
...  

Then i’m getting the following:

-------------  
p: 77  
a: 77  
c: 77  

… Which indicates that the baseline memory usage for Corona is actually right around 80 KB. (Similar to the startup usage as noted at the top of this post.

So the scripts available in this post are kind of “real world” numbers, as rarely will a developer manually cause GC.

Perhaps the Corona moderators can weigh in on this particular matter?

[import]uid: 616 topic_id: 20090 reply_id: 79302[/import]

Hey @mrgoose : Thumbs-Up! Excellent! Thank you.
Cheers,
Rodrigo. [import]uid: 89165 topic_id: 20090 reply_id: 79346[/import]