2

I must be missing something obvious, but how come that the following very simple app that creates 1000 1MB data objects and stores them in an array does work and does not seem to saturate (memory footprint of the app is shown as 1.7MB in Xcode)? This code was tested on an iPad 2 with 1GB of memory and does not crash.

@implementation AppDelegate {
    NSMutableArray* datas;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    datas = [NSMutableArray new];
    for (int i = 0; i < 1024; i++) {
        [datas addObject:[NSMutableData dataWithLength:1024*1024]];
    }
    return YES;
}

@end

I guess that the question really is if some of the allocations are actually done on the iPad flash memory (instead of RAM), and if anybody has any more details about this?

5
  • This crashes for me when run on a device. The dataWithLength call starts returning nil (I think when it runs out of memory) and the addObject: call throws an exception. Commented Sep 30, 2014 at 17:55
  • You are right, this starts to crash at some point. However, I still not quite understand why Xcode does report only 1.7MB memory footprint when running the app. Commented Sep 30, 2014 at 17:58
  • 1
    When you say Xcode reports only 1.7 MB footprint is this in the simulator? Commented Sep 30, 2014 at 18:59
  • 1
    This is done on a real device (iPad 2) Commented Oct 1, 2014 at 0:07
  • I get an out-of-memory crash if I catch the exception and NSLog one of the data objects. Commented Oct 1, 2014 at 20:57

1 Answer 1

3

Apple have always made the following statement about the iPhone Simulator:

Important: The simulator is great for debugging, but the ultimate arbiter of what will and won't work on iOS is a real device. It is especially important to keep this in mind when doing performance testing and debugging.

According to Apple Memory Usage Performance Guidelines:

If you do not plan to use a particular block of memory right away, deferring the allocation until the time when you actually need it is the best course of action. For example, to avoid the appearance of your app launching slowly, minimize the amount of memory you allocate at launch time.

So I am not sure if this is just an exercise to see how much memory you can alloc or not. But if it's a prototype for a future application, I would change the structure of your application and only load the information you need to display when you need it. Remember all data is living on a solid state device. You will not have the performance hit you would take with a standard spindle hard drive.

Sign up to request clarification or add additional context in comments.

1 Comment

I should have added that this was done on an iPad 2, not on the simulator. The idea is definitely not to load memory at the start. The issue is that I was surprised that I can load more than 1GB of memory on an iPad 2 which has 1GB RAM, and that I am surprised that XCode 6 does not report this memory usage (memory usage stays around 1.7MB)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.