Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To explain your own answer: This is not objective-C code, but plain old C, using the Mach APIs in order to obtain statistics from the kernel. Mach APIs are low level APIs exported by XNU, which is a hybrid kernel composed of the top layer (which exports BSD APIs, like the usual system calls we know and love from UN*X), and the bottom layer- which is Mach. The code uses the Mach "host" abstraction which (among other things) provides statistics about OS level utilization of resources.</p> <p>Specifically, here's a full annotation:</p> <pre><code>#import &lt;mach/mach.h&gt; // Required for generic Mach typedefs, like the mach_port_t #import &lt;mach/mach_host.h&gt; // Required for the host abstraction APIs. extern "C" // C, rather than objective-c { const int HWUtils_getFreeMemory() { mach_port_t host_port; mach_msg_type_number_t host_size; vm_size_t pagesize; // First, get a reference to the host port. Any task can do that, and this // requires no privileges host_port = mach_host_self(); host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t); // Get host page size - usually 4K host_page_size(host_port, &amp;pagesize); vm_statistics_data_t vm_stat; // Call host_statistics, requesting VM information. if (host_statistics(host_port, // As obtained from mach_host_self() HOST_VM_INFO, // Flavor - many more in &lt;mach/host_info.h&gt;(host_info_t)&amp;vm_stat, // OUT - this will be populated &amp;host_size) // in/out - sizeof(vm_stat). != KERN_SUCCESS) NSLog(@"Failed to fetch vm statistics"); // log error /* Stats in bytes */ // Calculating total and used just to show all available functionality // This part is basic math. Counts are all in pages, so multiply by pagesize // Memory used is sum of active pages, (resident, used) // inactive, (resident, but not recently used) // and wired (locked in memory, e.g. kernel) natural_t mem_used = (vm_stat.active_count + vm_stat.inactive_count + vm_stat.wire_count) * pagesize; natural_t mem_free = vm_stat.free_count * pagesize; natural_t mem_total = mem_used + mem_free; NSLog(@"used: %u free: %u total: %u", mem_used, mem_free, mem_total); return (int) mem_free; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload