Profiling memory leaks with Instruments- huge difference between iPhone 4 and iOS 5 Simulator - iphone

When profiling my app with Instruments (looking for memory leaks), I get extremely different results with the iOS 5 iPhone Simulator from those I get with my iPhone 4 running iOS 5. The first picture shows the results from the profiling with the real device, and the second is with the simulator:
Real device:
iOS 5 Simulator:
This profile is taken up to the same point in the app in both cases: completion of viewDidLoad in the rootViewController's view lifecycle. I have waited in both of them for the total allocated memory to stable out. As you can see in the device graph, there are some extreme fluctuations occurring at about 00:10, which aren't present in the Simulator. On the real device, total allocated memory, at around 00:08, jumps from 1MB to 3.5 MB then back down to 1.5 MB and finally jumps to 4.74, where it stabilizes. The allocated memory for the Simulator is much more linear, with it climbing steady and quickly to around 2.35 MB where it stabilizes.
Another thing to note is the presence of 2.25 MB of allocated memory present on the device but not the Simulator from malloc and 700+ KB from CFNumber. As I am relatively new to using Instruments and profiling, I'm not exactly sure if this is normal. A quick Google search turned up nothing definitive. That 2.25 MB and 700 KB more than make up for the difference in memory allocation. To balance things, there are more entries for malloc with different amounts of memory present in the Simulator test not present in the device test.
Also, I found that when a second UIViewController is pushed onto the UINavigationController stack, allocated memory jumps to about 8.5-9 MB on the real device, but only about 4.5 to maybe 4.5 megabytes tops on the Simulator.
I know it is to be expected that the device would perform much differently from the Simulator, but should memory allocation not be pretty similar because the same code is being run on both devices? I would understand if this is a performance profiling, but for memory allocation, it seems that the numbers should be pretty similar. Can anyone shed some light as to whether this is normal or not?

This behavior is to be expected. Technically, when you run profiling with the simulator you are measuring stats based on your desktop's hardware. Even if you're just profiling allocations you can't expect them to work similarly because a lot of software optimizations/algorithms/etc are based on the hardware it's running on.
Unfortunately, Apple doesn't have an iOS emulator. You're better of profiling with the device though, as emulators tend to still be unreliable and slow (e.g. Android emulator).

You should always run leaks on an iOS device and never on the simulator. The results you get from the simulator will only serve as a distraction since they are rarely 100% accurate. You'll find yourself chasing down a lot of red herrings! hehehe

I know it is to be expected that the device would perform much
differently from the Simulator, but should memory allocation not be
pretty similar because the same code is being run on both devices? I
would understand if this is a performance profiling, but for memory
allocation, it seems that the numbers should be pretty similar. Can
anyone shed some light as to whether this is normal or not?
Technically the code is completely different. The simulator application compiles for x86 bytecode, while the device compiles for armv6/armv7.

Related

iPhone is faster than Instrument tool's memory leaking check tool speed?

Currently i don't have an apple developer account ($99).
I am developing iOS app with searching function in iOS Simulator.
In simulator my app's search speed is not bad, just normal.
But when i check my app for memory leaking with Instrument tool, my app's search speed is too slow and data load speed are also too slow.
I have to load 30 MB data in Start of app.In Simulator it's fast.But in Instruments , it's too slow and also took 6 seconds.
So i am worring about my apps to run on physical devices because of speed.
I would like to know , Is the iPhone physical device faster than Instrument tool's check memory leaking?
In my experience, normal execution on iPhone is much faster than profiling with Instruments. But, that heavily depends on the application and what "instrument" are you using. If your application does a lot of memory allocations, it will be very slow when using Instruments with "Allocations" settings.
But also be prepared, that the iPhone Simulator is usually much faster than the real device. Your desktop CPU is usually x86 and over 2 GHz, but the device's parameters are lower. And also the architecture (ARM) makes the code to be compiled into more instructions than on x86, which also makes it a little bit slower.
Anyway, you really should just go ahead, buy the developer account and test your app on the device, there's no other way to predict the performance. I guess the rule in your situation should be don't guess, measure.
Please dont guess anything test on real device go an buy $99 developer account. iPhone Simulator is usually much faster than the real device. Its a very bad idea to download 30 MB of data at the start. If connection is slow it will take more than 5 min. If your data on device goes more tnan 45 MB apple will kill your applicationa nd free the data. Make sure you are doing in write way.

