473,471 Members | 1,684 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

free() performance question

Hi,
In my code, I need to concat two buffer into one. I take following
steps:
1 malloc a new space
2 copy two buffers over,
3. and then free the originial two buffers.
I put timer on each step. And I found that under some stress
condition(cpu intensive, lots of memory used), the last step took a
lot more time than step 1 and 2. Did anyone know what could be the
reason?

Jun 27 '07 #1
18 1444
Lianjun Jiang <li******@gmail.comwrites:
In my code, I need to concat two buffer into one. I take following
steps:
1 malloc a new space
2 copy two buffers over,
3. and then free the originial two buffers.
I put timer on each step. And I found that under some stress
condition(cpu intensive, lots of memory used), the last step took a
lot more time than step 1 and 2. Did anyone know what could be the
reason?
It's extremely system-specific.

The C standard says absolutely nothing about the performance of malloc
and free (or of anything else, for that matter).

malloc and free have to manipulate whatever internal data structures
the implementation uses for memory allocation. It's possible that
malloc() merely has to find the first available free chunk of at least
the requested size, but free() has to do considerable work to
rearrange things (merging adjacent free chunks, updating statistics,
whatever) for future allocations. But that's pure speculation on my
part.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '07 #2
On 28 Jun, 00:03, Lianjun Jiang <lianj...@gmail.comwrote:
Hi,
In my code, I need to concat two buffer into one. I take following
steps:
1 malloc a new space
2 copy two buffers over,
3. and then free the originial two buffers.
I put timer on each step. And I found that under some stress
condition(cpu intensive, lots of memory used), the last step took a
lot more time than step 1 and 2. Did anyone know what could be the
reason?
I have no idea why free takes a long time but one suggestion
is that you use realloc to make the first buffer large
enough to also fit the second buffer , then add the
second buffer to the end of the first and free the second.
Perhaps this will run faster.

Are you sure that the timer you're using is reliable ?

Jun 28 '07 #3
On Jun 27, 4:58 pm, Spiros Bousbouras <spi...@gmail.comwrote:
On 28 Jun, 00:03, Lianjun Jiang <lianj...@gmail.comwrote:
Hi,
In my code, I need to concat two buffer into one. I take following
steps:
1 malloc a new space
2 copy two buffers over,
3. and then free the originial two buffers.
I put timer on each step. And I found that under some stress
condition(cpu intensive, lots of memory used), the last step took a
lot more time than step 1 and 2. Did anyone know what could be the
reason?

I have no idea why free takes a long time but one suggestion
is that you use realloc to make the first buffer large
enough to also fit the second buffer , then add the
second buffer to the end of the first and free the second.
Perhaps this will run faster.

Are you sure that the timer you're using is reliable ?
At first, my implementation is using realloc, and then realloc takes
similar time as free. I think that is because realloc need one free
call, too. So I used current approach, and kind of verify it is the
free that slow down realloc.

For the timers, it at least provides very consistent result.

Jun 28 '07 #4
On Jun 27, 4:03 pm, Lianjun Jiang <lianj...@gmail.comwrote:
In my code, I need to concat two buffer into one. I take following
steps:
1 malloc a new space
2 copy two buffers over,
3. and then free the originial two buffers.
I put timer on each step. And I found that under some stress
condition(cpu intensive, lots of memory used), the last step took a
lot more time than step 1 and 2. Did anyone know what could be the
reason?
In balanced heap implementations, the cost of free() and malloc()
should be *roughly* the same. free() needs to merge the memory with
adjacent free buffers if possible, and then update the new free
buffers relative to whatever structures they exist in to assist the
malloc() function to find them quickly.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Jun 28 '07 #5
Lianjun Jiang wrote:
>
In my code, I need to concat two buffer into one. I take following
steps:
1 malloc a new space
2 copy two buffers over,
3. and then free the originial two buffers.
I put timer on each step. And I found that under some stress
condition(cpu intensive, lots of memory used), the last step took
a lot more time than step 1 and 2. Did anyone know what could be
the reason?
Yes. It usually depends on the number of malloced pieces in effect
at the time and the effort required to detect and join them. I
wrote nmalloc to avoid it (and a few other problems). See:

<http://cbfalconer.home.att.net/download/>

for an (almost) standard C implementation for DJGPP and others.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net
--
Posted via a free Usenet account from http://www.teranews.com

Jun 28 '07 #6
we******@gmail.com wrote:
Lianjun Jiang <lianj...@gmail.comwrote:
>In my code, I need to concat two buffer into one. I take following
steps:
1 malloc a new space
2 copy two buffers over,
3. and then free the originial two buffers.
I put timer on each step. And I found that under some stress
condition(cpu intensive, lots of memory used), the last step took a
lot more time than step 1 and 2. Did anyone know what could be the
reason?

