Skip navigation.
 
mlRe: Locating managed objects within ObjectAlloc (was Re: Garbage collection, core data, and tight loops)
FROM : ajb.lists
DATE : Sun Nov 04 12:27:23 2007

On Nov 3, 2007, at 7:08 PM, Ben Trumbull wrote:
>>

> You mean RSIZE as reported by 'top', not malloc's free heap space as 
> reported by 'heap'.


Actually, I was observing VSIZE (which I thought should track with 
RSIZE as long as the system isn't swapping my app) and also the graph 
in ObjectAlloc, which appeared to correlate with VSIZE.  In other 
words, even the graph of all allocations in ObjectAlloc shows only a 
small reduction when the document is closed.  'heap' is the tool I 
needed, though.  Thanks!

>> the peak memory doesn't go up, so the memory is apparently reused.

>
> Correct.  The memory is freed (heap space), but not returned to the 
> kernel (VM mapping).  This is the behavior of malloc on OSX.  You 
> will see a high watermark effect.
>

I've read that, but forgot to consider it in this case.  However, I'm 
confused by the test case below.  When the memory is malloc'ed, VSIZE 
goes up.  When the memory is used, RSIZE goes up.  When the memory is 
freed, both VSIZE and RSIZE go back down.  Does this contradict what 
you are saying?

#include <stdlib.h>

int main( int argc, char *argv[])
{
    sleep(5);
    char *ptr;
    while (1) {
        ptr = malloc(10 * 1024 * 1024);
        sleep(10);

        int i;
        for( i = 0; i < (10 * 1024 * 1024); i++)
            *(ptr + i) = 'x';
        sleep(10);

        free(ptr);
        sleep(10);
    }
}

>
> No, allocation events for managed objects are not currently tracked 
> by ObjectAlloc except as general blocks.  The 'heap' and 'leaks' 
> tools both perceive managed objects.  You'll probably find the 
> 'heap' tool useful for experimenting with these questions.  Also, 
> 'malloc_history' is much improved on Leopard and often overlooked.


I was using 'leaks' and had a few leaks, but nothing that explained 
the large amount of memory I was using, so I suspected I had a retain 
cycle with some managed objects.  Ultimately, I found I forgot to add 
an autorelease pool to one of my loops.

You are right, 'heap' would have been very useful for this.  I've used 
malloc_history for an over-release/BAD_EXEC bug, and it was great for 
that.

Thanks, Ben.

Aaron