[英]How to find available memory in iPhone programmatically?
我想知道如何通過 Objective-C 在 iPhone 中以編程方式找到可用內存?
您可以通過以下方式獲取物理內存:
NSLog(@"physical memory: %d", [NSProcessInfo processInfo].physicalMemory);
可用內存將不是您可以確定的硬數字,因為操作系統會根據需要為您關閉后台應用程序,以便為前台應用程序提供更多內存,同時清除文件緩存等。假設您正在執行此操作要優化您自己的緩存,您可以根據物理內存構建緩存大小並猜測應該使用多少。 例如,在舊的 128m iphone 3g 上,您的整個應用程序在被殺死之前可能只會獲得 10-15megs 的內存,而全新的 1024meg iphone5 將允許您在操作系統決定殺死您之前獲得數百兆字節的內存.
您可以使用Mach 調用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 */
}
從這個結構中,你可以得到內存大小。
斯威夫特 5
您的內存大小(以字節為單位):
let totalRam = ProcessInfo.processInfo.physicalMemory
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.