Multi-threaded pthread memory leaks under Linux

Source: Internet
Author: User

Target file:/proc/<pid>/maps If there is a large amount of 8K or so of memory fragmentation, there is a memory leak. Similarly, if a lot of memory fragmentation occurs in the maps file for the corresponding PID process, a memory leak is also indicated.

Query data learned that: Linux system, the thread resources of the program is limited, the performance of a program can run at the same time the number of threads is limited. By default, when a thread ends, its corresponding resource is not freed, so if the thread is repeatedly established in a program, and the thread exits by default, the final thread resource is exhausted and the process will no longer be able to establish a new thread.

To solve this problem, there are 2 ways that the system automatically frees the thread resource, or the thread resource is freed by another thread.

Here, I think the process runs after itself, and is also a thread that threads the main thread, the main thread and the main thread to share process resources. Unlike other threads, after the main thread runs, the program exits and all the program-established threads exit.

Method One, automatic system release
If you want the thread resources to be freed by the system at the end of threads, you need to set the thread property to detach (detach thread). Code, you can say this:

pthread_ T T; pthread_attr_t a;  //Thread Properties pthread_attr_init (&a);    //Initialize thread properties pthread_attr_setdetachstate (&a, pthread_create_detached);        //set thread properties pthread_create (&t, &a, Getandsaveauthviewsdrstub, ( void *) LP);                     //thread



Method Two, other threads release
Another way, the resource is freed by another thread. Code, you can say this:

pthread_t t;pthread_create( NULL, NULL, GetAndSaveAuthviewSDRStub, (void*)lp);pthread_join( t);


Pthread_join (t) waits for thread t to exit and frees the resource occupied by the T thread. Of course, there is also a synchronous feature that allows one thread to wait for another thread to exit before it continues to run.

Unlike Windows, Linux thread execution pthread has two state joinble states and unjoinable states, if the thread is joinable state when the thread function returns to its own exit or Pthread_ Exit does not release the stack and thread descriptors that the thread occupies (a total of more than 8K). These resources will be released only if you call Pthread_join. If the thread of the unjoinable state, these resources are automatically released when the threads function exits or pthread_exit.

The Unjoinable property can be specified at pthread_create time, or Pthread_detach itself in the thread after the thread is created. such as Pthread_detach (Pthread_self ()), change the state to unjoinable state to ensure the release of the resource. or set the thread to joinable, and then call Pthread_join as appropriate.

Check the/proc/<pid>/maps file during thread run, if you see a lot of virtual memory fragments of about 8k, it is basically confirmed that it is caused by threading resource leakage. Pthread_create failed after 300 consecutive threads were created.

Multi-threaded pthread memory leaks under Linux

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.