在 iOS 9 上获得免费内存?
get free ram on iOS 9?
我正在尝试在 iOS 设备上获取免费内存,但下面的代码在 iOS 9、iphone 5s 设备上为我提供了 34MB,即使只有 1 个应用程序 运行 正在使用 98MB 内存。以下代码中是否存在错误?
natural_t get_free_memory ()
{
mach_port_t host_port;
mach_msg_type_number_t host_size;
vm_size_t pagesize;
host_port = mach_host_self();
host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t);
host_page_size(host_port, &pagesize);
vm_statistics_data_t vm_stat;
if (host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size) != KERN_SUCCESS) {
NSLog(@"Failed to fetch vm statistics");
}
natural_t mem_free = vm_stat.free_count * pagesize;
return mem_free;
}
完整报告如下:
vm_statistics_data_t vmStats;
mach_msg_type_number_t infoCount = HOST_VM_INFO_COUNT;
host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vmStats, &infoCount);
int availablePages = vmStats.free_count;
float availableMemory = (float)availablePages*vm_page_size/1024./1024.;
int activePages = vmStats.active_count;
float activeMemory = (float)activePages*vm_page_size/1024./1024.;
int inactivePages = vmStats.inactive_count;
float inactiveMemory = (float)inactivePages*vm_page_size/1024./1024.;
int wirePages = vmStats.wire_count;
float wireMemory = (float)wirePages*vm_page_size/1024./1024.;
int pageoutsPages = vmStats.pageouts;
float pageoutsMemory = (float)pageoutsPages*vm_page_size/1024./1024.;
int hitsPages = vmStats.hits;
float hitsMemory = (float)hitsPages*vm_page_size/1024./1024.;
int purgeable_countPages = vmStats.purgeable_count;
float purgeable_countMemory = (float)purgeable_countPages*vm_page_size/1024./1024.;
int speculative_countPages = vmStats.speculative_count;
float speculative_countMemory = (float)speculative_countPages*vm_page_size/1024./1024.;
iOS是Unix系统(源自BSD),兼具多任务和复杂内存管理的特点。
phone 上只有一个进程 运行 的可能性很小。即使没有应用 运行(即您在任务切换器中通过 "sliding them up" 主动终止了所有其他应用),仍有大量守护进程 运行 执行各种任务。
除此之外,还有内核使用的内存,以及通过各种路径用作缓存的内存。可用内存根本不是可用内存的真正指示,因为系统会尝试将尽可能多的内存用于缓存和其他类似目的。
我有一个 FreeBSD 盒子(FreeBSD 是,就在 Mac OS X 之后,可能是最接近 OS 到 iOS),基本上有 64 GB 的 RAM什么都没有,它报告有 848 MB 可用...但是 56 GB "inactive" 内存,超过 1 GB "cache" 和 1.5GB "buffers"。大部分内存实际上是可用的,只是不被视为 "free" 因为 OS 实际上有其页面的映射(但如果需要会释放它们)。
我正在尝试在 iOS 设备上获取免费内存,但下面的代码在 iOS 9、iphone 5s 设备上为我提供了 34MB,即使只有 1 个应用程序 运行 正在使用 98MB 内存。以下代码中是否存在错误?
natural_t get_free_memory ()
{
mach_port_t host_port;
mach_msg_type_number_t host_size;
vm_size_t pagesize;
host_port = mach_host_self();
host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t);
host_page_size(host_port, &pagesize);
vm_statistics_data_t vm_stat;
if (host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size) != KERN_SUCCESS) {
NSLog(@"Failed to fetch vm statistics");
}
natural_t mem_free = vm_stat.free_count * pagesize;
return mem_free;
}
完整报告如下:
vm_statistics_data_t vmStats;
mach_msg_type_number_t infoCount = HOST_VM_INFO_COUNT;
host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vmStats, &infoCount);
int availablePages = vmStats.free_count;
float availableMemory = (float)availablePages*vm_page_size/1024./1024.;
int activePages = vmStats.active_count;
float activeMemory = (float)activePages*vm_page_size/1024./1024.;
int inactivePages = vmStats.inactive_count;
float inactiveMemory = (float)inactivePages*vm_page_size/1024./1024.;
int wirePages = vmStats.wire_count;
float wireMemory = (float)wirePages*vm_page_size/1024./1024.;
int pageoutsPages = vmStats.pageouts;
float pageoutsMemory = (float)pageoutsPages*vm_page_size/1024./1024.;
int hitsPages = vmStats.hits;
float hitsMemory = (float)hitsPages*vm_page_size/1024./1024.;
int purgeable_countPages = vmStats.purgeable_count;
float purgeable_countMemory = (float)purgeable_countPages*vm_page_size/1024./1024.;
int speculative_countPages = vmStats.speculative_count;
float speculative_countMemory = (float)speculative_countPages*vm_page_size/1024./1024.;
iOS是Unix系统(源自BSD),兼具多任务和复杂内存管理的特点。
phone 上只有一个进程 运行 的可能性很小。即使没有应用 运行(即您在任务切换器中通过 "sliding them up" 主动终止了所有其他应用),仍有大量守护进程 运行 执行各种任务。
除此之外,还有内核使用的内存,以及通过各种路径用作缓存的内存。可用内存根本不是可用内存的真正指示,因为系统会尝试将尽可能多的内存用于缓存和其他类似目的。
我有一个 FreeBSD 盒子(FreeBSD 是,就在 Mac OS X 之后,可能是最接近 OS 到 iOS),基本上有 64 GB 的 RAM什么都没有,它报告有 848 MB 可用...但是 56 GB "inactive" 内存,超过 1 GB "cache" 和 1.5GB "buffers"。大部分内存实际上是可用的,只是不被视为 "free" 因为 OS 实际上有其页面的映射(但如果需要会释放它们)。