This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Counting amount of memory allocated?


> The heap size numbers are computed using mallinfo, as suggested by Roland
> McGrath.  They are sampled to determine the amount of memory used to
> represent the analysis results for the various analyses run.  The only
> thing I remain concerned about is whether using STL containers will
> artificially inflate the numbers that I'm seeing.  If I use STL maps, for
> example, aren't the tree nodes pool allocated?

Ok, sorry to spam the list with a response to my own question, but yes,
pool allocation (or something) is going on, skewing the results.  Running
the following program gives me the following output:

Start: 128
One map: 251200
empty map: 251200
second map: 251200
map destroyed: 251200

I'd like to see the "empty map" and "map destroyed" lines be back to 128
or so, but I assume pool allocation is getting in the way here.  I tried
recompiling this test program with -D__USE_MALLOC, but I get the following
error:

$ g++ test.cpp -D__USE_MALLOC
In file included from .../gcc-3.2/include/c++/3.2/bits/stl_algobase.h:64,
                 from .../gcc-3.2/include/c++/3.2/bits/stl_tree.h:86,
                 from .../gcc-3.2/include/c++/3.2/map:66,
                 from test.cpp:3:
.../gcc-3.2/include/c++/3.2/i686-pc-linux-gnu/bits/c++config.h:74:2: #error __USE_MALLOC should only be defined within libstdc++-v3/include/bits/c++config before full recompilation of the library.

How do I go about disabling pool allocation that is happening for these
containers?  Ideally I would like to be able to disable it globally with a
single #define (such as -D__USE_MALLOC), so that I can just enable it for
debug builds.  Do I really have to recompile libstdc++ to do this, or is
there an easier way?

-Chris

Test program:

#include <malloc.h>
#include <map>
#include <iostream>

int main () {
  struct mallinfo MI = mallinfo();
  std::cerr << "Start: " << MI.uordblks << "\n";

  {
  std::map<int, float> M;
  for (unsigned i = 0; i != 10000; ++i)
    M[i] = i;

  MI = mallinfo();
  std::cerr << "One map: " << MI.uordblks << "\n";
  M.clear();

  MI = mallinfo();
  std::cerr << "empty map: " << MI.uordblks << "\n";
  for (unsigned i = 0; i != 10000; ++i)
    M[-i] = i;

  MI = mallinfo();
  std::cerr << "second map: " << MI.uordblks << "\n";
  }

  MI = mallinfo();
  std::cerr << "map destroyed: " << MI.uordblks << "\n";
}



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]