Android view fd information in the current process

Let's briefly introduce the concept of FD:
Since the bottom layer of Android is Linux, Android has many features of Linux, such as the concept of FD. FD is the abbreviation of File Descriptor, Chinese called: File Descriptor.
When the program opens an existing file or creates a new file, the kernel returns an FD file descriptor to the process. Since everything in the Linux system is a file, FD is a non-negative integer in form. It is an index value that points to the record table of all open files maintained by the kernel for each process.
In Linux, there is a fixed limit on the number of files that each process can open, which means that FD has an upper limit and cannot open files indefinitely. When the FD of a certain process reaches the specified number, it will crash when the file is opened again. Android also has this limitation, and the FD in Linux is generally 1024, and this value may be different in Android phones. So we sometimes need to check the FD information of the current process, the method is as follows:

import android.system.Os;
import android.util.Log;
import java.io.File;

private void listFd() {
    File fdFile = new File("/proc/" + android.os.Process.myPid() + "/fd");
    File[] files = fdFile.listFiles(); // 列出当前目录下所有的文件
    int length = files.length; // 进程中的fd数量
    StringBuilder stb = new StringBuilder();
    for (int i = 0; i < length; i++) {
        try {
            String strFile = Os.readlink(files[i].getAbsolutePath()); // 得到软链接实际指向的文件
            stb.append(strFile + "\n");
        } catch (Exception x) {
            Log.e(TAG, "listFd error=" + x);
        }
    }
    Log.d(TAG, "listFd=" + stb);
}

 

Guess you like

Origin blog.csdn.net/chenzhengfeng/article/details/115013823