【问题标题】:How to find available memory in iPhone programmatically?如何以编程方式在 iPhone 中查找可用内存?
【发布时间】:2010-11-04 10:54:36
【问题描述】:

我想知道如何通过 Objective-C 在 iPhone 中以编程方式查找可用内存?

【问题讨论】:

  • 适用于您的应用、总物理内存还是文件系统存储内存?

标签: iphone objective-c memory-management


【解决方案1】:

Swift 5

您的内存大小(以字节为单位):

let totalRam = ProcessInfo.processInfo.physicalMemory

【讨论】:

    【解决方案2】:

    您可以通过以下方式获取物理内存:

    NSLog(@"physical memory: %d", [NSProcessInfo processInfo].physicalMemory);
    

    可用内存将不是您可以确定的硬数字,因为操作系统会根据需要为您关闭后台应用程序,以便为前台应用程序提供更多内存,以及清除文件缓存等。假设您这样做是为了优化您自己的缓存,您可以根据物理内存构建缓存大小并猜测您应该使用多少。例如,在旧的 128m iphone 3g 上,您的整个应用程序在被杀死之前可能只会获得 10-15meg 的 ram,而全新的 1024meg iphone5 将允许您在操作系统决定杀死您之前获得数百兆字节的 ram .

    http://en.wikipedia.org/wiki/List_of_iOS_devices查看设备中的内存

    【讨论】:

    • 我猜应该是这个,NSLog(@"Physical Memory :%llu",[NSProcessInfo processInfo].physicalMemory);
    【解决方案3】:

    您可以使用Mach call host_info(host, flavor, host_info, host_info_count)。如果你用flavor=HOST_BASIC_INFO 调用它,host_info 指向的缓冲区被一个结构host_basic_info 填充,看起来像这样:

    struct host_basic_info {
        integer_t               max_cpus;               /* max number of CPUs possible */
        integer_t               avail_cpus;             /* number of CPUs now available */
        natural_t               memory_size;            /* size of memory in bytes, capped at 2 GB */
        cpu_type_t              cpu_type;               /* cpu type */
        cpu_subtype_t           cpu_subtype;            /* cpu subtype */
        cpu_threadtype_t        cpu_threadtype;         /* cpu threadtype */
        integer_t               physical_cpu;           /* number of physical CPUs now available */
        integer_t               physical_cpu_max;       /* max number of physical CPUs possible */
        integer_t               logical_cpu;            /* number of logical cpu now available */
        integer_t               logical_cpu_max;        /* max number of physical CPUs possible */
        uint64_t                max_mem;                /* actual size of physical memory */
    }
    

    从这个结构中,可以得到内存大小。

    【讨论】:

      猜你喜欢
      • 2011-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-16
      • 2012-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多