This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
mt_allocator allocates excessively
- From: David Weidenkopf <dweidenkopf at cococorp dot com>
- To: "libstdc++ at gcc dot gnu dot org" <libstdc++ at gcc dot gnu dot org>
- Date: Fri, 18 Apr 2008 09:48:46 -0700
- Subject: mt_allocator allocates excessively
- Accept-language: en-US
- Acceptlanguage: en-US
The gist of the problem we are having is that our application is consuming way too much memory. Using valgrind/massif, I saw that __gnu_cxx::__mt_alloc was allocating 32,768 or 81,940 bytes when the desired allocation is in the hundreds of bytes. I then wrote a test program to demonstrate the behavior, the source is below. Of course, using the environment variable GLIBCXX_FORCE_NEW I was able to get memory consumption back to a reasonable level. I suspect something is wrong with our toolchain or its configuration. Further, I think it may have something to do with pthreads. Can anyone point me to docs that describe the proper way to enable pthread support for libstdc++? The g++ manual says to use -pthread on various platforms, though not i386 et.,al. We are using it and g++ is not complaining. I appreciate any help.
Test code:
----------
#include <iostream>
#include <exception>
#include <vector>
int main(int , char* )
{
try
{
std::vector<char*> ptrs;
while( true )
{
char* pbuf = new char[100];
ptrs.push_back(pbuf);
sleep(1);
}
}
catch(const std::exception& e)
{
std::cerr << e.what() << std::endl;
return 1;
}
}
Valgrind/massif output sample from above code:
----------------------------
99.67% (305,702B) (heap allocation functions) malloc/new/new[], --alloc-fns, etc.
->26.71% (81,940B) 0x80490B5: __gnu_cxx::__mt_alloc<char*>::_S_initialize() (in /opt/coco/sbin/memTest.clothed)
| ->26.71% (81,940B) 0x42B083A: pthread_once (in /lib/tls/i686/cmov/libpthread-2.6.1.so)
| ->26.71% (81,940B) 0x8049455: std::vector<char*, std::allocator<char*> >::_M_insert_aux(__gnu_cxx::__normal_iterator<char**, std::vector<char*, std::allocator<char*> > >, char* const&) (in /opt/coco/sbin/memTest.clothed)
| ->26.71% (81,940B) 0x8048BB3: main (in /opt/coco/sbin/memTest.clothed)
|
->26.71% (81,940B) 0x8049099: __gnu_cxx::__mt_alloc<char*>::_S_initialize() (in /opt/coco/sbin/memTest.clothed)
| ->26.71% (81,940B) 0x42B083A: pthread_once (in /lib/tls/i686/cmov/libpthread-2.6.1.so)
| ->26.71% (81,940B) 0x8049455: std::vector<char*, std::allocator<char*> >::_M_insert_aux(__gnu_cxx::__normal_iterator<char**, std::vector<char*, std::allocator<char*> > >, char* const&) (in /opt/coco/sbin/memTest.clothed)
| ->26.71% (81,940B) 0x8048BB3: main (in /opt/coco/sbin/memTest.clothed)
|
->26.71% (81,940B) 0x80490A5: __gnu_cxx::__mt_alloc<char*>::_S_initialize() (in /opt/coco/sbin/memTest.clothed)
| ->26.71% (81,940B) 0x42B083A: pthread_once (in /lib/tls/i686/cmov/libpthread-2.6.1.so)
| ->26.71% (81,940B) 0x8049455: std::vector<char*, std::allocator<char*> >::_M_insert_aux(__gnu_cxx::__normal_iterator<char**, std::vector<char*, std::allocator<char*> > >, char* const&) (in /opt/coco/sbin/memTest.clothed)
| ->26.71% (81,940B) 0x8048BB3: main (in /opt/coco/sbin/memTest.clothed)
|
->10.68% (32,768B) 0x8048FF1: __gnu_cxx::__mt_alloc<char*>::_S_initialize() (in /opt/coco/sbin/memTest.clothed)
| ->10.68% (32,768B) 0x42B083A: pthread_once (in /lib/tls/i686/cmov/libpthread-2.6.1.so)
| ->10.68% (32,768B) 0x8049455: std::vector<char*, std::allocator<char*> >::_M_insert_aux(__gnu_cxx::__normal_iterator<char**, std::vector<char*, std::allocator<char*> > >, char* const&) (in /opt/coco/sbin/memTest.clothed)
| ->10.68% (32,768B) 0x8048BB3: main (in /opt/coco/sbin/memTest.clothed)
Uname -a
------------
Linux linbuild02 2.6.18-4-686 #1 SMP Mon Mar 26 17:17:36 UTC 2007 i686 GNU/Linux
./i486-linux-g++-3.4 -v
-----------------------------
Reading specs from /home/build/build/trunk/chroot/usr/bin/../lib/gcc/i486-linux/3.4.4/specs
Configured with: ../src/configure -v --enable-languages=c,c++,java,f77,pascal,objc,ada,treelang --prefix=/usr --libexecdir=/usr/lib --with-gxx-include-dir=/usr/include/c++/3.4 --enable-shared --with-system-zlib --enable-nls --without-included-gettext --program-suffix=-3.4 --enable-__cxa_atexit --enable-libstdcxx-allocator=mt --enable-clocale=gnu --enable-libstdcxx-debug --enable-java-gc=boehm --enable-java-awt=gtk --disable-werror i486-linux
Thread model: posix
gcc version 3.4.4 20050314 (prerelease) (Debian 3.4.3-13sarge1)
build output
---------------
linux_x86_release | memTest | i486-linux-g++-3.4 ['alloc.cpp'] -> ['alloc.o']
['i486-linux-g++-3.4',
'-o',
'protocol/modules/memTest/src/alloc.o',
'-c',
'-pipe',
'-Wall',
'-Winvalid-pch',
'-Wno-multichar',
'-Wno-unused',
'-march=i586',
'-pthread',
'-I/tonka2/external/trunk/all/boost-1.33.1',
'-I/tonka2/external/trunk/linux_x86/usr/include',
'-O3',
'-DP_LINUX',
'-DLINUX',
'-DENDIAN=LITTLE',
'-DLINUX86',
'-DLINUX_X86',
'-DBOOST_ALL_NO_LIB',
'-DUSE_PCH',
'-D_GLIBCXX_PTHREADS',
'-DNDEBUG',
'-Iinclude',
'-I/tonka2/datastore/depot.coco.dev/input/dweidenkopf/extern/include',
'-I/tonka2/datastore/depot.coco.dev/input/dweidenkopf/extern/protocol/include',
'-I/tonka2/datastore/depot.coco.dev/input/dweidenkopf/extern/shared/include',
'protocol/modules/memTest/src/alloc.cpp']
| linux_x86_release | memTest | i486-linux-g++-3.4 ['alloc.o', 'memTest_version.o'] -> ['memTest.
clothed']
['i486-linux-g++-3.4',
'-L/tonka2/external/trunk/linux_x86/usr/lib',
'-L/usr/lib',
'-L/lib',
'-Wl,-rpath-link,/usr/lib',
'-Wl,-rpath-link,/lib',
'-L/tonka2/external/trunk/linux_x86/usr/lib/release',
'-o',
'protocol/modules/memTest/memTest.clothed',
'protocol/modules/memTest/src/alloc.o',
'protocol/modules/memTest/memTest_version.o',
'-Llib',
'-L/tonka2/datastore/depot.coco.dev/input/dweidenkopf/extern/lib',
'-L/tonka2/datastore/depot.coco.dev/output/dweidenkopf/extern/linux_x86_release/bin',
'-Wl,--start-group',
'-lrt',
'-Wl,--end-group',
'-Wl,--whole-archive',
'-Wl,--no-whole-archive']
| linux_x86_release | memTest | strip ['memTest.clothed'] -> ['memTest']
['strip',
'protocol/modules/memTest/memTest.clothed',
'-o',
'protocol/modules/memTest/memTest']
| linux_x86_release | memTest | Install ['memTest'] -> ['memTest']
['Install',
'file:',
'"protocol/modules/memTest/memTest"',
'as',
'"/tonka2/datastore/depot.coco.dev/output/dweidenkopf/extern/linux_x86_release/bin/memTest"']
| linux_x86_release | memTest | Install ['memTest.clothed'] -> ['memTest.clothed']
['Install',
'file:',
'"protocol/modules/memTest/memTest.clothed"',
'as',
'"/tonka2/datastore/depot.coco.dev/output/dweidenkopf/extern/linux_x86_release/bin/memTest.clothed"
']