Linux handle leak problem view

  

Background:

When we develop Linux online server, we often encounter problems with handle leaks. Because in the Linux system design, everything follows the principle of files, that is, disk files, directories, network sockets, disks, pipes, etc., all of which are files. When we open it, we will return an fd, which is a file. Handle. If you open a file frequently, or open a network socket and forget to release it, there will be a leak of the handle. In Linux system, the number of file handles that a process can call is limited. By default, the maximum number of handles that can be called by each process is 1024. If this limit is exceeded, the process will not be able to obtain a new handle, but will cause You cannot open a new file or network socket, and the service will be rejected for the online server.

View and modify the handle:

In the Linux system, you can view the maximum number of handles per process limit by ulimit–n, and modify the maximum number of handles of the process by ulimit –HSn 10240. When the number of handles reaches the limit, it will appear ”too many files open”.

There are several ways to view the number of handles occupied by a process:

1) You can view the thread whose thread pid number is open by using cat/proc/pid/fd;

2) With the lsof command, the /usr/sbin/lsof-p 21404 command results are as follows:


COMMAND PID USER FD TYPE DEVICE SIZE NODE NAMEvas 21404 root cwd DIR 8,3 4096 30195729 /home/users/root /vasvas 21404 root rtd DIR 8,2 4096 2 /vas 21404 root txt REG 8,3 112201650 30195914 /home/users/root/vasvas 21404 root mem REG 0,0 0 [heap] (stat: No such file or directory) Vas 21404 root mem REG 8,2 105080 339377 /lib64/ld-2.3.4.sovas 21404 root mem REG 8,2 1493186 339367 /lib64/tls/libc-2.3.4.sovas 21404 root mem REG 8,2 17943 339392 /lib64/libdl-2.3.4.sovas 21404 root mem REG 8,2 613297 339369 /lib64/tls/libm-2.3.4.sovas 21404 root mem REG 8,2 79336 490463 /usr/lib64/libz.so.1.2 .1.2


COMMAND: Name of the process PID: Process Identifier USER: Process Owner FD: File Descriptor, the application identifies the file by file descriptor. Such as cwd, txt, etc. TYPE: file type, such as DIR, REG, IPV4, FIEO, etc. DEVICE: Name of the specified disk SIZE: Size of the file NODE: Index node (identification of the file on the disk) NAME: The exact name of the open file < Br>

In addition, the lsof command can also view the process that occupies the port:

/usr/sbin/lsof-i :9001

Copyright © Windows knowledge All Rights Reserved