50

... when it's in the foreground.

For the original iPad, with 256MB RAM, I found that my app could use up to 100-140MB before it got killed.

What's the situation nowadays? I could do go buy a bunch of iDevices and do a test myself, but I wanted to check: Has anyone done a test?

I understand that this does not have a precise answer, and I'm looking for a range, like: "Apps are killed when they use 300-350MB on a 512MB device. You can safely use up to 300MB."

Specifically:

  1. On a device with 512MB memory, how much can one app use?

  2. On a device with 1GB memory, how can can one app use?

  3. Is there a difference between the above? Is each individual app limited to a fixed amount of memory in the interest of keeping a few apps in the background, or can the foreground app kick out ALL background apps from memory, and take the whole 1GB (or 512MB) to itself (and the OS, of course)?

  4. Does it matter whether the device is an iPad or an iPhone? If I get my app working on an iPad with 512MB memory, does it mean that it will also work on an iPhone with 512MB memory, and vice-versa? I know that UIViews, and their Core Animation backing stores, will take more memory on the iPad because of the larger screen size, but other than that, is the memory situation the same between an iPhone and an iPad with the same memory?

I'm referring to the total memory used by the process -- heap, stack, static data, code, Core Animation backing stores, etc.

If you're inclined to say that it depends on the OS version, you can assume we're talking about iOS 7.

I know that using too much memory means that when my app goes into the background, iOS will terminate it quicker. I'm fine with this tradeoff for now.

Kartick Vaddadi
  • 4,818
  • 6
  • 39
  • 55
  • 2
    That's not true. For the original iPad, with 256MB RAM, I found that my app could use up to 100-140MB before it got killed. – Kartick Vaddadi Jan 12 '14 at 05:06
  • 3
    Even if it were true, that wouldn't make it a bad QUESTION. "No one outside Apple knows" would be an answer in that case. – Kartick Vaddadi Jan 12 '14 at 05:07
  • @HotLicks I didn't say I was looking for a precise answer. A range will do, as I said in my comment on xcodeNoob's answer. Like: 350-400MB on a 512MB device. I'm clarifying the question. – Kartick Vaddadi Jan 12 '14 at 14:39
  • HotLicks is right about no one knowing an exact answer. From all the posts I have read in the past, I would venture to guess that you can use 10% as a rough number. If you use lazy loading and other such techniques, you should not ever get too close to that number anyway. – sangony Jan 12 '14 at 15:04
  • Doesn't iOS evict background apps as necessary for the foreground app? That means, on a 1 GB device, I can use 1GB - memory needed for the OS. Since the memory needed for the OS has to be < 400M (otherwise, iOS 7 wouldn't run on 512M devices), that means I am guaranteed to have 600M available on a device with 1G RAM. Correct? – Kartick Vaddadi Jan 12 '14 at 15:30
  • (I kinda suspect that Apple uses some sort of "learning" algorithm that keeps statistics on an app and gives it more or less space based on how it behaves and how the user uses it. An app that grows steadily is apt to be treated worse than one that reaches a level and stays there, eg, and an app that has been well-behaved in the past is apt to be given a bit more leeway.) – Hot Licks Jan 12 '14 at 15:42
  • I read that that leeway applies when your app is in the background: if you use more memory, you're more likely to be killed when you go into the background. – Kartick Vaddadi Jan 12 '14 at 15:44
  • @sangony I know about lazy loading and other such techniques. I appreciate that you're trying to help, but I'd like an answer to the question I asked and not random optimization tips. – Kartick Vaddadi Jan 12 '14 at 15:45
  • Obviously no one knows an exact number. I suggest you open a technical support ticket in your Apple developer account and ask Apple directly. – sangony Jan 13 '14 at 15:10
  • As I said a couple of times, I'm not looking for an exact number, but a range, like 300-350MB. – Kartick Vaddadi Jan 13 '14 at 15:45
  • 2
    @Kartick this is perfectly valid question and your responses are terrific. Reading through this was very helpful to me, as I've seen my app just jump from 86 MB to 136 MB after a recent iteration. I've decided to make a goal of staying right around 100 MB - this will keep me lean and allow me to pivot quicker if and when I want to modify my app for Apple Watch or tvOS. – Sergi Nov 16 '15 at 19:14
  • @whalesharkie Happy it was useful. Do you have access to a device with 1GB memory, like the iPhone 5, 5c or 5s? Can you run my test app, twice, and report the lower of the two numbers? Don't quit other apps or reboot the device. Thanks — it will help me with my design. – Kartick Vaddadi Apr 21 '17 at 05:59

