Set the maximum number of opened files in linux. Description of nofile, nr_open, and file-max

Source: Internet
Author: User

Set the maximum number of opened files in linux. Description of nofile, nr_open, and file-max
During Development and O & M, we often encounter problems such as "Socket/File: Can't open so many files", "cannot open more processes", or the coredump is too large, you can set resource limits to solve these problems. When I was teaching a customer how to set the maximum number of files, I wrote this document when I found that the methods on the Internet were different. We usually know that you can use the ulimit command to view and set system resources for a linux User.

Table 1. ulimit parameter description

Option [options] Description Example
-H Set hard resource limits. Once set, they cannot be added. Ulimit-Hs 64; hard resources are restricted. The thread stack size is 64 KB.
-S Set soft resource limit. After setting, you can add but cannot exceed the hard resource setting. Ulimit-Sn 32; restrict soft resources and 32 file descriptors.
- Displays all current limit information. Ulimit-a; displays all current limit information.
-C The maximum core file size, in blocks. Ulimit-c unlimited; the size of the generated core file is not limited.
-D The maximum data segment size of a process, in Kbytes. Ulimit-d unlimited; do not limit the Data Segment Size of the process.
-F Maximum number of files that can be created by a process, in blocks. Ulimit-f 2048; restrict the maximum file size that can be created by a process to 2048 blocks.
-L Maximum lockable memory size, in Kbytes. Ulimit-l 32; the maximum lockable memory size is 32 Kbytes.
-M The maximum memory size, in Kbytes. Ulimit-m unlimited; maximum memory is not limited.
-N Maximum number of file descriptors that can be opened. Ulimit-n 128; a maximum of 128 file descriptors can be used.
-P The size of the MPs queue buffer, in Kbytes. Ulimit-p 512; limit the MPs queue buffer size to 512 Kbytes.
-S Thread stack size, in Kbytes. Ulimit-s 512; limits the thread stack size to 512 Kbytes.
-T The maximum CPU usage time, in seconds. Ulimit-t unlimited; the maximum CPU usage time is not limited.
-U Maximum number of processes available to users. Ulimit-u 64; limit that users can use up to 64 processes.
-V Maximum available virtual memory of a process, in Kbytes. Ulimit-v 200000; Limit the maximum available virtual memory to 200000 Kbytes.
Of course, we all know that most of the linux Command settings take effect temporarily, and the ulimit command takes effect only for the current terminal. If you need to take effect permanently, we have two methods, one is to write the command to profile and bashrc, and write it to rc on some web pages. local, in fact, is not correct; another is in limits. add a record in conf (The record must be restarted to take effect, and in/etc/pam. the seesion in d/is used in the limit module ). Next, we will discuss the maximum number of opened files in limits. conf. For the maximum number of files opened by users. the nofile corresponding to the conf file, whether in man's manual or in the file, is just a sentence "maximum number of open files". It actually corresponds to the maximum number of files that a single process can open, usually to save time, we want to remove its limitations. According to man's manual, "values-1, unlimited or infinity indicating no limit",-1, unlimited, and infinity indicate no restrictions, however, when you set nofile to this value, you will find that you cannot log on to the system after restarting. It can be seen that nofile has an upper limit. At the same time, use ulimit to test: # ulimit-n unlimitedbash: ulimit: open files: cannot modify limit: the operation is not allowed to write a simple for loop and the result is: 1 # for V in 'seq 100000 10000000 '; do ulimit-n $ V; [[$?! = 0] & break; done then executes ulimit-n. We can see that 1048576 is the maximum value of nofile, but why is this value? 1048576 is 1024*1024. Of course, this is useless... After tracking, we will find that this value is actually defined by the Kernel Parameter nr_open: # cat/proc/sys/fs/nr_open 1048576 so far we will talk about nr_open, when talking about setting the maximum number of files on the internet, sometimes some posts talk about modifying file-max. Literally, file-max does seem to correspond to the maximum number of files, in The Linux kernel documentation, the two explanations are: file-max: the value in file-max denotes The maximum number of file-handles that the Linux kernel will allocate. when you get lotsof error messages about running out of file handles, you mightwant to increase this limit execution: grep- R MemTotal/proc/meminfo | awk '{printf ("% d", $2/10)}', which is similar to file-max; nr_open: this denotes the maximum number of file-handles a process canallocate. default value is 1024*1024 (1048576) which shocould beenough for most machines. actual limit depends on RLIMIT_NOFILEresource limit. by now, I am a little dizzy, file-handles (that is, the file handle). In UNIX/LINUX, we are more exposed to file discriptor (FD, that is, file descriptor), through Baidu search, it seems that file-handle in windows is similar to f Ile discrptor, but we are talking about linux. After google, we can be precise to the differences between the two concepts in C, according to their discussion, file-handle should be a high-level object called using fopen, fread and other functions, while FD is an underlying object, which can be called through open, read and other functions. At this point, we can conclude that file-max is the maximum number of files that can be allocated to the kernel, and nr_open is the maximum number of files that can be allocated to a single process, so when we use ulimit or limits. if you want to exceed the default value of 1048576, increase the value of nr_open (sysctl-w fs. nr_open = 100000000 or directly write to sysctl. conf file ). Of course, the maximum number of file-handle opened by a single process of millions should be enough .. Due to the limited level of the author, there are inevitable errors and omissions in the content, as well as why the default value of file-max is smaller than the value of nr_open and has not been further investigated. You are welcome to discuss it.

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.