如何以编程方式在iPhone中查找可用内存?

Rom*_*gan 20 iphone memory-management objective-c

我想知道如何从Objective-C中找到iPhone中可编程可用的内存?

esc*_*ord 8

您可以使用以下内容获取物理内存:

NSLog(@"physical memory: %d", [NSProcessInfo processInfo].physicalMemory);
Run Code Online (Sandbox Code Playgroud)

可用的内存将不是你可以指定为硬数字的东西,因为操作系统会根据需要为你提供更多内存,以及清除文件缓存等等.你假设你正在这样做为了优化您自己的缓存,您可以根据物理内存构建缓存大小并猜测应该使用多少.例如,在一个旧的128米iphone 3g上,你的整个应用程序在它被杀之前只能得到10-15meg的ram,其中一个全新的1024meg iphone5将允许你在攻击决定杀死你之前有数百兆的ram .

请参阅http://en.wikipedia.org/wiki/List_of_iOS_devices中的设备内存


Mat*_*ias 3

您可以使用马赫调用 host_info(host, flavor, host_info, host_info_count)。如果您使用 来调用它flavor=HOST_BASIC_INFO,则指向的缓冲区host_info将填充一个 struct 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 */
}
Run Code Online (Sandbox Code Playgroud)

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