swirl
Home Software Blog Wallpapers Webtools
Open file handles in Linux & ulimit
Sunday 13, October 2024   |   Post link
Flow

Overview

If you work on Linux, you've probably encountered the "too many open files" error sometime or the other. This post discusses few things I tried to investigate how this works.

One of our indexing nodes at work was reporting this errors in a log file. The Java process was terminating and it seemed all we had to do was increase the limit of maximum allowed open files on the machine. The next question was how?

ulimit

If you search for the above problem 'too many open files', you'll definitely come across answers telling you to set the correct ulimit. The ulimit not only controls the number of open file handles but also controls many other things, all of which can be viewed using the following command:

siddharth@ubuntu:/shared$ ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 63447
max locked memory       (kbytes, -l) 65536
max memory size         (kbytes, -m) unlimited
open files                      (-n) 4096
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 63447
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

Let's talk about the open file setting in detail - there are actually two limits for this - a soft-limit and a hard-limit. The soft-limit can be raised up to the hard-limit by the logged in non-root user whereas the hard-limit can be changed by a root user. As the name suggests this limit is used to cause a program to error out when the total number of open files exceeds the limit.

A Java program named FileLimits was used to create a large number of files which are kept open and closed when the program exits, the source code for this is available here: FileLimits source code.

Setting ulimits

Let's quickly view the current limits of the number of open files:

siddharth@ubuntu:/$ ulimit -Sn; ulimit -Hn
4096
24000
So, our soft-limit is 4096 and our hard-limit is 24000. Where are these number coming from? These limits can be defined in the following two places (as of my findings):
  • /etc/security/limits.conf file
  • /etc/systemd/user.conf

/etc/security/limits.conf file

Limits can be defined for specific users here. The entries look like this:

    $ cat /etc/security/limits.conf
    # /etc/security/limits.conf
    #
    #Each line describes a limit for a user in the form:
    #
    #            
    #
    #Where:
    # can be:
    #        - a user name
    #        - a group name, with @group syntax
    #        - the wildcard *, for default entry
    #        - the wildcard %, can be also used with %group syntax,
    #                 for maxlogin limit
    #        - NOTE: group and wildcard limits are not applied to root.
    #          To apply a limit to the root user,  must be
    #          the literal username root.
    #
    # can have the two values:
    #        - "soft" for enforcing the soft limits
    #        - "hard" for enforcing hard limits
    #
    # can be one of the following:
    #        - core - limits the core file size (KB)
    #        - data - max data size (KB)
    #        - fsize - maximum filesize (KB)
    #        - memlock - max locked-in-memory address space (KB)
    #        - nofile - max number of open file descriptors
    #        - rss - max resident set size (KB)
    #        - stack - max stack size (KB)
    #        - cpu - max CPU time (MIN)
    #        - nproc - max number of processes
    #        - as - address space limit (KB)
    #        - maxlogins - max number of logins for this user
    #        - maxsyslogins - max number of logins on the system
    #        - priority - the priority to run user process with
    #        - locks - max number of file locks the user can hold
    #        - sigpending - max number of pending signals
    #        - msgqueue - max memory used by POSIX message queues (bytes)
    #        - nice - max nice priority allowed to raise to values: [-20, 19]
    #        - rtprio - max realtime priority
    #        - chroot - change root to directory (Debian-specific)
    #
    #                 
    #
    
    #*               soft    core            0
    #root            hard    core            100000
    #*               hard    rss             10000
    #@student        hard    nproc           20
    #@faculty        soft    nproc           20
    #@faculty        hard    nproc           50
    #ftp             hard    nproc           0
    #ftp             -       chroot          /ftp
    #@student        -       maxlogins       4
    
    siddharth        soft    nofile          24000
    siddharth        hard	 nofile		     24000
    
    testuser         soft    nofile          5000
    testuser         hard    nofile          5000
    
    # End of file

Notice the limits defined towards the end of the file for siddharth and testuser.

/etc/systemd/user.conf file

This file defines limits for all users

$ cat /etc/systemd/user.conf
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.
#
# You can override the directives in this file by creating files in
# /etc/systemd/user.conf.d/*.conf.
#
# See systemd-user.conf(5) for details