In balanced heap implementations, the cost of free() and malloc()
should be *roughly* the same. free() needs to merge the memory with
adjacent free buffers if possible, and then update the new free
buffers relative to whatever structures they exist in to assist the
malloc() function to find them quickly.
I don't know what you mean by 'balanced heap', but in general the
cause is the excessive searching for adjacent blocks to merge
during the free operation. This is often an O(n) operation. In
nmalloc, it is cut to O(1).

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net
--
Posted via a free Usenet account from http://www.teranews.com

Jun 28 '07 #7

"CBFalconer" <cb********@yahoo.comwrote in message
news:46***************@yahoo.com...
Lianjun Jiang wrote:
>>
In my code, I need to concat two buffer into one. I take following
steps:
1 malloc a new space
2 copy two buffers over,
3. and then free the originial two buffers.
I put timer on each step. And I found that under some stress
condition(cpu intensive, lots of memory used), the last step took
a lot more time than step 1 and 2. Did anyone know what could be
the reason?

Yes. It usually depends on the number of malloced pieces in effect
at the time and the effort required to detect and join them. I
wrote nmalloc to avoid it (and a few other problems). See:

<http://cbfalconer.home.att.net/download/>

for an (almost) standard C implementation for DJGPP and others.
Although in most cases I agree with you, how do you find an
almost because I wrote it, appropriate for clc?
Jun 28 '07 #8
Barry wrote:
"CBFalconer" <cb********@yahoo.comwrote in message
news:46***************@yahoo.com...
>Lianjun Jiang wrote:
>>In my code, I need to concat two buffer into one. I take following
steps:
1 malloc a new space
2 copy two buffers over,
3. and then free the originial two buffers.
I put timer on each step. And I found that under some stress
condition(cpu intensive, lots of memory used), the last step took
a lot more time than step 1 and 2. Did anyone know what could be
the reason?
Yes. It usually depends on the number of malloced pieces in effect
at the time and the effort required to detect and join them. I
wrote nmalloc to avoid it (and a few other problems). See:

<http://cbfalconer.home.att.net/download/>

for an (almost) standard C implementation for DJGPP and others.

Although in most cases I agree with you, how do you find an
almost because I wrote it, appropriate for clc?

Can you explain what's wrong with the message?
Or you think it is forbidden to speak about what one's writes
in this newsgroup?

jacob
Jun 28 '07 #9

"jacob navia" <ja***@jacob.remcomp.frwrote in message
news:46**********************@news.orange.fr...
Barry wrote:
>"CBFalconer" <cb********@yahoo.comwrote in message
news:46***************@yahoo.com...
>>Lianjun Jiang wrote:
In my code, I need to concat two buffer into one. I take following
steps:
1 malloc a new space
2 copy two buffers over,
3. and then free the originial two buffers.
I put timer on each step. And I found that under some stress
condition(cpu intensive, lots of memory used), the last step took
a lot more time than step 1 and 2. Did anyone know what could be
the reason?
Yes. It usually depends on the number of malloced pieces in effect
at the time and the effort required to detect and join them. I
wrote nmalloc to avoid it (and a few other problems). See:

<http://cbfalconer.home.att.net/download/>

for an (almost) standard C implementation for DJGPP and others.

Although in most cases I agree with you, how do you find an
almost because I wrote it, appropriate for clc?

Can you explain what's wrong with the message?
Or you think it is forbidden to speak about what one's writes
in this newsgroup?

jacob
I wrote my response in simple English. What part confuses you?
Jun 28 '07 #10
Barry wrote:
"jacob navia" <ja***@jacob.remcomp.frwrote in message
news:46**********************@news.orange.fr...
>Barry wrote:
>>"CBFalconer" <cb********@yahoo.comwrote in message
news:46***************@yahoo.com...
Lianjun Jiang wrote:
In my code, I need to concat two buffer into one. I take following
steps:
1 malloc a new space
2 copy two buffers over,
3. and then free the originial two buffers.
I put timer on each step. And I found that under some stress
condition(cpu intensive, lots of memory used), the last step took
a lot more time than step 1 and 2. Did anyone know what could be
the reason?
Yes. It usually depends on the number of malloced pieces in effect
at the time and the effort required to detect and join them. I
wrote nmalloc to avoid it (and a few other problems). See:

<http://cbfalconer.home.att.net/download/>

for an (almost) standard C implementation for DJGPP and others.

Although in most cases I agree with you, how do you find an
almost because I wrote it, appropriate for clc?
Can you explain what's wrong with the message?
Or you think it is forbidden to speak about what one's writes
in this newsgroup?

jacob

I wrote my response in simple English. What part confuses you?

You wrote:

"Although in most cases I agree with you, how do you find an
almost because I wrote it, appropriate for clc?"

Parse error. "How do you find an almost" ???
Something is missing I would say.
Jun 28 '07 #11

