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() {
}
~tst() {
}
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, " " ( )
tominko