[Manager]
#LogLevel=info
#LogTarget=console
#LogColor=yes
#LogLocation=no
#SystemCallArchitectures=
#TimerSlackNSec=
#StatusUnitFormat=description
#DefaultTimerAccuracySec=1min
#DefaultStandardOutput=inherit
#DefaultStandardError=inherit
#DefaultTimeoutStartSec=90s
#DefaultTimeoutStopSec=90s
#DefaultTimeoutAbortSec=
#DefaultRestartSec=100ms
#DefaultStartLimitIntervalSec=10s
#DefaultStartLimitBurst=5
#DefaultEnvironment=
#DefaultLimitCPU=
#DefaultLimitFSIZE=
#DefaultLimitDATA=
#DefaultLimitSTACK=
#DefaultLimitCORE=
#DefaultLimitRSS=
#DefaultLimitNOFILE=
DefaultLimitNOFILE=4096:524288
#DefaultLimitAS=
#DefaultLimitNPROC=
#DefaultLimitMEMLOCK=
#DefaultLimitLOCKS=
#DefaultLimitSIGPENDING=
#DefaultLimitMSGQUEUE=
#DefaultLimitNICE=
#DefaultLimitRTPRIO=
#DefaultLimitRTTIME=

The earlier result of running ulimit -Sn and ulimit -Hn showed 4096 and 24000 respectively. We can see that the /etc/security/limits.conf file is setting the soft and hard nofile limits for user siddharth to 24000 and 24000, however the /etc/systemd/user.conf is setting the limits to 4096 and 524288. Looks like the system is combining the values present in both files and taking the lower values for the final result i.e. soft-limit is 4096 and hard-limit is 24000.

Seeing the impact of the soft and hard limits

We now run the FileLimits program to see how these limits impact the running of the program which opens a specified number of temporary files.

siddharth@ubuntu:/shared$ java -jar FileLimits.jar . 4097
Files created, 4097 streams opened.
Press  to close & delete the files and exit the program.

4097 files closed, 4097 files deleted.
siddharth@ubuntu:/shared$ ulimit -n
4096
siddharth@ubuntu:/shared$ java -jar FileLimits.jar . 5000
Files created, 5000 streams opened.
Press  to close & delete the files and exit the program.

5000 files closed, 5000 files deleted.

Clearly, the soft-limit of 4096 is having no impact as the program is able to create and open 5000 files. Next, lets test the hard-limit of 24000.

siddharth@ubuntu:/shared$ java -jar FileLimits.jar . 24000
Something went wrong: Too many open files
Number of open files: 23995.
23995 files closed, 23995 files deleted.

This time the program gets an error, it was able to open 23995 files of the specified 24000 files, 5 less.

System limits

Everything discussed thus far is at the level of users. Its possible to specify limits at the machine level as well. Let's see what this has been set to out-of-the-box.

siddharth@ubuntu:/$ sysctl fs.file-max
fs.file-max = 9223372036854775807

or

cat /proc/sys/fs/file-max
9223372036854775807 is a pretty large number and I suggest you don't touch it as it will affect every process in the system. In case you do want to change it, it can be changed using the following command:
sysctl -w fs.file-max=<PUT YOUR NUMBER>

Setting it using the above command does not make it permanent, the value is reset on a reboot which is a great thing as I managed to make my OS unusable by setting a low number for this limit. If you do want to change this permanently, you need to edit the /etc/sysctl.conf file and add a line like:

fs.file-max=9223372036854775806

I just decreased the limit by 1. Reboot and you should see the changed file-max value.

Another way to set these limits is to create a file in the /etc/sysctl.d/ directory and placing the same setting there like this:

siddharth@ubuntu:/etc/sysctl.d$ cat 20-custom.conf
fs.file-max = 9223372036854775805

Files in this directory are evaluated in order with files coming later in the sort order overriding values defined in previous files.

Rounding off

In most production issues, its most likely the file-max limit has been set to a low value. Changing the limit in either in the /etc/sysctl.conf file or in a conf file present in the /etc/sysctl.d directory will get rid of the error.

References

Stack over flow Linux Increase The Maximum Number Of Open Files / File Descriptors (FD)


Categories: Linux (2)

Comments

Posts By Year

2024 (4)
2023 (5)
2022 (10)
2021 (5)
2020 (12)
2019 (6)
2018 (8)
2017 (11)
2016 (6)
2015 (17)
2014 (2)
2013 (4)
2012 (2)