"jacob navia" <ja***@jacob.remcomp.frwrote in message
news:46***********************@news.orange.fr...
Barry wrote:
>"jacob navia" <ja***@jacob.remcomp.frwrote in message
news:46**********************@news.orange.fr...
>>Barry wrote:
"CBFalconer" <cb********@yahoo.comwrote in message
news:46***************@yahoo.com...
Lianjun Jiang wrote:
>In my code, I need to concat two buffer into one. I take following
>steps:
>1 malloc a new space
>2 copy two buffers over,
>3. and then free the originial two buffers.
>I put timer on each step. And I found that under some stress
>condition(cpu intensive, lots of memory used), the last step took
>a lot more time than step 1 and 2. Did anyone know what could be
>the reason?
Yes. It usually depends on the number of malloced pieces in effect
at the time and the effort required to detect and join them. I
wrote nmalloc to avoid it (and a few other problems). See:
>
<http://cbfalconer.home.att.net/download/>
>
for an (almost) standard C implementation for DJGPP and others.
>
Although in most cases I agree with you, how do you find an
almost because I wrote it, appropriate for clc?
Can you explain what's wrong with the message?
Or you think it is forbidden to speak about what one's writes
in this newsgroup?

jacob

I wrote my response in simple English. What part confuses you?

You wrote:

"Although in most cases I agree with you, how do you find an
almost because I wrote it, appropriate for clc?"

Parse error. "How do you find an almost" ???
Something is missing I would say.
"How do you find an almost," is not what it says. Had I written:
How do you find a purple polka dot elephant in a bowl of cherries?
Would you ask how do you find a purple?
Jun 28 '07 #12
In article <13*************@corp.supernews.com>,
Barry <ba****@nullhighstream.netwrote:
>>I wrote my response in simple English. What part confuses you?
>You wrote:

"Although in most cases I agree with you, how do you find an
almost because I wrote it, appropriate for clc?"

Parse error. "How do you find an almost" ???
Something is missing I would say.
>"How do you find an almost," is not what it says. Had I written:
How do you find a purple polka dot elephant in a bowl of cherries?
Would you ask how do you find a purple?
I'm a native speaker of English, and I can't make any sense of your
sentence.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Jun 28 '07 #13
ri*****@cogsci.ed.ac.uk (Richard Tobin) wrote:
In article <13*************@corp.supernews.com>,
Barry <ba****@nullhighstream.netwrote:
>I wrote my response in simple English. What part confuses you?
You wrote:

"Although in most cases I agree with you, how do you find an
almost because I wrote it, appropriate for clc?"

Parse error. "How do you find an almost" ???
Something is missing I would say.
"How do you find an almost," is not what it says. Had I written:
How do you find a purple polka dot elephant in a bowl of cherries?
Would you ask how do you find a purple?

I'm a native speaker of English, and I can't make any sense of your
sentence.
I'm not a native speaker of English, and I find that sentence completely
understandable provided it is not ripped cruelly out of context.

Richard
Jun 28 '07 #14
Barry wrote:
"CBFalconer" <cb********@yahoo.comwrote in message
.... snip ...
>>
Yes. It usually depends on the number of malloced pieces in effect
at the time and the effort required to detect and join them. I
wrote nmalloc to avoid it (and a few other problems). See:

<http://cbfalconer.home.att.net/download/>

for an (almost) standard C implementation for DJGPP and others.

Although in most cases I agree with you, how do you find an
almost because I wrote it, appropriate for clc?
Please attempt to write a fully standard general malloc package.
Can't be done. That is one reason it is part of the
implementation.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jun 28 '07 #15
Richard Tobin wrote:
Barry <ba****@nullhighstream.netwrote:
>>>I wrote my response in simple English. What part confuses you?
>>You wrote:

"Although in most cases I agree with you, how do you find an
almost because I wrote it, appropriate for clc?"

Parse error. "How do you find an almost" ???
Something is missing I would say.
>"How do you find an almost," is not what it says. Had I written:
How do you find a purple polka dot elephant in a bowl of cherries?
Would you ask how do you find a purple?

I'm a native speaker of English, and I can't make any sense of your
sentence.
I am also (a native speaker), yet I can make sense out of it. It
is an effort, but possible.

BTW someone has been grossly careless in snipping attributions for
quoted material.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jun 28 '07 #16

"CBFalconer" <cb********@yahoo.comwrote in message
news:46***************@yahoo.com...
Barry wrote:
>"CBFalconer" <cb********@yahoo.comwrote in message
... snip ...
>>>
Yes. It usually depends on the number of malloced pieces in effect
at the time and the effort required to detect and join them. I
wrote nmalloc to avoid it (and a few other problems). See:

<http://cbfalconer.home.att.net/download/>

for an (almost) standard C implementation for DJGPP and others.