4 Answers4

49

I wrote a test app that measures how much memory an app can allocate before it's killed. Here are the numbers:

  • iPhone 5s (iOS 10, debug mode, 1GB memory): 600MB can be allocated
  • iPad Air 2 (iOS 11.4, 2GB memory): 1.3GB can be allocated
  • iPhone X (iOS 11.4, 3GB memory): 1.2GB can be allocated
  • iPhone 7 Plus (iOS 12.1, 3GB memory): 1.8GB can be allocated
  • iPad 13-inch (iOS 11.4, 4GB memory): 3GB can be allocated

It's interesting that I never got a memory warning.

Here's the code if you want to run the test yourself:

import UIKit

let sizeInMb = 100

class Wrapper {
  var array = [UInt8](repeating: 0, count: sizeInMb * 1048576)  // 100 MB
}

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        window = UIWindow(frame: UIScreen.main.bounds)

        var i = 0

        sleep(5)  // So that you can see how much memory it consumes before any allocations.

        while true {
            let w = Wrapper()
            Unmanaged<Wrapper>.passRetained(w)
            i += 1
            print("\(i * sizeInMb) MB allocated")
            sleep(1)  // Give the OS a chance to kill other processes.
        }

        return true
    }

    func applicationDidReceiveMemoryWarning(_ application: UIApplication) {
        print("Memory warning!")
    }
}

This does not work on the simulator. Anything regarding performance should be tested on device.

Kartick Vaddadi
  • 4,818
  • 6
  • 39
  • 55
  • 2
    It seems to be around 60-70% of the memory capacity of the device. – Hlung Jan 16 '19 at 07:03
  • Is this also work on simulators? (I ran this on iPhone SE simulator with iOS12 and it went above 25GB and I stopped it manually) – Ahmadreza Aug 26 '19 at 03:41
12

As of 2014 my minimum hardware testing device is an iPhone 4s running iOS7 with ~50 apps installed. After a reboot, the OS can free up 200mb out of 512 total. After a week of regular use, the best it can manage is 100mb.

I'm developing an Adobe AIR app which does not receive low memory warnings from the OS. If it gets near the limit, it crashes. So try to stay under 100mb if you want to run on devices with 512mb total ram. Remember, this is for the gpu and cpu combined.

Sarah Northway
  • 1,029
  • 1
  • 14
  • 24
  • hows cpu gpu and ram related? – Esqarrouth Apr 04 '15 at 18:31
  • 4
    Usually graphics textures are stored on the video card in GPU ram, but iOS devices use a Unified Memory Architecture and store their textures in CPU ram. http://apple.stackexchange.com/questions/54977/how-much-gpu-memory-do-iphones-and-ipads-have – Sarah Northway Sep 19 '15 at 19:18
9

Hi I just tested with my app, for a 512MB device, the app will crash anytime after 250mb usage, giving "Memory Pressure" issue.

Ajeet
  • 892
  • 1
  • 9
  • 22
2

In case it's of interest to anyone else:

On an iPhone 12 Pro, with 6 GB of RAM (AFAIK), it allocated 2800 MB (almost half) before being terminated. YMMV

NomDeGear
  • 54
  • 1