Posts By Category

.NET (4)
.NET Core (2)
ASP.NET MVC (4)
AWS (5)
AWS API Gateway (1)
Android (1)
Apache Camel (1)
Architecture (1)
Audio (1)
Azure (2)
Book review (3)
Business (1)
C# (3)
C++ (2)
CloudHSM (1)
Containers (4)
Corporate culture (1)
Database (3)
Database migration (1)
Desktop (1)
Docker (1)
DotNet (3)
DotNet Core (2)
ElasticSearch (1)
Entity Framework (3)
Git (3)
IIS (1)
JDBC (1)
Java (10)
Kibana (1)
Kubernetes (1)
Lambda (1)
Learning (1)
Life (7)
Linux (2)
Lucene (1)
Multi-threading (1)
Music (1)
OData (1)
Office (1)
PHP (1)
Photography (1)
PowerShell (2)
Programming (28)
Python (1)
Rants (5)
SQL (2)
SQL Server (1)
Security (3)
Software (1)
Software Engineering (1)
Software development (2)
Solr (1)
Sql Server (2)
Storage (1)
T-SQL (1)
TDD (1)
TSQL (5)
Tablet (1)
Technology (1)
Test Driven (1)
Testing (1)
Tomcat (1)
Unit Testing (1)
Unit Tests (1)
Utilities (3)
VC++ (1)
VMWare (1)
VSCode (1)
Visual Studio (2)
Wallpapers (1)
Web API (2)
Win32 (1)
Windows (9)
XML (2)

Posts By Tags

.NET(6) API Gateway(1) ASP.NET(4) AWS(3) Adults(1) Advertising(1) Android(1) Anti-forgery(1) Asynch(1) Authentication(2) Azure(2) Backup(1) Beliefs(1) BlockingQueue(1) Book review(2) Books(1) Busy(1) C#(4) C++(3) CLR(1) CORS(1) CSRF(1) CTE(1) Callbacks(1) Camel(1) Certificates(1) Checkbox(1) Client authentication(1) CloudHSM(1) Cmdlet(1) Company culture(1) Complexity(1) Consumer(1) Consumerism(1) Containers(3) Core(2) Custom(2) DPI(1) Data-time(1) Database(4) Debugging(1) Delegates(1) Developer(2) Dockers(2) DotNetCore(3) EF 1.0(1) Earphones(1) Elastic Search(2) ElasticSearch(1) Encrypted(1) Entity framework(1) Events(1) File copy(1) File history(1) Font(1) Git(2) HierarchyID(1) Hyper-V(1) IIS(1) Installing(1) Intelli J(1) JDBC(1) JSON(1) JUnit(1) JWT(1) Java(3) JavaScript(1) Kubernetes(1) Life(1) LinkedIn(1) Linux(2) Localization(1) Log4J(1) Log4J2(1) Logging(1) Lucene(1) MVC(4) Management(2) Migration history(1) Mirror(1) Mobile Apps(1) Modern Life(1) Money(1) Music(1) NGINX(1) NTFS(1) NUnit(2) OData(1) OPENXML(1) Objects(1) Office(1) OpenCover(1) Organization(1) PHP(1) Paths(1) PowerShell(2) Processes(1) Producer(1) Programming(2) Python(2) QAAC(1) Quality(1) REDIS(2) REST(1) Runtimes(1) S3-Select(1) SD card(1) SLF4J(1) SQL(2) SQL Code-first Migration(1) SSH(2) SSL(1) Sattelite assemblies(1) School(1) Secrets Manager(1) Self reliance(1) Service(1) Shell(1) Solr(1) Sony VAIO(1) Spirituality(1) Spring(1) Sql Express(1) System Image(1) TDD(1) TSQL(3) Table variables(1) Tables(1) Tablet(1) Ubuntu(1) Url rewrite(1) VMWare(1) VSCode(1) Validation(2) VeraCode(1) Wallpaper(1) Wallpapers(1) Web Development(4) Windows(2) Windows 10(2) Windows 2016(2) Windows 8.1(1) Work culture(1) XML(1) Yii(1) iTunes(1) open file handles(1) renew(1) security(1) static ip address(1) ulimit(1)