Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

"Received memory warning" question
Started by joe528 Oct 04 2013 05:32 AM

- - - - -
9 replies to this topic
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

joe528

[GLOBAL: userInfoPane.html]
joe528
  • Contributor

  • 800 posts
  • Corona SDK

I saw the console log of my app on an iOS device, it says

 

"Received memory warning"

 

I have some questions about this warning:

 

(1) In what condition would this happen?

 

(2) When it happens, would a scene get destroyed automatically? (by iOS or by Corona?)

I ask because I saw one of my exited scene gets destroyed after "Received memory warning". I have checked the code, I don't think my code purge the scene at that point.

 

(3) My app doesn't use a lot of texture memory at any point. I have been tracking the texture memory used, it's in the range of 80K ~ 100K. How could this memory warning happen?

 

 

Please help. Thanks.



[TOPIC: post.html]
#2

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,393 posts
  • Enterprise

1. When your device's memory gets low.  The exact threshold I'm not sure.  

2. If you are using storyboard and you are not purging/removing old scenes  yourself, storyboard will purge the oldest scene to free up memory.  If another low memory event comes it, it will continue to purge scenes least used first.  This is built into storyboard.  Nothing you have to do for it.

3  80K - 100K of texture memory is incredibly low.  A single 128x128 image is going to take 65K of memory.  A single 256x256 image is 264K.   Either you are reporting your memory incorrectly and that's really 80-100M of memory (which seems more realistic since a single background image on a 640x960 device is 4M) or you are reporting your Lua memory which is normally in the 80-100K range.   Can you show the code were you are determining your memory usage?

 

Thanks

Rob



[TOPIC: post.html]
#3

joe528

[GLOBAL: userInfoPane.html]
joe528
  • Contributor

  • 800 posts
  • Corona SDK

I guess I was wrong in saying 100K used in texture memory. The code is here

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( 3000, checkMemory, 0 )

It's 1024x1024, so I am using 80M ~ 100M of texture memory. Is this considered high? 

 

How could I receive the memory warning? It had happened in my iPad and iPhone. Is it normal?

 

My app uses a lot of graphics and then removes them. Maybe I should call collectgarbage() in my code proactively? 

 

Or this warning is not harmful anyway that I can safely ignore?



[TOPIC: post.html]
#4

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,393 posts
  • Enterprise

It is not a warning you can safely ignore.  If your app does not release memory, the operating system will likely kill it within a few seconds. 

 

80-100M of memory shouldn't be a problem on most modern devices.  It's too much on an iPhone 3Gs and an iPad 1 and it's pushing the limits on an iPad 2/iPhone 4.   Memory isn't just your app.  The OS is using it, and now with iOS 7 doing more background work, other apps are consuming memory periodically that wasn't happening before.  With iOS 6 and earlier you really only had to contend with mail starting up and fetching new messages periodically and other "Apple controlled" devices.   Now if you have background updates on (and you have to assume your customers will), then that facebook app updates periodically and so on. 

 

Storyboard has a listener for the low memory event that it uses to free up memory.  You can also have your own low memory handler:

 

local function memoryWarning(event)
    --print_r(event)
    print("************************************")
    print("")
    print("* lowMemory warning - texture memory in use: " .. system.getInfo("textureMemoryUsed") .. " lua memory: " .. collectgarbage("count"))
    print("")
    print("************************************")
    --scr.messageText.text = "LOW MEMORY"
    return true
end

Runtime:addEventListener("memoryWarning", memoryWarning)

 

Now the above example doesn't do anything other than print some info, but you could collect garbage at that point.

 

If you're using Storyboard to manage your graphics and you have good memory leak hygiene going on, then you shouldn't have to deal with your own garbage collection.   But if your spawning a lot of items, it might not hurt to call the GC more frequently.



[TOPIC: post.html]
#5

joe528

[GLOBAL: userInfoPane.html]
joe528
  • Contributor

  • 800 posts
  • Corona SDK

thanks for the useful info and help.
 
I added the function and sometimes I did receive the memory warning and it's printed like this:
 
************************************
* lowMemory warning - texture memory in use: 119532032 lua memory: 1966.625
************************************
 
I didn't know the freedom to use memory is so tight for iOS. Since my app has to utilize about 25 large pictures at the same time, is there a way to reduce the texture memory used without removing these display objects that hold the pictures? 
 
