C ++ new operator takes a lot of memory (67MB) via libstdC ++

I have some problems with the new operator in libstdc++ . I wrote a C ++ program and had some problems with memory management.

After debugging with gdb to determine if my ram was riding, I got the following for info proc mappings

 Mapped address spaces: Start Addr End Addr Size Offset objfile 0x400000 0x404000 0x4000 0 /home/sebastian/Developement/powerserverplus-svn/psp-job-distributor/Release/psp-job-distributor 0x604000 0x605000 0x1000 0x4000 /home/sebastian/Developement/powerserverplus-svn/psp-job-distributor/Release/psp-job-distributor 0x605000 0x626000 0x21000 0 [heap] 0x7ffff0000000 0x7ffff0021000 0x21000 0 0x7ffff0021000 0x7ffff4000000 0x3fdf000 0 0x7ffff6c7f000 0x7ffff6c80000 0x1000 0 0x7ffff6c80000 0x7ffff6c83000 0x3000 0 0x7ffff6c83000 0x7ffff6c84000 0x1000 0 0x7ffff6c84000 0x7ffff6c87000 0x3000 0 0x7ffff6c87000 0x7ffff6c88000 0x1000 0 0x7ffff6c88000 0x7ffff6c8b000 0x3000 0 0x7ffff6c8b000 0x7ffff6c8c000 0x1000 0 0x7ffff6c8c000 0x7ffff6c8f000 0x3000 0 0x7ffff6c8f000 0x7ffff6e0f000 0x180000 0 /lib/x86_64-linux-gnu/libc-2.13.so 0x7ffff6e0f000 0x7ffff700f000 0x200000 0x180000 /lib/x86_64-linux-gnu/libc-2.13.so 0x7ffff700f000 0x7ffff7013000 0x4000 0x180000 /lib/x86_64-linux-gnu/libc-2.13.so 0x7ffff7013000 0x7ffff7014000 0x1000 0x184000 /lib/x86_64-linux-gnu/libc-2.13.so 

It just burst out of him. However, everything is fine. Some of them relate to the code for standard libraries, and some relate to the heap, and some relate to the sections of the stack for the created threads.

But. there is one section i id that does not determine why it stands out:

  0x7ffff0000000 0x7ffff0021000 0x21000 0 0x7ffff0021000 0x7ffff4000000 0x3fdf000 0 

These two sections are created at random random times. There are several hours of debugging with no similarities in time or in a specific thread created or so. I set a breakpoint with awatch *0x7ffff0000000 and again gave it a few run .

These two sections are created almost at the same time in the same code section of a non-debugged function (gdb shows it on the stack as in ?? () from /lib/x86_64-linux-gnu/libc.so.6 ). More precisely, this is the approximate stack in which it occurred:

 #0 0x00007ffff6d091d5 in ?? () from /lib/x86_64-linux-gnu/libc.so.6 #1 0x00007ffff6d0b2bd in calloc () from /lib/x86_64-linux-gnu/libc.so.6 #2 0x00007ffff7dee28f in _dl_allocate_tls () from /lib64/ld-linux-x86-64.so.2 #3 0x00007ffff77c0484 in pthread_create@ @GLIBC_2.2.5 () from /lib/x86_64-linux-gnu/libpthread.so.0 #4 0x00007ffff79d670e in Thread::start (this=0x6077c0) at ../src/Thread.cpp:42 #5 0x000000000040193d in MultiThreadedServer<JobDistributionServer_Thread>::Main (this=0x7fffffffe170) at /home/sebastian/Developement/powerserverplus-svn/mtserversock/src/MultiThreadedServer.hpp:55 #6 0x0000000000401601 in main (argc=1, argv=0x7fffffffe298) at ../src/main.cpp:29 

Another example would be here (from a different launch):

 #0 0x00007ffff6d091d5 in ?? () from /lib/x86_64-linux-gnu/libc.so.6 #1 0x00007ffff6d0bc2d in malloc () from /lib/x86_64-linux-gnu/libc.so.6 #2 0x00007ffff751607d in operator new(unsigned long) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 #3 0x000000000040191b in MultiThreadedServer<JobDistributionServer_Thread>::Main (this=0x7fffffffe170) at /home/sebastian/Developement/powerserverplus-svn/mtserversock/src/MultiThreadedServer.hpp:53 #4 0x0000000000401601 in main (argc=1, argv=0x7fffffffe298) at ../src/main.cpp:29 

All of this suggests that it occurs in a calloc called from pthread lib, or in another situation it caused a new operator or malloc from it. It doesn’t matter in which new it is - in several runs it happened with almost every new or creating threads in my code. The only "constant" with it is that it happens every time in libc.so.6 .

It doesn't matter at what point in the code,
it doesn't matter if used with malloc or calloc,
it doesn’t matter after how long the program started,
no matter after how many threads are created,
it's always that part: 0x7ffff0000000 - 0x7ffff4000000.

Every time the program starts. But every time at a different moment in the program. I'm really confused because he allocated 67 MB of virtual space, but he does not use it . When looking at the variables created there, especially the observed ones that are created when malloc or calloc were called by libc, none of them are used by them. They are created in the heap section, which is far from this address range (0x7ffff0000000 - 0x7ffff4000000).


Edit:

I also checked the stack size of the parent process and got the use of bytes 8388608, which is 0x800000 (~ 8 MB). To get these values, I did:

 pthread_attr_t attr; size_t stacksize; struct rlimit rlim; pthread_attr_init(&attr); pthread_attr_getstacksize(&attr, &stacksize); getrlimit(RLIMIT_STACK, &rlim); fit into a size_t variable. */ printf("Resource limit: %zd\n", (size_t) rlim.rlim_cur); printf("Stacksize: %zd\n", stacksize); pthread_attr_destroy(&attr); 

Please help me with this. I am really confused by this.

+6
source share
1 answer

It looks like it is allocating stack space for the stream.
Space will be used when making function calls in the stream.

But really, what he does is none of your business. It is part of the internal implementation of pthread_create() , it can do whatever it likes.

+4
source

All Articles