简体   繁体   English

Objective-C内存使用率

[英]Objective-C memory usage

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)? 我一定会遗漏一些显而易见的东西,但是下面这个非常简单的应用程序创建了1000个1MB数据对象并将它们存储在一个数组中的确能正常工作,而且似乎并没有饱和(该应用程序的内存占用在Xcode中显示为1.7MB) ? This code was tested on an iPad 2 with 1GB of memory and does not crash. 此代码已在具有1GB内存的iPad 2上进行了测试,不会崩溃。

@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? 我想问题确实在于,是否某些分配实际上是在iPad闪存(而不是RAM)上完成的,是否有人对此有更多详细信息?

Apple have always made the following statement about the iPhone Simulator : 苹果一直对iPhone模拟器发表以下声明:

Important : The simulator is great for debugging, but the ultimate arbiter of what will and won't work on iOS is a real device . 重要提示 :模拟器非常适合调试,但是在iOS上可以使用和不能使用的最终仲裁者是真正的设备 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. 您将不会有标准主轴硬盘驱动器带来的性能下降。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM