Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It takes some digging through the source to figure it out, but Top uses the task_info() call to interface with the Mach kernel and gather memory statistics. You can read some <em>mostly</em> correct information about task_info() at <a href="http://www.gnu.org/software/hurd/gnumach-doc/Task-Information.html" rel="nofollow">http://www.gnu.org/software/hurd/gnumach-doc/Task-Information.html</a>. I say mostly correct because there's at least one difference I've found in the OS X implementation: memory sizes are reported in bytes, not pages.</p> <p>As a summary, you pass task_info() a "target task" (mach_task_self() if you want information about your program itself, otherwise use task_for_pid() or processor_set_tasks()) and tell it you want "basic info," the category under which virtual and resident memory sizes fall. Then task_info() fills in a task_basic_info struct with the information you want.</p> <p>Here's a class I wrote to get the resident memory size. It also shows how you can use sysctl to get information about your system (how much physical ram you have, in this instance):</p> <pre><code>#include &lt;sys/sysctl.h&gt; #include &lt;mach/mach.h&gt; #include &lt;cstdio&gt; #include &lt;stdint.h&gt; #include &lt;unistd.h&gt; //////////////////////////////////////////////////////////////////////////////// /*! Class for retrieving memory usage and system memory statistics on Mac OS X. // (Or probably any system using the MACH kernel.) */////////////////////////////////////////////////////////////////////////////// class MemoryInfo { public: /** Total amount of physical memory (bytes) */ uint64_t physicalMem; //////////////////////////////////////////////////////////////////////// /*! Constructor queries various memory properties of the system */////////////////////////////////////////////////////////////////////// MemoryInfo() { int mib[2]; mib[0] = CTL_HW; mib[1] = HW_MEMSIZE; size_t returnSize = sizeof(physicalMem); if (sysctl(mib, 2, &amp;physicalMem, &amp;returnSize, NULL, 0) == -1) perror("Error in sysctl call"); } //////////////////////////////////////////////////////////////////////// /*! Queries the kernel for the amount of resident memory in bytes. // @return amount of resident memory (bytes) */////////////////////////////////////////////////////////////////////// static size_t Usage(void) { task_t targetTask = mach_task_self(); struct task_basic_info ti; mach_msg_type_number_t count = TASK_BASIC_INFO_64_COUNT; kern_return_t kr = task_info(targetTask, TASK_BASIC_INFO_64, (task_info_t) &amp;ti, &amp;count); if (kr != KERN_SUCCESS) { printf("Kernel returned error during memory usage query"); return -1; } // On Mac OS X, the resident_size is in bytes, not pages! // (This differs from the GNU Mach kernel) return ti.resident_size; } }; </code></pre>
    singulars
    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. 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.
    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