ios - 简单的 iPhone 应用程序使用太多内存 (4MB)

标签 ios

谢谢你的时间 我正在使用:

  • XCode 4.0 版本 4A304a
  • iOS 4.3.1
  • 在 iPhone 4 上运行

我刚刚使用导航模板创建了一个项目。 该应用程序什么都不做,表格只有一行,当您点击这一行时,它会检查该应用程序使用了多少内存并将结果写入 detailTextLabel。 这个应用程序使用 4MB。这正常吗?

这是我用来获取内存信息的代码:

- (float) reportUsedMemoryInBytes {

    struct task_basic_info info;
    mach_msg_type_number_t size = sizeof(info);
    kern_return_t kerr = task_info(mach_task_self(),
                               TASK_BASIC_INFO,
                               (task_info_t)&info,
                               &size);
    if( kerr == KERN_SUCCESS ) {
        return info.resident_size;
    } else {
        NSLog(@"Error with task_info(): %s", mach_error_string(kerr));
        return -1.0;
    }
}

标签是用

填充的
cell.detailTextLabel.text = [NSString stringWithFormat:@"%.2f Mb", 
                             [self reportUsedMemoryInBytes] / 1024.0 / 1024.0];

将 Instrument 与内存监视器一起使用时,如果我向下滚动,我会看到名为 MemTest 的进程。

  1. 在实际内存列中保持 4.58 Mb
  2. 在虚拟内存列中保持 75.78 Mb

但是,如果我使用分配并检查第一行所有分配,那么

  1. 在 Live Bytes 列中保持 631.51 Kb
  2. 在 Overall Bytes 列中保持 1.26 Mb

现在这个简单的应用程序使用的正确内存量是多少? 我只想知道我的应用程序使用了多少内存,而没有框架和 IOS 库使用的内容。

感谢大家的帮助。

最佳答案

UIKitFoundation 很可能占了其中的大部分。报告的 RSS 可能包括诸如此类的共享库/框架。有关这一点的讨论,请参阅 "Understanding Memory Usage on Linux" :

Why ps is "wrong"

Depending on how you look at it, ps is not reporting the real memory usage of processes. What it is really doing is showing how much real memory each process would take up if it were the only process running. Of course, a typical Linux machine has several dozen processes running at any given time, which means that the VSZ and RSS numbers reported by ps are almost definitely "wrong". In order to understand why, it is necessary to learn how Linux handles shared libraries in programs.

Most major programs on Linux use shared libraries to facilitate certain functionality. For example, a KDE text editing program will use several KDE shared libraries (to allow for interaction with other KDE components), several X libraries (to allow it to display images and copy and pasting), and several general system libraries (to allow it to perform basic operations). Many of these shared libraries, especially commonly used ones like libc, are used by many of the programs running on a Linux system. Due to this sharing, Linux is able to use a great trick: it will load a single copy of the shared libraries into memory and use that one copy for every program that references it.

关于ios - 简单的 iPhone 应用程序使用太多内存 (4MB),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7224659/

相关文章:

ios - 如何使用 SubView 的 SuperView 初始化 UIView

ios - Xcode 6 : "Archive validation failed with error" "Invalid image path"

ios - 如何将 Objective-C 外部常量迁移到 Swift

ios - Mailcore2 库文件大小将近 200MB Xcode 9 Swift 4

ios - 新 WCSessionDelegate 方法在早期版本的 iOS 和 watchOS 上的行为

ios - 表格单元格中的很棒的菜单检测点击

ios - UIViewController 报告为响应 addChildViewController : on iOS 4

ios - MonoTouch 在选项卡之间传递数据

ios - 异步添加图片不会自行显示

ios - Swift UIImage 命名返回 nil 并在 Assets 中使用通用图像