Re: Memory problem



xcommerx <xcommerx.1vfaxg@xxxxxxxxxxxxxxxxxxx> wrote:
>
> I test the memory usage with my System Resources Manager and the top
> command (I work with Linux but the Mac forums are the only place to go
> to with Objective-C questions).

Well, there's your error.

There are two levels of memory allocation going on here, even though you
the programmer normally only sees one of them.

One level is the malloc level, where you can request things in byte-sized
chunks (or actually, on OS X, 16-byte-sized chunks, probably similar on
Linux). At a lower level, malloc is getting memory from the OS in
page-sized chunks, which is 4kB on OS X and most other OSes.

Things like fragmentation can prevent malloc from giving those pages back
to the OS. If you allocate enough objects to fill a 4kB page and then
destroy all but one of them, that page has to stay mapped into your
process. But the good news is that the page can be reused to allocate more
objects, so this isn't a big deal.

Malloc will also hang on to the page even if it's empty, a lot of the
time, because allocating and deallocating a page is expensive. Chances are
that your program is going to need to allocate more memory soon, and so it
won't give it up right away.

To summarize: looking at your program's memory usage by using top or
similar tools looks at its usage at the system (page) level, and will be
extremely misleading when you're worrying about usage at the user (object)
level.

I don't know what's available on Linux, but on the Mac I would use tools
such as ObjectAlloc, MallocDebug, 'leaks', etc. to discover whether I was
leaking any objects. I'm sure similar things exist in the Linux world, you
just need to find the right tool for the job.

--
Michael Ash
Rogue Amoeba Software
.



Relevant Pages

  • malloc under linux
    ... Linux follows an optimistic memory allocation strategy. ... This means that when malloc() returns non-NULL there is no guarantee that the memory really is available. ... jacob navia ...
    (comp.lang.c)
  • Re: malloc under linux
    ... Linux follows an optimistic memory allocation strategy. ... This means that when malloc() returns non-NULL there is no guarantee ...
    (comp.lang.c)
  • Re: malloc under linux
    ... Linux follows an optimistic memory allocation strategy. ... This means that when malloc() returns non-NULL there is no guarantee ...
    (comp.lang.c)
  • Re: best way to discover this processs current memory usage, cross-platform?
    ... > intercept each call to malloc and free, ... That'll only get you memory usage from malloc/free calls, ... that uses large shared memory segments generated with mmap() or SysV ...
    (comp.lang.python)
  • Re: malloc under linux
    ... Linux follows an optimistic memory allocation strategy. ... This means that when malloc() returns non-NULL there is no guarantee ... <end quote> ...
    (comp.lang.c)