-1

I am trying to get free ram on iOS device but the code below gives me 34MB on an iOS 9, iphone 5s device even when there is only 1 app running which is using 98MB ram. Is there some bug in the below code ?

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;
}
anuj
  • 1,010
  • 2
  • 11
  • 26
  • 1
    Your code is exactly the same as http://stackoverflow.com/questions/5012886/determining-the-available-amount-of-ram-on-an-ios-device so it is unlikely to have a bug. – matt Feb 07 '16 at 22:37
  • check also this other topic http://stackoverflow.com/questions/26198073/query-available-ios-device-memory-with-swift – Ulysses Feb 07 '16 at 22:40
  • @matt can you please try the code out. This code tells me that I have 134MB memory on my iPhone 5s on iOS 9 when there is no apps in the background and the only code in the current app is the code to get available memory. – anuj Feb 07 '16 at 22:46
  • @anuj It sounds like you may have a misunderstanding about what "free" means in a virtual memory system. – matt Feb 07 '16 at 22:50

2 Answers2

3

iOS is a Unix system (derived from BSD), which features both multi-tasking and complex memory management.

There is very little chance there's only a single process running on the phone. Even if no apps are running (i.e. you actively terminated all other apps by "sliding them up" in the task switcher), there's a gazillion daemons still running to perform various tasks.

Beyond that, there's memory used by the kernel, and memory used as a cache through various paths. Free memory is not really indicative of available memory at all, as the system tries to use as much memory as possible for caches and other similar purposes.

I have a FreeBSD box (FreeBSD being, right after Mac OS X, probably the closest OS to iOS) with 64 GB of RAM doing basically nothing, and it reports 848 MB free... but 56 GB "inactive" memory, over 1 GB "cache" and 1.5GB "buffers". Most of that memory is actually available, it's just not considered "free" as the OS actually has mappings for its pages (but will release them if needed).

jcaron
  • 17,302
  • 6
  • 32
  • 46
0

Here is a complete report:

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.;
SwiftArchitect
  • 47,376
  • 28
  • 140
  • 179
  • can you please try the code out. This code tells me that I have 134MB memory on my iPhone 5s on iOS 9 when there is no apps in the background and the only code in the current app is the code to get available memory. – anuj Feb 07 '16 at 22:47