我想知道如何从Objective-C中找到iPhone中以编程方式可用的内存?
您可以使用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 */
}
通过此结构,您可以获取内存大小。
您可以通过以下方法获得物理内存:
NSLog(@"physical memory: %d", [NSProcessInfo processInfo].physicalMemory);
可用内存将不再是一件容易的事,因为操作系统会根据需要为您杀死后台应用程序,以便为前台应用程序提供更多内存,并清除文件缓存等。假设您正在这样做可以优化自己的缓存,您可以根据物理内存来构建缓存大小,然后猜测应该使用多少缓存。例如,在旧的128m iphone 3g上,您的整个应用在被杀死之前可能只会得到10-15兆公分的公羊,而在操作系统决定杀死您之前,全新的1024meg iphone5将允许您数百兆的公羊。
Swift 5] >>
您的Ram大小(以字节为单位):
let totalRam = ProcessInfo.processInfo.physicalMemory