iPhone Memory Usage Optimization / Performance Tuning

I am in the last phase of my iphone game development: optimization and performance tuning. My problem is, the game runs quite smooth on iphone 4 and iPad, but it often crashes on iPhone 2 with iOS 3.1.3 due to low memory.
I have gone through all the memory leak detection / clean up processes, and the Xcode instrument shows no leak except those from system library (see following screenshot). I also use "autorelease" rarely.
(bigger picture: click here)
I also profiled my application using "CPU Sampler" and "Allocations", but is a little confused by the result. This is the result from "Allocation" benchmark:
(bigger picture: click here)
This is the result after one game. As you can see, the "Live Bytes" is only 3.93MB, which shouldn't be a big deal (according to my understanding) -- but the game often crashes at this time on iPhone 2, ios 3.1.3 .
I also did a "CPU sampler" benchmark, following is the result:
(bigger picture: click here)
What confused me is, the real memory shows "22.32MB" and the virtual memory is more than 100MB, which is dramatically different from the result of "Allocation benchmark".
I am also confused by the fact that, my iPhone 3G, running iOS 4.1, even if it has almost exactly same hardware spec with iPhone 2, can run my game very well. It's slow and not as snappy, but it rarely crashs.
So my questions are:
What else I can do to identify the low memory issue on iphone 2?
Are the leaks from system libraries on the "Leaks" profiling result a problem?
Why "CPU sampler" and "Allocation" shows different memory foot print? Did I read them correctly?
Why iPhone 3G runs a lot more smoother than iPhone 2G? Is it because the newer iOS version (4.1 vs 3.1.3)?
1. What else I can do to identify the low memory issue on iphone 2?
Run your app in iOS simulator and use Hardware menu item "Simulate Memory Warning" item to trigger Out-Of-Memory events in places, which you suspect to crash.
Instrument your app in real device, but before launching it start as many other apps as possible to reduce amount of available memory. While running your app, switch every now and then back to app grid to (re)launch other apps. This way you will make system generate genuine Out-Of-Memory events, but you can't choose which app will receive them.
You could generate OOM events by yourself, but then you would be running a different application. Might be useful, while developing, but not recommended when close to release. Very annoying to debug bugs caused by debugging "helper" routines...
2. Are the leaks from system libraries on the "Leaks" profiling result a problem?
They could be side-effects of your code. Look around which part of your app might be starting that system service and whether you release / cancel / close everything.
3. Why "CPU sampler" and "Allocation" shows different memory foot print? Did I read them correctly?
No idea, but would like to know :)
4.Why iPhone 3G runs a lot more smoother than iPhone 2G? Is it because the newer iOS version (4.1 vs 3.1.3)?
iPhone 3G has better, faster and more hardware than iPhone2. As result, apps seem to run faster... which can be a problem, if your app runs too fast in faster hardware. Making sure your app runs "just right speed" regardless of hardware is a different question.

Memory uses limit on iPhone