I saw some business apps display graphics in a different way (e.g. a low resolution pic turns to high resolution one), I don't know how they do it but I guess they try not to load high-resolution pictures until the user sees them.


[TOPIC: post.html]
#6

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,393 posts
  • Enterprise

Well you are using around 119M of texture memory which is getting on the high side for the 4, 4s, iPad2 and mini.  They have a max memory of 512m and a good portion of that is tied up by the OS.   The iPhone 3Gs, iPad 1 only have 256M of memory and well taking over half of it's a problem.  I think the iPhone 5 family and the iPad 3/4 are at 1GB of memory so the 119M should be comfortable on those devices with some room to grow.

 

Keep in mind how texture memory works.  You take your image size and round each side up to the next power of two.  A 640x1135 image (full size iPhone 5 image) is really 1024x2048x4 bytes of memory (8mb), but if that same image was downsampled a little to 512 x 909, then it would be a 512 x 1024 x 4 or a 2mb texture and the image can resample up with a little loss of quality (but Corona and iOS do a really good job of this). 

 

Also make sure you're using dynamic image sizes.   Provide a regular sized image that's pre-scaled for small screens say 320x568 keeping with the image sizes here, then provide and @2x image at 640x1136 and an @4x image at 1280x2272.  The Retina iPads will use the big image (and they have more memory), the Retina iPhones and lower memory iPads will use the mid-sized image and the older phones the low size.    Combine this technqiue with keeping your images at the smaller round-up memory sizes and you can conserve a lot of memory.

 

Finally, if you don't need it in memory, don't load it until you need it and then get rid of things don't need.   For instance if you were building a photo album type app.  Pre-build your square thumbnails and don't use a full size image for them.  Then you only need three pics in memory at once.  The one that's on the screen and the one that can be swiped in either direction.  Once you swipe, dispose of the one furthest away, load in the next one in the swipe direction.

 

Watch your transparencies too.  I had an app I was building for a customer and they sent me an animation sequence that was full screen 1024x768.  But the actual action was happening in a much smaller space, the artist was trying to make things easier with the full screen images, I was able to trim all the non-important transparency and get that animation down to something reasonable.  It went from over 100MB to 26MB. 

 

Without knowing more about what you're trying to do, it would be hard to provide any more specific guidance.



[TOPIC: post.html]
#7

joe528

[GLOBAL: userInfoPane.html]
joe528
  • Contributor

  • 800 posts
  • Corona SDK

Thanks for the detailed info and explanations. It's very helpful.

 

Yes I can understand about using dynamic image sizes. I do use this for all my icons and buttons. But for most part of the images I display, they are downloaded from the network, therefore I can't use this technique to reduce my texture memory usage.

 

I have a storyboard that displays 20 images at once (via Scrollview). It's the place that uses the most memory. And I don't really want to purge this storyboard because the user comes back often.



[TOPIC: post.html]
#8

joe528

[GLOBAL: userInfoPane.html]
joe528
  • Contributor

  • 800 posts
  • Corona SDK

I recently still saw "Received memory warning" for my iPhone 4S with 41mb texture memory used... and the phone showed a white apple. But it's kind of weird, I thought the phone was going to restart, but it took just 5 seconds and I could see the desktop again.
Oct 20 12:04:54  MyAppDev[15987] <Warning>: Received memory warning.
Oct 20 12:04:54  MyAppDev[15987] <Warning>: * lowMemory warning - texture memory in use: 41929216 lua memory: 1740.6044921875

 

any thoughts?



[TOPIC: post.html]
#9

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,393 posts
  • Enterprise

Are you on iOS 7 or iOS 6?

 

iOS 7 changes how apps multi-task and other apps seem to take up more memory now.  Its very possible that you could get low memory warnings even with a light weight app.  The system generates that message and all apps will get it.  If you are running Storyboard, then it will purge unused scenes when it gets a low memory warning for you.  But you can take that as an opportunity to clean up anything you can.



[TOPIC: post.html]
#10

joe528

[GLOBAL: userInfoPane.html]
joe528
  • Contributor

  • 800 posts
  • Corona SDK

Yes, I am running on iOS 7

 

Thanks for the information. It's very helpful.




[topic_controls]
[/topic_controls]