Re: Memory alloc/dealloc performance

From: Tom (tom_usenet_at_hotmail.com)
Date: 03/05/04


Date: 5 Mar 2004 02:30:12 -0800


"Stephen Walch" <swalch@proposion.com> wrote in message news:<eUtYKxeAEHA.3352@TK2MSFTNGP09.phx.gbl>...
> I did some initial performance testing of my VC++ app and found that a
> pretty significant amount of time is spent in functions like this:
>
> buff = new char[buffsize];
> ...
> delete [] temp;
>
> These allocations occurs hundreds of thousands of times in a simple run and,
> unfortunately, the size is quite variable (typically from 10 to 10,000).
>
> I know that performance tuning is a big topic, but any quick suggestions as
> to how to approach optimizing a case like this? Or pointers to some good
> memory management tutorials?

The biggest performance improvement is going to come from reducing the
number of calls to new. Reuse memory (using std::vector perhaps), and
you should find a huge speed improvement. Tuning the memory allocator
itself is much more effort for much less gain...

For example, never do:

for(int i = 0; i < v.size(); ++i)
{
  char* buffer = new char[v[i].size()];
  //do some processing etc.
  delete[] buffer;
}

but instead do:

vector<char> buffer;
for (int i = 0; i < v.size(); ++i)
{
  buffer.resize(v[i].size()); //won't reallocate unless it has to.
  //do some processing etc.
}

By removing memory allocations from inner loops, it is usually
possible to increase execution speed by many orders of magnitude. Use
a profiler to find the most time-consuming ones.

>From an allocator point of view, you can optimize the smaller
allocations using some kind of memory pool. A good one can be found at
www.boost.org (the Pool library).

Tom



Relevant Pages

  • Re: Problem with linked list
    ... !pool and see what piece of memory has ... allocated (thankfully you tag your allocations). ... > The buffer is filled the following way: ... >> what's in the section of MYSTRUCT (which you don't appear to have ...
    (microsoft.public.development.device.drivers)
  • RE: 2003 SBS stalling randomly
    ... A memory leak occurs in an application using the Volume Shadow Copy Service ... Poolmon displays data that the ... The data is grouped by pool allocation tag. ... Press P twice to display allocations from only the paged pool. ...
    (microsoft.public.windows.server.sbs)
  • Re: xmalloc string functions
    ... than a NULL return from malloc(). ... pointer value to null at the point I want to trigger the failure. ... I've also had VMWare report out-of-resource at times when the only resource that was tight was memory, and again it gave me the chance to recover the situation which saved me significant work because I had two VMs running and the state between them was important and took time setting up. ... allocations without reference to other circumstances (number of ...
    (comp.lang.c)
  • Re: System pretends to be out of memory when it isnt
    ... With 1Gb of memory I can usually copy dozens of photos ... >>> to analyze Windows heap allocations for a specific process. ... >>> discusses how you can use UMDH to help locate memory leak problems. ... the maximum stack trace depth is 16. ...
    (microsoft.public.windowsxp.general)
  • Re: xmalloc string functions
    ... than a NULL return from malloc(). ... pointer value to null at the point I want to trigger the failure. ... There are about five bazillion allocations, ... Memory is quite a different kind of resource. ...
    (comp.lang.c)

Quantcast