Summary of Linux STL usage

Source: Internet
Author: User

Two days ago, we found that the memory usage of STL programs in Linux was relatively large. No Memory leakage was found through the valgrind check. The analysis may be related to STL, So we tested STL separately.

The program creates 10 threads and uses the separation method to create a thread every 2 seconds.

1. test new and delete

In each thread, a large new piece of memory is added and then deleted after 20 s.

Using Top to view the data column and virt column, we found that the memory increased to more than 200 MB at the beginning, and then gradually decreased with the delete memory, and eventually stabilized at 100 MB, the default thread stack of each thread in your system is 10 MB, so I think it is normal at this time. Of course, if the memory is not released, the memory cannot be downgraded.

2. Linux STL Testing

The first case: 10 threads. Each thread defines a set <string> and inserts data into the set using 10000 cycles. After 20 s, the clear function is used to clear the set.

Result: When the top column is used to view the data column and virt column, the memory will increase to around MB, and the clear column will not drop.

Case 2: place the set <string> definition and data insertion in {}, clear it, and then re-define the set to insert the same data, and the memory will not increase, fixed at 290 m.

Summary:

Even if the set in STL is destroyed, the memory will not be released. Instead, it will wait for the next re-use. The specific implementation in STL still needs to be read by STL source code.

Problem:

1. if data is inserted continuously, will the memory be exhausted?

2. If the set is used only once, it will not be reused. When will the memory be released?

3. the cache technology is available in Linux. The cache is used as part of the idle memory. However, it is found that the cache is large. The results of viewing the memory fragmentation through CAT/proc/buddyinfo are as follows:

There is basically no surplus in highmem, while normal is in use. At this time, the cache is more than 1 GB. I don't understand why this problem occurs, the memory usage is not very high (use top and PS-Aux to view the memory respectively ).

The above is a glimpse of your use process. If you have any misunderstanding, please correct it. At the same time, I hope that Daxia can solve the following problems.

The test code is as follows:

#include <iostream>#include <stdlib.h>#include <set>using namespace std;void *mallocThread(void *argv){//char *p = new char[10000000];{set<string> mSet;for (int i = 0 ; i < 100000; i++){char buf[10];sprintf(buf,"%d",i);string temp = buf;temp += "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";mSet.insert(temp);}sleep(20);cout << "clear" << endl;mSet.clear();}{set<string> mSet;for (int i = 0 ; i < 100000; i++){char buf[10];        sprintf(buf,"%d",i);        string temp = buf;        temp += "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";mSet.insert(temp);}}//delete []p;//sleep(20);}int main(int argc, char *argv[]){int i = 0 ;while (i < 10){pthread_t tid;int temp = pthread_create(&tid, NULL,mallocThread,NULL);if(temp == 0){pthread_detach(tid);}printf("AAAAAAAA\n");i++;sleep(2);}sleep(200);}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.