Std :: string memory management

I have a problem managing memory using std :: string.

I have an application - a multi-threaded server with separate threads (I need to join them, they will perform the task and exit), and I found that after a while the memory usage becomes quite high. I started experimenting where the problem is, and I created a test program that demonstrates the problem.

#include <iostream>

#include <string>
#include <pthread.h>

pthread_t           thread[100];

using namespace std;

class tst {
    public:
        tst() {
            //cout << "~ Create" << endl;
        }
        ~tst() {
            //cout << "~ Delete" << endl;
        }
        void calc() {
            string TTT;
            for (int ii=0; ii<100000; ii++) {
                TTT+="abcdenbsdmnbfsmdnfbmsndbfmsndb ";
            }
        }
};

void *testThread (void *arg) {
    int cnt=*(int *) arg;
    cout << cnt << " ";
    tst *TEST=new tst;
    TEST->calc();
    delete TEST;
    pthread_exit((void *)0);
}

int main (int argc, char * const argv[]) {

cout << "---------------------------------------------------" << endl;
sleep(5);

    for (int oo=0; oo<100; oo++) {
        pthread_create(&thread[oo], NULL, testThread, &oo);
        pthread_detach(thread[oo]);
    }
    cout << endl;
    cout << "---------------------------------------------------" << endl;

    sleep(5);

    for (int oo=0; oo<100; oo++) {
        pthread_create(&thread[oo], NULL, testThread, &oo);
        pthread_detach(thread[oo]);
    }
    cout << endl;
    cout << "---------------------------------------------------" << endl;

    sleep(5);
    exit(0);
}

after the first “---”, the memory usage is 364 KB, after the second - 19 MB, after the third - 33.5 MB. There is another strange thing: each run shows a different memory usage, but basically the last memory usage is about 50% more than after the second “---”.

, TEST (tst) , - , - tst, , .

, , , - (

- , "" ?

TTT = "" TTT.clear() - .

... - multicpu, " " ( )

+5
5

, ​​ calc(), calc. tst , .

, , 200 . , , 100 , 100.

, , , , . , 256- . , 256- . , .

, :

  • .reserve(your_largest_expected_string_size) . , .
  • . .
  • - jemalloc tcmalloc.
  • , , , , . . ( STL) .
+6

, , , . , 200 , . std::string, , , , , .

, , : , reserve(). (31 * 100 000) + 1 , calc() :

string TTT;

// We know how big the string will get, so pre-alloc memory for it to avoid the 
// inefficiencies of the default allocation strategy as the string grows.

TTT.reserve(3100001);

for (int ii=0; ii<100000; ii++) {
    TTT+="abcdenbsdmnbfsmdnfbmsndbfmsndb ";
}

, . Valgrind , 1,5 ; 620 .

, Valgrind . , , : Valgrind home.

+5

, calc - .

? , , . , , : http://hashpling.org/asm/

pthreads, , - , API, .

+2

( free() malloc()) /. . ​​ , , .

+2

"threaded-" - if (1) {..} , - , , ( if block) 'int cnt'

void *testThread (void *arg) {
    if (1) {
        int cnt=*(int *) arg;
        cout << cnt << " ";
        tst *TEST=new tst;
        TEST->calc();
        delete TEST;
    }
    pthread_exit((void *)0);
}

... ,

0
source

All Articles