Although in most cases I agree with you, how do you find an
almost because I wrote it, appropriate for clc?

Please attempt to write a fully standard general malloc package.
Can't be done. That is one reason it is part of the
implementation.
Chuck, the post was partially in gest because you are typically the
first person to point out off topic posts. I didn't expect it to be a big
deal :-).
Jun 28 '07 #17
Barry wrote:
"CBFalconer" <cb********@yahoo.comwrote in message
>Barry wrote:
>>"CBFalconer" <cb********@yahoo.comwrote in message
... snip ...
>>>>
Yes. It usually depends on the number of malloced pieces in
effect at the time and the effort required to detect and join
them. I wrote nmalloc to avoid it (and a few other problems).
See:

<http://cbfalconer.home.att.net/download/>

for an (almost) standard C implementation for DJGPP and others.

Although in most cases I agree with you, how do you find an
almost because I wrote it, appropriate for clc?

Please attempt to write a fully standard general malloc package.
Can't be done. That is one reason it is part of the
implementation.

Chuck, the post was partially in gest because you are typically the
first person to point out off topic posts. I didn't expect it to be
a big deal :-).
OK. However, I did refrain from posting the code here. :-)

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jun 29 '07 #18
On Jun 27, 7:40 pm, CBFalconer <cbfalco...@yahoo.comwrote:
websn...@gmail.com wrote:
Lianjun Jiang <lianj...@gmail.comwrote:
In my code, I need to concat two buffer into one. I take following
steps:
1 malloc a new space
2 copy two buffers over,
3. and then free the originial two buffers.
I put timer on each step. And I found that under some stress
condition(cpu intensive, lots of memory used), the last step took a
lot more time than step 1 and 2. Did anyone know what could be the
reason?
In balanced heap implementations, the cost of free() and malloc()
should be *roughly* the same. free() needs to merge the memory with
adjacent free buffers if possible, and then update the new free
buffers relative to whatever structures they exist in to assist the
malloc() function to find them quickly.

I don't know what you mean by 'balanced heap',
I mean 'balanced (heap) *implementation*'. Balanced, as in, one
operation is not penalized for the performance of another. For
example, its easy to make a super-fast malloc with a correspondingly
super-slow free and vice-versa. You can argue that the System V
implementation, for example, does favor malloc over free.
[...] but in general the
cause is the excessive searching for adjacent blocks to merge
during the free operation. This is often an O(n) operation. In
nmalloc, it is cut to O(1).
Right, but even being O(1), its not necessarily that fast. The OP is
talking real numbers, not just the theoretical stuff. Personally,
I've measured various implementations of malloc and free to be in the
100 clocks (on an Athlon CPU) kind of range (my recollection from
looking through your sources, is that you should have similar
performance), indicating that heap operations are worth minimizing.

But even in fair implementations you can see how mallocs from a fresh
heap might be fast-pathed and may be very fast when you measure it
directly under a simplistic test scenario. But the compiler developer
may have been lazy or it just might be difficult to implement a
corresponding fast-path for free (or perhaps its only fast-path if you
treat the heap like a stack, which you are unlikely to do, for
example, if you are allocating and deallocating an array of elements.)

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Jun 29 '07 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Randell D. | last post by:
Folks, I have a Javascript performance question that I might have problems explaining... In PHP, better performance can be obtained dealing directly with a variable, as opposed to an element...
115
by: Mark Shelor | last post by:
I've encountered a troublesome inconsistency in the C-language Perl extension I've written for CPAN (Digest::SHA). The problem involves the use of a static array within a performance-critical...
40
by: boris | last post by:
Hi! I'm seeking some answers about what seems to be a memory leak. I have a loop that looks much like this: double *largeArray = (double*) calloc(); for (...) { printf("iteration #...\n");...
6
by: James dean | last post by:
I am comparing performance of my own application with another microsoft product. Is there first of all any s/w that could measure the performance of a running application like Ms...
6
by: Mike | last post by:
Lets just say my app is done HOO HOO. Now, I'm accessing the database via a web service and one thing i noticed that my app is running real slow. When I first started working on the app is ran...
3
by: arne.muller | last post by:
Hello, I've read in the FAQ that modern versions of malloc/free can cache memory, i.e. free does not return memory to the OS so that it may be re-used by the next malloc call. I was thinking...
5
by: Markus Ernst | last post by:
Hello A class that composes the output of shop-related data gets some info from the main shop class. Now I wonder whether it is faster to store the info in the output class or get it from the...
5
by: toton | last post by:
Hi, I want a few of my class to overload from a base class, where the base class contains common functionality. This is to avoid repetition of code, and may be reducing amount of code in binary,...
30
by: galiorenye | last post by:
Hi, Given this code: A** ppA = new A*; A *pA = NULL; for(int i = 0; i < 10; ++i) { pA = ppA; //do something with pA
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.