In Xcode 5, there's a new debug panel that shows the CPU and memory consumption in % and MB respectively. How do we make use of this? Is there a CPU % threshold I should try to stay below? I sometimes see my apps goes to 100% or over.. does that mean I am doing too much processing in my app and should try to optimize?
Any tips?
(PS. I'm developing on iOS)
A modern iPhone or iPad has 1024Mb memory.
But how much of that is available for apps is something that Apple has never divulged.
Just use as little memory as possible, and release non essential memory when the OS notifies your app about low memory.
Similarly, use as little CPU as possible, but more importantly, do not block the UI thread.
Use the profiler to find hot spots for CPU use and try to optimize those.
Related
I'm trying to debug why our SceneKit-based app is using so much memory but Xcode and Instruments / Allocations seem to have very different values for the amount of memory being used. When I look in Xcode I see something like 600 MB but when I transfer the same running session over to Instruments / Allocations, I see a very different number for persistent bytes, like 150 MB.
Which one is correct? Why the difference? Are they measuring different things?
(Regardless of whether I Transfer an Xcode debug session or start fresh in Instruments, it doesn't seem to make much difference.)
The reason that I care is that iOS is killing the app for excessive memory use (according to Xcode) but I can't seem to find the problem via Instruments.
I've tried turning off all GPU and Metal debug options but they don't seem to make a difference.
Which one is correct?
My intuition is: Instruments. It uses Dtrace to (sorry) instrument your code and watch actual allocations and deallocations as they happen, at the expense of performance. The Xcode debug navigator memory graph is more of an outside view designed to give a very general sense of what’s happening. That is exactly why the latter offers you a way to switch to the former — because that (Instruments) is where you’re going to get real measurements.
(However, let’s keep in mind that Instruments may fail to include in the total you’re seeing some virtual memory backing stores for graphics. There are plenty of WWDC videos discussing this topic in more detail. )
I know that this answer is quite late, but for the sake of future developers with the same problem, I would advise you to check the images in your assets folder. If any of your images have dimensions larger than 1000 x 1000 you should scale them down. With the example above, the image comprises of 1000000 pixels. Following how images are loading in (4 bytes per pixel), this means 4 MB of memory is used to load the image. Unbeknownst to me, I had an image of roughly 3600 * 4000 in my assets folder. Doing the maths, this was over 50 MB of memory usage!
I am trying to decide if I should preload all of my textures on a loading screen in my game, but I don’t know how much memory I can use for this. I looked around the web and I found where someone said that you can preload all of your textures as long as it is 80MB or under. If this is correct does that mean 80MB on all iOS devices (iPhone 3gs and up)?
Only the system knows
Ultimately, this question is all about memory. Apple doesn't care what you are doing with the memory, they just care how much you are using.
There is no 'hard set' limit on how much memory you can use on device X and up. The system (iOS) decides that.
If you are using too much, the system will send you a memory warning. If your memory usage grows, the system will begin to kill background tasks - like music, etc.
If you continue using too much, it will kill your app.
This differs between devices. For example, the 3GS has 256 MB of RAM, the 4 and above have 512 MB, and future devices may have more. Adjust accordingly.
So, test your app, watch for memory warnings, and optimize memory usage!
I am having some trouble around memory leaks in my app.
I just wanted to know that whether it is possible to identify how much memory allocated by my app on heap so that i can reduce my resources accordingly.
I know that system gives low memory warnings & i can clean up my data there but even if sometimes it wont happen.
If i get to know that my app is reaching maximum memory & remaining size on heap so i could be better to reduce my resources.
Thanks in advance....
Use Instruments. Command-I in XCode to profile your app in Instruments.
Yes of course, I would recommend the instrument's tool "Leaks". It lets you know how much memory allocates your app, and if there are leaks it tells you what they are, when and were it was allocated (and obviously not released).
Here is a nice guide I used some time ago.
http://www.cimgf.com/2008/04/02/cocoa-tutorial-fixing-memory-leaks-with-instruments/
What coding tricks, compilation flags, software-architecture considerations can be applied in order to keep powerconsumption low in an AIR for iOS application (or to reduce powerconsumption in an existing application that burns too much battery)?
One of the biggest things you can do is adjust the framerate based off of the app state.
You can do this by adding handlers inside your App.mxml
<s:ViewNavigatorApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
activate="activate()" deactivate="close()" />
Inside your activate and close methods
//activate
FlexGlobals.topLevelApplication.frameRate = 24;
//deactivate
FlexGlobals.topLevelApplication.frameRate = 2;
You can also play around with this number depending on what your app is currently doing. If you're just displaying text try lowering your fps. This should give you the most bang for your buck power savings.
Generally, high power consumption can be the result of:
intensive network usage
no sleep mode for display while app idle
un-needed location services usage
continously high usage of cpu
Regarding (flex/flash) AIR I would suggest that:
First you use the Flex profiler + task-manager and monitor CPU and Memory usage. Try to reduce them as much as possible. As soon as you have this low on windows/mac they will go lower (theoretically on mobile devices)
Next step would be to use a network monitor and reduce the amount and size of the network (webservice) calls. Try to identify unneeded network activity and eliminate it.
Try to detect any idle state of the app (possible in flex, not sure about flash) and maybe put the whole app in an idle mode (if you have fireworks animation running then just call stop())
Also I am not sure about it, but will reduce for sure cpu and use more gpu: by using Stage3D (now available with air 3.2 also for mobile) when you do complex anymations. It may reduce execution time since HW accel is there, therefore power consumption may be lower.
If I am wrong about something please comment/downvote (as you like) but this is my personal impression.
Update 1
As prompted in the comments, there is not a 100% link between cpu usage on a desktop and on a mobile device, but "theoreticaly" on the low level we should have at least the same cpu usage trend.
My tips:
Profile your App in a first step with the profiler from the Flash Builder
If you have a Mac, profile your app with Instruments from XCode
And important:
behaviors of Simulator, IPA-Interpreter packages and IPA-Test builds are different.
Simulator - pro forma optimizations
IPA-Interpreter - Get a feeling of performance
IPA-Test - "real" performance behavior
And finally test the AppStore-Build, it is the fastest (in meaning of performance) package mode.
Additional we saw, that all this modes can vary.
I am running an ipad application compiled for release and am seing memory warnings once in a while.
When I run the app on the device and connect Instruments, I see that the app never passes 40MB of real memory, but the warnings are still occurring.
What might be causing this? How can I better track down the reason?
40 MB of real memory is a lot, for an iPad. Even if it was not, the system will deliver the low-memory warning to you from time to time anyway, without your application being the main culprit. Tracking down precise memory usage in your application is sometimes hard, I’d suggest to spend some time with the Object Allocation instrument while working with the app. If you are not getting killed and you are sure that you do not leak the memory, you can also simply ignore the warnings.
40MB is high for the iPad considering it only has 256MB to start with. There could be other applications holding on to memory which will be killed off as more memory is needed. Just make sure you aren't leaking anything. Also use NSAutoReleasePools where applicable to reduce peak memory usage in memory intensive loops.