f



how much does free free?

Hi,
Is there a way of knowing how much memory a call to free() actually
freed?
thanks,
Wojtek

0
4/28/2007 2:52:30 AM
comp.lang.c 30657 articles. 4 followers. spinoza1111 (3246) is leader. Post Follow

13 Replies
440 Views

Similar Articles

[PageSpeed] 30

wojtek.fedorko@gmail.com wrote:
> Hi,
> Is there a way of knowing how much memory a call to free() actually
> freed?

Yes, keep track of how big the object you allocated and freed was.
There isn't any other way.

-- 
Ian Collins.
0
ian-news (10155)
4/28/2007 2:56:37 AM
wojtek.fedo...@gmail.com 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.)

from malloc.h:

struct mstats {
    size_t      bytes_total;
    size_t      chunks_used;
    size_t      bytes_used;
    size_t      chunks_free;
    size_t      bytes_free;
};

extern struct mstats mstats(void);

0
pmac360 (4)
4/28/2007 3:06:23 AM
"pete m" <pmac360@yahoo.com> wrote in message 
news:1177729583.599959.256000@o5g2000hsb.googlegroups.com...
>
> wojtek.fedo...@gmail.com 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.)
>
> from malloc.h:
>
> struct mstats {
>    size_t      bytes_total;
>    size_t      chunks_used;
>    size_t      bytes_used;
>    size_t      chunks_free;
>    size_t      bytes_free;
> };
>
> extern struct mstats mstats(void);
How widespread is this undocumented function?  To be honest, it sounds like 
a practical joke.
--
WW 


0
invalid163 (956)
4/28/2007 3:25:24 AM
On Apr 27, 8:25 pm, "Wade Ward" <inva...@invalid.net> wrote:
> "pete m" <pmac...@yahoo.com> wrote in message
>
> news:1177729583.599959.256000@o5g2000hsb.googlegroups.com...
>
>
>
> > wojtek.fedo...@gmail.com 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.)
>
> > from malloc.h:
>
> > struct mstats {
> >    size_t      bytes_total;
> >    size_t      chunks_used;
> >    size_t      bytes_used;
> >    size_t      chunks_free;
> >    size_t      bytes_free;
> > };
>
> > extern struct mstats mstats(void);
>
> How widespread is this undocumented function?  To be honest, it sounds like
> a practical joke.

It's on Linux and OS X, at the very least.


0
pmac360 (4)
4/28/2007 3:36:23 AM
On 27 Apr 2007 20:06:23 -0700, pete m <pmac360@yahoo.com> wrote in
comp.lang.c:

> 
> wojtek.fedo...@gmail.com 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.

> from malloc.h:

C does not have a header names malloc.h, either.

If you want to talk about non-standard, platform specific extensions,
post on groups for the platform.

-- 
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
0
jackklein (3930)
4/28/2007 3:58:30 AM
On 27 Apr 2007 19:52:30 -0700, wojtek.fedorko@gmail.com wrote in
comp.lang.c:

> Hi,
> Is there a way of knowing how much memory a call to free() actually
> freed?
> thanks,
> Wojtek

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.

-- 
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
0
jackklein (3930)
4/28/2007 4:00:49 AM
On Apr 27, 8:58 pm, Jack Klein <jackkl...@spamcop.net> wrote:
> On 27 Apr 2007 20:06:23 -0700, pete m <pmac...@yahoo.com> wrote in
> comp.lang.c:
>
> > wojtek.fedo...@gmail.com 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.

0
pmac360 (4)
4/28/2007 5:14:44 AM
On 27 Apr 2007 22:14:44 -0700, pete m <pmac360@yahoo.com> wrote in
comp.lang.c:

> On Apr 27, 8:58 pm, Jack Klein <jackkl...@spamcop.net> wrote:
> > On 27 Apr 2007 20:06:23 -0700, pete m <pmac...@yahoo.com> wrote in
> > comp.lang.c:
> >
> > > wojtek.fedo...@gmail.com 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
0
jackklein (3930)
4/28/2007 5:24:12 AM
In article <feh533h42ipej4lp5h0gl1fbt9lc78sn8d@4ax.com>,
Jack Klein  <jackklein@spamcop.net> wrote:
>If you want to talk about non-standard, platform specific extensions,
>post on groups for the platform.

Apparently several platforms have the same extension.  Do you suggest
newsgroups for all combinations of platforms?

