How many file handles can be opened by a process (Linux)

Source: Internet
Author: User
A process can only enable the maximum number of file handles (Linux)-General Linux technology-Linux programming and kernel information. For more information, see the following. In Linux, we can use the ulimit-n command to view the maximum number of file handles that a single process can open (socket connections are included ). The default is 1024.

For general applications (such as Apache and system processes), 1024 is fully enough. However, how to process a large number of requests by a single process such as squid, mysql, and java is a little stretched. If the number of file handles opened by a single process exceeds the value defined by the system, the "too program files open" error message will be mentioned. How many file handles are opened by the current process? The following small script can help you view it:

Lsof-n | awk '{print $2}' | sort | uniq-c | sort-nr | more

During the system access peak hours, run the above script as the root user. The possible results are as follows:

# Lsof-n | awk '{print $2}' | sort | uniq-c | sort-nr | more
131 24204
57 24244
57 24231
56 24264

The first line is the number of opened file handles, and the second line is the process number. After obtaining the process number, we can use the ps command to get the detailed content of the process.

Ps-aef | grep 24204
Mysql 24204 24162 99? 00:24:25/usr/sbin/mysqld

Oh, it turns out that the maximum number of file handles opened by the mysql process. However, he currently only opens 131 file handles, far from the default 1024.

However, if the system concurrency is very large, especially the squid server, it is likely to exceed 1024. At this time, you must adjust the system parameters to adapt to application changes. Linux has hard and soft limits. You can use ulimit to set these two parameters. Run the following command as the root user:

Ulimit-HSn 4096

In the preceding command, H specifies the hard size, S specifies the soft size, and n indicates setting the maximum number of opened file handles for a single process. I personally think it is best not to exceed 4096. After all, the more open file handles, the slower the response time. After the number of handles is set, the default value is restored after the system restarts. If you want to save it permanently, you can modify the. bash_profile file, and modify/etc/profile to add the above command to the end.
Related Article

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.