What is the amount of memory an app can take before getting kicked by iOS?
Does the amount of memory depends on the device version?
I have developed an app which is using 30+ mb and its getting kicked on iPhone 2g. Can it work on iPhone 4 or 3GS?
My experience with the iPhone 3G is that you should try to stay as small as humanly possible--build your data model with ditchability in mind, because you'll need to ditch. 20mb is bumping against the limit. 25 MIGHT be okay if the phone has been rebooted recently. You'll probably never get 30mb.
By contrast... I managed to prompt a memory warning on my iPhone 4 once, but it was due to an infinite loop bug that downloaded the same image file an infinite number of times. In other words, it took something REALLY drastic to crush the 4. Not that you can ignore memory management completely (a leak is still a leak), but for sure you've got some breathing room.
The 3Gs is somewhere between the two. I don't have one to test on, but I'd expect its performace is more 4-like than 3G-like, because while the on-board memory doubled from the 3G, the OS is still taking up the same space, meaning all of the new memory is yours to play with.
All your application's resources on an iPhone 2 should probably stay at less than 20 MB. You can go a little over, but that's it, otherwise the memory warnings will occur. There's only 128 MB of total physical ram for everything - that's the OS as well as your own app.

How do I see how much memory my iPhone app is using?

How much real memory should my iphone app be using? What's going too high?
Keeping an eye on -(void)applicationDidReceiveMemoryWarning:(UIApplication *)application is definitely important, but if this is game, chances are the assets (notably textures presently on screen) can't simply be deallocated when that warning is received.
If you're a bit on the high side (20 MB +) I would recommend doing a bit of testing. Using Instruments and the Object Allocation tool (Run > Run with Performance Tool > Object Allocations) you can monitor how large your memory footprint gets. Then, try running Safari and fill the pages, then a few games and whatever else you can to get the memory higher, and see how your app performs.
In my testing for a recent release, 24 MB seemed to be pretty safe, and is a number I've heard elsewhere. Once you get above 30 or so MB, chances are your users will start having rare crashes (which happens to be the case for us, verified by crash reports). The higher you go, the more crashes users will see. There's no specific limit though, for the sake of testing I've pushed my app on an iPhone 3G up to 70 MB before, it just isn't likely to work for most, nor for long.
Requesting on the iTunes page that users restart their devices can help, though there's no guarantees it'll be effective.
Also, this is all assuming devices prior to the 3GS / 3rd Gen iPod Touch. If the app merely runs on an older device, it should have no problem on the newer ones (which have twice as much ram, 256 MB).
I think available memory may depend on several factors such as device model, how long it has not been rebooted etc.
You should not rely on some fixed values but instead try to use as little memory as possible and implement -(void)applicationDidReceiveMemoryWarning:(UIApplication *)application in your application delegate and/or -didReceiveMemoryWarning in your view controllers to handle low memory warnings there and free unused memory.

Xcode Instruments: peak RAM of iPhone apps running in Simulator?

Is Activity Monitor (a.k.a. Memory Monitor) the only tool in Xcode Instruments that can measure the total app RAM usage of an iPhone app running in Simulator? Just that line showing momentary wired RAM?
And how accurate is that versus iPhone hardware, especially given OSX paging i/o to VM?
I'm seeing 7-8MB wired RAM figures for just the default Xcode iPhone project templates (other than OpenGL) compiled and running. At the same time Object Allocations shows well under 1MB for all objects.
Nope, there's a much better way to do it.
Go to the Run menu and select Run with Performance Tool then Object Allocations.
This will start Instruments and will show RAM usage. It's also useful for detecting memory leaks if you choose Leaks instead of Object Allocations.
I would agree, since the Simulator is just an "API simulator", the behavior you see is going to be at least somewhat dependent how OS X manages memory. And the restrictions are obviously different, for example you can exceed 128 MB of ram, or even 256 MB, which obviously isn't possible on any current iPhone or iPod Touch (at least, as of this writing...).
I've still found it useful in OpenGL for making sure textures and other items are properly deallocated, but beyond that, it's just a rough guideline.
However, if you want to see memory usage on device, there's a simple enough solution. Just target Device in Xcode, and go to Run > Run with Performance Tool > Object Allocations, then the build will be sent to the device and the Object Allocations tool will pop up next to Xcode and show on device memory usage.