-- Richard

-- 
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
0
richard91 (3692)
4/28/2007 10:47:46 PM
In article <1177737284.046540.131250@n59g2000hsh.googlegroups.com>,
pete m  <pmac360@yahoo.com> wrote:

>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

Why is that?  On a system with no alignment constraints, malloc(1) can
allocate exactly one byte if it wishes, and will naturally free() the
same amount.

>and there is also overhead from the data structure used by the free list.

malloc() doesn't have to use a free list, though I believe most
implementations do.  It could, for example, use a bitmap.

-- Richard
-- 
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
0
richard91 (3692)
4/28/2007 10:53:05 PM
On 28 Apr 2007 22:47:46 GMT, in comp.lang.c , richard@cogsci.ed.ac.uk
(Richard Tobin) wrote:

>In article <feh533h42ipej4lp5h0gl1fbt9lc78sn8d@4ax.com>,
>Jack Klein  <jackklein@spamcop.net> wrote:
>>If you want to talk about non-standard, platform specific extensions,
>>post on groups for the platform.
>
>Apparently several platforms have the same extension.  Do you suggest
>newsgroups for all combinations of platforms?

Two possibilities:

1) the different platforms have different extensions with the same
name. In  this case, its /vital/ to post in the right platform
specific group.

2) the different platforms have an identical extension. In this case
it doesn't matter which platform-specific group one posts to. 

Either way, one assumes that the OP (and you) can 'engage your brain'
before posting? 
-- 
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place. 
 Therefore, if you write the code as cleverly as possible, you are, 
 by definition, not smart enough to debug it."
--Brian Kernighan
0
markmcintyre (4555)
4/30/2007 10:29:15 PM
Mark McIntyre wrote, On 30/04/07 23:29:
> On 28 Apr 2007 22:47:46 GMT, in comp.lang.c , richard@cogsci.ed.ac.uk
> (Richard Tobin) wrote:
> 
>> In article <feh533h42ipej4lp5h0gl1fbt9lc78sn8d@4ax.com>,
>> Jack Klein  <jackklein@spamcop.net> wrote:
>>> If you want to talk about non-standard, platform specific extensions,
>>> post on groups for the platform.
>> Apparently several platforms have the same extension.  Do you suggest
>> newsgroups for all combinations of platforms?
> 
> Two possibilities:
> 
> 1) the different platforms have different extensions with the same
> name. In  this case, its /vital/ to post in the right platform
> specific group.
> 
> 2) the different platforms have an identical extension. In this case
> it doesn't matter which platform-specific group one posts to. 

You forgot the third possibility, there is a group of platforms 
conforming to some additional standard, in which case post to a group 
that deal with all platforms conforming to that standard. I'm thinking 
of comp.unix.programmer here as opposed to, for example, an AIX specific 
group.

> Either way, one assumes that the OP (and you) can 'engage your brain'
> before posting? 

Agreed.
-- 
Flash Gordon
0
spam331 (4048)
5/1/2007 7:02:34 AM
On Fri, 27 Apr 2007 23:25:24 -0400, "Wade Ward" <invalid@invalid.net> wrote:
>"pete m" <pmac360@yahoo.com> wrote in message 
>news:1177729583.599959.256000@o5g2000hsb.googlegroups.com...
>> wojtek.fedo...@gmail.com wrote:
>>> Hi,
>>> Is there a way of knowing how much memory a call to free() actually
>>> freed?
>>
>> 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.)
>>
>> from malloc.h:
>>
>> struct mstats {
>>    size_t      bytes_total;
>>    size_t      chunks_used;
>>    size_t      bytes_used;
>>    size_t      chunks_free;
>>    size_t      bytes_free;
>> };
>>
>> extern struct mstats mstats(void);
>
> How widespread is this undocumented function?  To be honest, it sounds
> like a practical joke.

Does it really matter?  There is no "malloc.h" in the standard, so this
is not a question that is valid in the context of c.l.c.

For example, it can't be very widespread, since two of the Unix(R)
systems I often use for development work do not have mstats() or a
'struct mstats' definition in "malloc.h".  Even more importantly, there
are dozens of malloc/free implementations out there, and I highly doubt
all of them use the simple model of "chunks" and "bytes" hinted at
above.

0
keramida (464)
5/2/2007 1:53:10 AM
Reply: