The size of the line stacks space

Source: Internet
Author: User
Always do the Windows Server to Linux platform porting work, for the thread stack space is also indefinitely, so there are some problems and summed up some of the experience for everyone to share.
After 286 threads were started on my server, the thread failed to start, and the error returned was 12, which was defined as follows:
#define
Enomem/OUT of memory * *
It appears that the cause of the thread failure was the failure to allocate memory, while the process occupied a virt display of 3070MB, which is estimated to be low on virtual memory.
So we looked at two platforms about the thread stack space, listed as follows:
In Windows, the CreateThread function's parameter dwstacksize is the size of the stack in bytes that will be assigned to the new thread. The stack size should be a non-zero integer of 4KB, with a minimum of 8KB. The default size of the stack is 1MB.
In Linux, the thread stack size is set in the system's default settings or in the Thread Property object, and I did not set it, so I looked at the stack size of the system using the Ulimit command, as follows:
Stack size (Kbytes,-s) 10240
No wonder the use of so much virtual memory, 286*10m=2860m, and other parts of the consumption, so virtual memory is not enough.
Also consult the documentation, reflecting the Linux platform stack default size should be 8192KB, rather than 10M, but whether my server has been modified by others is not clear.

The Linux platform operates the POSIX line stacks space using the following functions:
int Pthread_attr_setstacksize (pthread_attr_t *threadattr, int stack_size);/Set
int Pthread_attr_getstacksize (pthread_attr_t *threadattr, int stack_size);
Immediately use the above function to test, the process is as follows:
In Linux, the stack size is set in the Thread Property object, that is, the parameter "threadattr" with the type "pthread_attr_t" is passed to the function pthread_create (). Before setting the property, you need to initialize the object by calling Pthread_attr_init (), and then use the Pthread_attr_setstacksize () function to set the line stacks space size, in bytes, and then call Pthread_create ( ) to create the thread, and then call Pthread_attr_destroy () to destroy the Property object.
After the line stacks space is set to 2M, the test confirms that the virtual memory consumed by the creation thread has been reduced successfully.
Note that all pthread_attr_setxxxx calls have a function similar to the PTHREAD_XXXX call (if any), except that you can use pthread_attr_xxxx before the thread is created to update the pthread_ that will be passed as a parameter The Property object for the Create. At the same time, you can use pthread_xxxx to modify any time after you create a thread.

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.