my iPhone software is so slow! - iphone

I ported one of my mac applications to the iphone. WOW!!! is it slow! At first I started thinking maybe I was doing a lot of disk access. But as I started looking I realized I simply do an offset read of a binary file. I only read in about 512 bytes of data. I also have an array that is huge. Maybe 2MB. But why would that be slow? It is perminatly located in memory.
I would love to hear some ideas from you all!

When you have major performance problems, the tool to pull out first is Instruments. Start with "Run With Performance Tool > CPU Sampler" and get a feel for where your app is spending its time. After that, check Object Allocations to see if you're hitting memory harder than you should. iPhone is a resource-constrained environment compared to the Mac. Things that you think of as fast can dramatically impact performance on iPhone. Disk access is much more expensive. Even allocating memory can be a significant impact (welcome to the world that server developers deal with every day). You only have one core, so things you stuck off on a background thread now compete with your main thread. It's a different world.

It's hard to debug the application over Stackoverflow, but there are many reasons why the iPhone app runs much slower: Your mac applications run on probably the latest CPUs with tons of memory, as for the iPhone it's very limited (3GS is rumored to have 600MHz CPU with 256MB RAM). Also, Mac applications are a bit more forgiving when it comes to memory usage; as for the iPhone it's important that only create the objects you need when you need them and release them when you no longer use them. Delaying object de-allocation results in some slowdown as well.
I recommend using Instruments performance profiling tool, that is bundled with XCode and the Developer tools. It'll give good tips on what the bottlenecks are.

Related

available RAM iPhone app

how do i increase the available memory (more concrete: the part of the RAM) that can be used by my app?
i am not referring to the space available on the harddisk of an iphone, but to the RAM.
does anyone know if there's a way to use the whole free part of the RAM for my app only?
i have to clear this up a bit, sorry if it was too vague:
i had to think of the java heap size, which can be increased by adding a parameter to the startup command of the java programming.
as i don't know, but at the moment suppose on the iphone something similar happens (every app has just xxMB RAM for execution) this value might be adjustable, so that i can use the whole remaining RAM for my app.
which pretty much is what i need for this special app (non-app-store-publication; file-sizes between 50MB and several 100MB)
How do i increase the available memory?
Short answer: you don't.
Memory is managed by the kernel.
Your application process can't control this.
You can't explicitly control this - this is managed by iOS.
You can't do that. Try reducing your memory usage instead of looking for ways to remove well needed limits.
Store your data instead and read from it when needed.
I don't know for sure if you can configure RAM allocated for your app. I think that is taken care of by the iOS kernel
It's unlikely (read: not going to work) that you will be able to allocate anything more than a few Mb in your application at once.
Not planning on publishing your app to the App Store won't change this. Apple don't officially acknowledge the amount of memory in iOS devices. But its known that devices have between 128Mb and 512Mb of physical RAM.
With the kernel, essential applications (Phone app, etc), background processes, etc, you won't have anything like that available to your application. Careful analysis in instruments would suggest that you'll generally start getting memory warnings when you've allocated around 22Mb of RAM in your application.
A change made in iOS 5 makes the watchdog process much more aggressive with killing applications after you get a memory warning. If you get a memory warning on iOS5 you have to reduce your memory usage or you will get jettisoned by the OS.
If you want to proceed, you will have to figure out how to reduce the amount of memory your datasets require. Its unlikely that all of the 100Mb file needs to be in memory at once. iOS devices have "relatively" fast CPU's and storage, you'll have to architect your application to read and write to storage in chunks and work on smaller subsets of your data.
Some related Stackoverflow questions and links:
Monitor memory usage in an iphone app?
How much memory can an iPhone app use?
10 iPhone Memory Management Tips

Can my iPad app cause the device to reboot?

I have an iPad app that has a process for downloading lots of map files (a couple of gigs of data and 10s of thousands of files).
In my most recent test release, the device will sometimes reboot during the download process, (downloads can take a couple of hours).
When the app reboots, it does not leave a crash report. We have observed this behavior on both an iPad 1 and an iPad 2 running 4.3.3.
The only thing I can think of is we increased the max concurrent threads from 4 to 20 for doing these downloads.
Completely exhausting the system memory triggers a hard reboot of the device. This used to be more common in iPhone OS 2.0, running on the limited hardware of the initial iPhones and iPod touches. In recent OS versions, Apple has more rigidly enforced the hard kill of your application when it exceeds its memory ceiling, so it's become much harder to do this. Also, the devices have much more memory than they used to.
One way that you can sometimes do this is by loading many large textures or other graphical components that may not be immediately identified as memory used by your application. I've been able to cause a system reboot when loading a pile of data onto the GPU in a tight loop. You may be encountering something similar here.
I doubt that this is related to the number of active threads you have going, although they probably make it easier for you to dump a mess of data into memory before the system can kill your application.
As an aside, rather than having piles of threads, which consume resources, have you looked at using GCD or a queue-based framework like ASIHTTPRequest? These might be more efficient for your application, yet still provide the concurrency you need.
Your app is crashing most likely due to memory management. Since you are downloading several GB's of data, maybe you are running out of disk space? I am not sure why it would take your device several hours to reboot.
Try posting some code.
Have you debugged in Instruments? This will show you if allocations are rising and filling memory. If you are attempting to load these GBs into memory, then of course your going to experience a crash.
Also, have you looked at dispatch_apply instead of threads? GCD automatically distributes and increases/reduces the threads it uses based on load. This way you don't have to manage this yourself. It might be worth a shot.

