一个iOS应用程序可以使用多少内存?

问题描述:

...当它在前台时。

... when it's in the foreground.

对于原装iPad,拥有256MB RAM,我发现我的应用程序可以使用高达100-140MB被杀了。

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

现在的情况如何?我可以去买一堆iDevices并自己做一个测试,但我想检查一下:有人做过测试吗?

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?

我知道这没有确切的答案,我正在寻找一个范围,例如:应用程序在512MB设备上使用300-350MB时会被杀死。您可以放心使用高达300MB。

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."

具体来说:


  1. 在具有512MB内存的设备上,一个应用程序可以使用多少?

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

在内存为1GB的设备上,一个应用程序如何使用?

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

两者之间是否存在差异?以上?为了在后台保留一些应用程序,每个应用程序是否限制在固定数量的内存中,或者前台应用程序可以从内存中踢出所有后台应用程序,并将整个1GB(或512MB)带到自身(以及OS,当然)?

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)?

设备是iPad还是iPhone?如果我让我的应用程序在具有512MB内存的iPad上运行,是否意味着它也适用于具有512MB内存的iPhone,反之亦然?我知道UIViews和他们的核心动画支持商店会因为更大的屏幕尺寸而在iPad上占用更多内存,但除此之外,iPhone和具有相同内存的iPad之间的内存情况是否相同?

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.

如果你倾向于说它取决于操作系统版本,你可以假设我们在谈论iOS 7。

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

我知道使用太多内存意味着当我的应用程序进入后台时,iOS会更快地终止它。我现在对这个权衡很好。

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.

我写了一个测试应用程序来测量应用程序之前可以分配多少内存它被杀了。以下是数字:

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,调试模式,1GB内存):可分配600MB

  • iPad Air 2(iOS 11.4,2GB内存):可分配1.3GB

  • iPhone X(iOS 11.4,3GB内存):可分配1.3GB

  • iPhone 7 Plus(iOS 10,调试模式,3GB内存):可分配2GB

  • iPad 13英寸(iOS 11.4,4GB内存):3GB可以分配

  • 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.3GB can be allocated
  • iPhone 7 Plus (iOS 10, debug mode, 3GB memory): 2GB 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!")
    }
}