Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
368 views
in Technique[技术] by (71.8m points)

iphone - How much memory can one iOS app use?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...