How do keep memory usage and size of application down (iPhone)?

I am currently working on a personal iPhone project that is very audio and graphic-intense and I have therefore been quite conscious of how much of a footprint my application has (in terms of both memory usage while active and/or in 'multi-tasking mode' and how much the size of application is) as Apple sub-consciously gets into my mind and has drilled in the fact the iPhone has limited memory and capacity - and I was curious if any of you guys had any solutions that could help me keep down the usage of my application - I've open up Instruments and configured some aspects although this primarily appeals to me as a performance utility). I have also been testing on my device as I cannot stand the simulator...
As long as you are properly releasing objects that aren't being used, you should be fine. If you are loading large images and audio files, then your app will require more memory to handle those, so alloc/release those as they are needed.
Also, debugging memory issues on the Simulator just wont work, so don't rely on it for testing memory issues.

How much memory can an iPhone app use?

Can anyone link me to a page that describes memory allocations for iPhone apps.
I have heard that you are limited to a sandbox of ~20 megs, depending on the state of the phone, but I can't find the source for this.
It depends on the device you're using. I've found that with an iPhone 3G, when the total memory your app is using goes over 10-14MB, it's vulnerable to crashing. When it hits 20MB, it will crash for sure.
As far as I know, Apple doesn't give any hard figures for the memory that you can use. It's always changing so it's hard to pin down. They recommend that you rely on memory warnings and respond to them appropriately.
iPhone 3GS has a much higher amount of memory available to apps (I've heard that it's around 4 times). Although the iPhone 3GS only has double the memory of the iPhone 3G, a lot of that memory is taken up by the OS. There's very little left for apps to run in. That's why the 3GS will allow apps to use so much more memory.
Also, there can be a lot of memory already in use by the OS. One example is when safari is keeping a lot of tabs open. This is why lots of games recommend that you restart the device if it crashes a lot.
When you're programming, keep an eye on the increase of memory (due to not properly deallocating objects) and the peak memory. This problem is more about real-world testing (on as many devices as possible) and good programming practices.
Here's an article to ready more about how to deal with memory problems:
http://akosma.com/2009/01/28/10-iphone-memory-management-tips/
This tool finds what is the maximum memory capacity of any iOS device. It also can also find at which memory level you received the Low Memory warning.
here is the link: https://github.com/Split82/iOSMemoryBudgetTest
It depends on current device state. Some native iPhone applications can run in background and waste device resources(Mail, Safari, Sms, Phone...) So, it's hard to say how much memory u will get...Ideally, your application should stay within 10-12Mb limit to successfully overcome any memory issues.
I believe it's
Memory warnings at 20MB
Application "crashes" at 30MB
I'm fairly certain I saw this in The iPhone Developer's Cookbook: Building Applications with the iPhone SDK by Erica Sadun (amazing book, btw).
Also: Duplicate -> Maximum runtime memory available in iPhone and iPod?.

iPhone: leaks from other Apps taking up RAM?

I'm reading some people stating that if another (3rd party) app on someone's iPhone has been leaking memory, that this may reduce the (mystery) amount of RAM your app would otherwise have available.
This confuses me -- does not all app memory get released when the app is closed by the user? And only one app is open at a time on iPhone?
Normally, any memory that your application allocates will be freed when it exits. However, many of Apple's applications continue running after they're "closed", so memory leaks in Mail, for instance, can affect available memory.
In addition, there are apps out there that claim to free up allocated memory. They really don't do anything other than force some dirty pages out of the buffer cache, but they appear to do something, so people believe they must be doing something useful.
On a jailbroken phone - yes, third party apps can be running at the same time as yours. Running out of memory is common with people who like to have many apps running at once hence the need for task managers, killing tasks etc.
On an unhacked phone - no. Yours is the only non-apple app that is running, no others can run at the same time.
So what can you do? All you can do is try to use the minimum memory possible which you're probably already doing. Realistically you can only test with a factory, unhacked phone, unless you are going to spend hours trying to please everyone. If you think you are maybe using too much you could identify the larger allocations using the instruments tools ("Run with performance tool >" from within Xcode) and then post that chunk of code here to get ideas of how to reduce it.
You should run Instruments and then add the instrument "Memory Monitor" to see the memory use of all of the other processes on your phone. (Add with Window -> Library , then drag the Memory Monitor instrument to the instrument panel.
What I'm still trying to determine is why is iOS releasing memory from MY app, and not all of the other memory pig apps that are not currently running.