iOS: how to count open apps - ios

Can someone suggest to me a way to count (inside an iOS app) all open apps on OS, to show to the user an alert if there's not enough memory to run my app?

While theoretically this is possible, it's not a good idea. First of all, the number of open apps is an inadequate measure when in fact you care about memory consumption. Next, this doesn't take into account the fact that different devices have different amounts of memory. Last but not least, if memory is running tight iOS will first kill background applications and free some memory for you.
So, don't do it. Instead, try to be a better iOS citizen: respond to memory warnings, try to cache stuff in files and read up on memory mapping (for example with mmap) to reduce your app's memory footprint.

Related

What API do apps like Battery Doctor use to free up RAM

How do apps like battery doctor able to free RAM on my iphone. what API do they use? Any example/reference please?
Initially i thought these apps just fake it. Now i used two apps. Used one to clean up the memory and then open the other app to find the memory was really cleaned by the previous one. So there should be some level of access apple should have provided.
update1:
Why am asking this? Try this yourself.
Install Battery doctor(https://itunes.apple.com/gb/app/battery-doctor-master-battery/id446751279?mt=8) and Wdgts app(https://itunes.apple.com/us/app/wdgts-collection-awesome-notification/id916103272?mt=8). Now Add Memory & Disk widget to Todays widget and check the memory available. Go to Battery doctor and clean memory. Open the todays notification and you will see the memory displayed has changed and cleaned.
Just a guess but they probably cause the OS to invoke a low memory handler which in turn causes it to send messages to some apps to free memory and to shutdown others. The easiest way for them to do that would be to just allocate a lot of memory. Depending on the maturity of the OS, they may have to actually write to each memory block to ensure it is backed. Then free it. Immediately.
They just fake it. iOS apps are sandboxed and have no access to memory outside of what the operating system has allocated to the app. Only the operating system can terminate processes to free up memory.

Why Does the App crashes on Low Device Memory?

I am particularly new to iOS & still quite distant from the basic Concepts.
I read in iOS books & forums that the application crashes due to low memory but why does it crash?
It would be helpful if someone could throw some light.
iOS Devices Use virtual memory with Paging . As it is a Mobile device and there is no Extensible memory or pretty
large Memory available (Like Hard drives) , so the availability of the pages is limited by various factors such as the number of applications open , Allocations by different applications , etc. Moreover , some on-board applications will always keep using some pages even when they are in dormant state such as safari , i-tunes , messaging etc.
So , essentially with number of application active , the number of pages your application can use gets diminished further.
So , your application will crash in cases when the rate of allocation by your application is exceeding the rate at which pages are being freed by other Applications.
OS only frees up read-only data from the memory while writable data is not freed-up .When the writable data crosses a certain threshold , the OS asks the application to free memory.Unable to free the memory leads to the crash.
Memory Allocations Apple
It does not really crash. Apps get terminated by the kernel if they do not free enough memory after a notification.
To make this transparent a crash report is written which contains the specifics about the current situation. To the user it looks like the application crashed, as it just suddenly disappears.
The devices don’t have much memory and if you are piggy with memory, you are looking for trouble
iOS have good memory tool called ARC. Please read the full documentation here
I've been burned before in my own apps when loading table views consisting of nothing but UIImages, which might look like tiny thumbnails in the table but end up being full resolution (and lots of memory) objects behind the scenes.
You need to be careful when working with objects that can be potential memory hogs.
But take heart, Apple provides tools like Xcode Instruments you can use to profile your app's memory performance.

How much memory can an iOS app allocate?

I'm trying to get a feel for the amount of memory an iOS app can reliably allocate to help me drive some design decisions. The app is going to involve real time synchronised processed audio and animation.
Other than writing code that loads up the frameworks I'll need then trying to progressively allocate memory until I get warnings, is there any way to determine this kind of thing?
The simulator doesn't let you select a specific hardware model, so I assume I can't even simulate this stuff.
You cannot determine how much memory an app allocate as far as I know. Always try to keep the lowest memory possible for your app.
The memory allocated to your app depends on many factors like : number of background process happening, amount of memory available, memory used by other apps, the device you are running etc..
So, its not a good practice to keep a maximum line for memory consumed by your app and design accordingly.
Also try to hold only the necessary memory you need and handle memory issue in the memory callback methods like 'didreceivememorywarning'. Always consider that you have the least amount of memory allocated by the OS.

How to tell users how much memory your iOS application needs

I'm developing an iOS application which (like any other) requires a certain amount of free memory to run correctly. In my case - at least 4MB, I cannot use any less than that. It's a fairly small amount, but a few times (on my device at least) I got only 2MB free and the program crashed. What do you think is the best way to tell users how much memory you need. I know the code to get the currently available memory, but even if I tell the user (like in a UIAlertView when the user starts the program) that he is running low, what can I suggest him to do to free more memory (except turning off and on the device). Any ideas?
On older devices you can't really rely on getting more than 8MB. 4MB is a great target, and if through your profiling you've determined that's all you need, you should be fine.
However, I think the concept here is that if you receive memory warnings you wouldn't bother the user with those types of things. I would find it pretty annoying myself. It would be better to limit your app's activity or throttle back whatever you are doing that is so memory intensive.
On which kinds of iPhone devices your app is being tested? I suppose that the iOS has to do its job well to free enough memory for you or kill alll background app so it can have more memory

iPad, any way to clear extra memory before running app?

I am creating apps for the Ipad and its driving me crazy.
The memory that is usable by the apps changes depending on what other apps were ran before it.
There is no reliable set amount of memory that can be used by your app.
i.e. If safari is ran then even after it closes it takes up some amount of memory which effects other apps.
Does anyone know if there is a way to clear the memory before my app runs so I can get the same running environment every time?
I have created several prototype apps to show to other people and it seems like after a few days they always come back to me and tell me that it crashes and to fix it.
When I test it, the reason is always because there is not enough memory (when there was enough before when I was testing). So I need to squeeze every bit of memory (which usually effects performance due to heavy loading and releasing) out of the app and tell them to restart their ipad if it continues to happen.
I read in a book that generally apps can use at max 40mb or so, most of the apps that crash are crashing at around 27mb. I want my remaining 13mb!!
While you will get a pretty good state after a reboot, what you really should look for is clean memory management and avoiding leaks.
Using the available memory wisely is solely up to the programmer. Don't tell your users to reboot the device, ever. And with every update of the OS memory things might change anyway.

Resources