Re: how much does free free?



On 27 Apr 2007 22:14:44 -0700, pete m <pmac360@xxxxxxxxx> wrote in
comp.lang.c:

On Apr 27, 8:58 pm, Jack Klein <jackkl...@xxxxxxxxxxx> wrote:
On 27 Apr 2007 20:06:23 -0700, pete m <pmac...@xxxxxxxxx> wrote in
comp.lang.c:

wojtek.fedo...@xxxxxxxxx wrote:
Hi,
Is there a way of knowing how much memory a call to free() actually
freed?
thanks,

Not in general. However, if your system supports the undocumented
function mstats(), you can call it before and after free and take the
difference. (You may have to use debug malloc, which is horrible.)

Your reply, in addition to being off-topic, will be totally useless to
the OP if his (unnamed) platform is not the same as your (unnamed)
platform.

This group discusses the standard C language. It does NOT have any
undocumented functions.

In fact, all of the answers posted so far are wrong in one way or
another.
My answer was off topic; yours gives the wrong value.

Free does not free the same amount of memory as the size of the malloc
request. At the very least, free must release a multiple of 4 or 8
(or possibly 16) bytes, and there is also overhead from the data
structure used by the free list. And presumably one reason someone
might ask this question is to find out what the system overhead
actually is.

Obviously you did not read my reply very carefully. To make it easy
for you, here it is again:

"Yes, it freed exactly as many bytes, no more and no less, as were
allocated by the call to an allocation function (malloc(), calloc(),
or realloc()) that supplied the pointer value. Unless the pointer
value is NULL, in which case free() frees exactly 0 bytes, that value
was at least as many bytes as you asked for when you called that
allocation function."

You will notice that I did not say that free releases the same amount
of memory as the size of the malloc request. I said it releases as
much memory as it allocated, which is *** at least *** as much as was
requested if the allocation was successful.

And no, there is neither guarantee nor requirement that free() must
release a multiple of some number of bytes, it releases exactly as
much as was allocated. Some implementations might only allocate in
blocks that are multiples of some number of bytes, which is an
entirely different story.

As for a "data structure" and "the free list", neither of these things
is defined by the standard, or even required.

One of the reasons that such things are not defined by the C standard
is that there are many ways for the implementation to provide them,
some of them quite different from those you might happen to be
familiar with.

As for speculation about the OP wanting to know about the overhead, if
any, of memory allocation on his system, there is certainly nothing in
the wording of his post to suggest that. But if that is what he's
looking for, he's still asking in the wrong place. He would need to
ask in a group that supports his particular compiler/OS combination,
which might be completely different from you particular compiler/OS
combination.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
.



Relevant Pages

  • Re: C++ vs Java "new" (no flame war please!)
    ... There is no memory overhead for each class. ... overhead of the Java allocation and copy would be much less than for the ... In Java, it would be read into a new buffer each time, the allocation ...
    (comp.lang.java)
  • Re: How to release heap memory that is marked as free
    ... As I said, fragmentation is a very serious problem, and one of the most serious problems ... my allocator was accused of using massive amounts of memory. ... I'm going to have to re-think the memory allocation that I'm ... process's 'working set'. ...
    (microsoft.public.vc.mfc)
  • Re: [PATCH 00/28] Swap over NFS -v16
    ... memory they can consume. ... So we need the extra (skb) ... included in the reserve? ... if the allocation had to dip into emergency reserves, ...
    (Linux-Kernel)
  • Re: Memory leak with CAsyncSocket::Create
    ... read my essay on how storage allocators work. ... Create method is consuming system memory that is not released back to ... The memory consumption is either shown as "Mem Usage" on the Task ... many levels of allocation going ...
    (microsoft.public.vc.mfc)
  • Re: OT: C++ overloading operators
    ... use of memory, at the cost of a somewhat slower execution. ... dynamic allocation, no matter how many "clever tricks" are used... ... I don't think you will find many programmers outside the ... "big" number and very generous overhead allocation. ...
    (comp.dsp)