Maximum number of handles that a process can open

Source: Internet
Author: User
A process can open the maximum file handle (Linux)

In Linux, we useUlimit-nCommand 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 check: 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.

 

When deploying applications in Linux, sometimes the socket/file: Can't open so many files problem occurs. In fact, Linux has file handle restrictions (like WINXP ?), In addition, the default value is not very high, generally 1024. As a production server, it is very easy to reach this number. Therefore, we need to increase this value.

I probably know that the ulimit command is related. I searched for it on Google. Most of the statements are vague, and I didn't talk about them in a unified way, after reading a lot of articles in two hours, I finally figured out some ulimit-related configuration problems.

We can use ulimit-a to view all the limit values. I only care about the number of file handles.

Open File (-N) 1024

This is the limit.

Here, many articles on ulimit are vague. Is this 1024 a system limitation or a user restriction. In fact, this is due to User restrictions. The complete statement should be the restrictions on the programs to be run by the current user.

1. This restriction is for a single program.

2. The restriction does not change the restriction of the previously running program.

3. The modification to this value will disappear after exiting the current shell.

For example, I first run a program A, then changed the limit to 2048 through ulimit, then run B, then exit shell, then log on, and then run C. then, only B can open the first 2048 handles.

If we need to change the overall limit value or the program we run is started by the system, what should we do?

One method is to put the ulimit modification command in/etc/profile, but this method is not good.

The correct method should be to modify/etc/security/limits. conf.

There are detailed annotations, such

* Soft nofile 2048

* Hard nofile 32768

You can change the file handle limit to soft 2048 and hard 32768.

Another question is involved here: what is soft limitation and what is hard limitation

The hard limit is the actual limit, while the soft limit is the warnning limit, only the warning

In fact, the ulimit command itself has the hardware and software settings. Adding-H is hard, and adding-S is soft.

Soft restrictions are displayed by default. If they are not added during modification, they are modified together.

The first part of the configuration file is domain, which is set as an asterisk to represent the global. In addition, you can make different restrictions for different users.

Modified, and ulimit will take effect immediately after you log on again. However, new values can be used only when the previously started program is restarted. I use centos. It seems that some systems need to be restarted to take effect.

Ulimit is actually a restriction on a single program

What about system restrictions?

Actually, here,/proc/sys/fs/file-max

You can use Cat to view the current value and use echo to modify it immediately.

In addition,/proc/sys/fs/file-NR

Read-only, you can see the number of file handles currently used by the entire system

When looking for file handle problems, there is also a very practical program lsof

You can easily see the handles opened by a process.

You can also see which process occupies a file/directory.

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.