Note that there are some explanatory texts on larger screens.

plurals
  1. PODiagnosing heap fragmentation on Mac OS X?
    text
    copied!<p>A Core Foundation app I'm writing seems to be consuming way more memory (according to the "Real Mem" count in Activity Monitor) than I am ever actually allocating. </p> <p>I have confirmed my actual allocations are what I expect them to be (about 10MB) via the Live Bytes Allocations view in Instruments, but the "Real Mem" count in Activity Monitor shows > 60MB and apparently growing. I've also confirmed there are no leaks, also using Instruments.</p> <p>My application keeps a large queue of buffers of varying size, and is constantly free()-ing and malloc()-ing buffers as it adds/removes queue items. </p> <p>Having read a little bit about heap fragmentation, this seems a likely explanation for what is happening. So my questions are as follows:</p> <ol> <li><strong>Is there any way to confirm this on OS X e.g. maybe get a visual representation of the heap?</strong></li> <li><strong>Is there an optional low fragmentation heap manager for OS X as there is for Windows?</strong></li> </ol> <p>For anyone who wants to replicate the problem, the following example code shows up the same symptoms quite nicely:</p> <pre><code>#define MAX_SIZE (10*1024*1024) int main (int argc, const char * argv[]) { size_t actual_alloc=0; size_t max_alloc=0; char *bigbuf=NULL; size_t bigsize=0; for (long x=0; x&lt;10000000; x++) { if (bigbuf!=NULL) { actual_alloc -= bigsize; free(bigbuf); } bigsize = rand() % MAX_SIZE; // alloc random amount up to MAX_SIZE bigbuf = (char*)malloc(bigsize); memset(bigbuf, 'x', bigsize); actual_alloc += bigsize; if (actual_alloc &gt; max_alloc) max_alloc = actual_alloc; if (x%100==0) { printf("alloc = %u \t max = %u\n", (unsigned long)actual_alloc, (unsigned long)max_alloc); // max_alloc tends towards 10MB, // "Real Mem" in activity monitor tends towards 60MB } } return 0; } </code></pre> <p>If you remove the random element from the above code, you get around 10MB process memory usage as expected.</p>
 

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