Android文件描述符洩漏調試 (Android file descriptor leakage debuging)


問題描述

Android文件描述符洩漏調試 (Android file descriptor leakage debuging)

我們公司有很多在虛擬/真實設備上運行的 ui 測試。運行一段時間後測試隨機崩潰,我認為這是文件描述符超出的結果:我使用了

ls /proc/${PID}/fd | wc ‑llsof ‑p ${PID} 但它並沒有太大幫助 ‑ lsof 中的大多數行看起來像:

30015    u0_a104  678      sock                                    859560 socket:[859560]
30015    u0_a104  679      0000                0,8                 4539 anon_inode:[eventpoll]
30015    u0_a104  680      0000                0,8                 4539 anon_inode:[eventfd]
30015    u0_a104  681      0000                0,8                 4539 anon_inode:[eventfd]
30015    u0_a104  682      0000                0,8                 4539 anon_inode:[eventpoll]
30015    u0_a104  683      0000                0,8                 4539 anon_inode:[eventfd]
30015    u0_a104  684      0000                0,8                 4539 anon_inode:[eventpoll]
30015    u0_a104  685      0000                0,8                 4539 anon_inode:[eventfd]

所以我的問題是:是否有任何 android/java/linux 工具/工具可以找到洩漏的來源

PS System.gc() 沒有幫助


參考解法

方法 1:

I have researched for this question for a while and would like to share what I found:

  1. File descriptor are used in Android at least for:

    • Network sockets (or another type of file)
    • Mapped to memory files
    • Threads ‑ so it was my case. See below why
  2. If you have created a HandlerThread, even if the last link to the HandlerThread instance will disappear thread will still work and consume FileDescriptor

  3. Threads in android can be seen:

    • In "Java heap dump" in Memory Abalyze Tool ‑ so I have seen > 500 threads while running intsrumentation tests ‑ they "eat" all file descriptors
    • Via terminal on android device adb shell ps ‑t or just ps ‑t

(by ArtArt)

參考文件

  1. Android file descriptor leakage debuging (CC BY‑SA 2.5/3.0/4.0)

#file-descriptor #java #linux-kernel #Android






相關問題

在 Android 中持久化文件描述符 (Persisting File Descriptors in Android)

無法理解 select() 系統調用 (failure to understand select() system call)

文件描述符中實際存儲了多少信息? (How much information is actually stored in a file descriptor?)

為同一個文件描述符創建兩個文件是否定義明確? (Is creating two FILEs for the same file descriptor well-defined?)

Android文件描述符洩漏調試 (Android file descriptor leakage debuging)

C中的文件描述符分配 (File Descriptor Assignment in C)

在某些 linux 程序中關閉奇怪的描述符 (Strange descriptor closing in some linux programs)

c read() 導致錯誤的文件描述符錯誤 (c read() causing bad file descriptor error)

C中系統調用的文件句柄問題 (Filehandle issue with system call in C)

使用 copy_file_range 進行複制加速 (Copy acceleration with copy_file_range)

打開文件過多錯誤,但 99.5% 的 inode 是免費的 (Too many open files error but 99.5% inodes are free)

SIGPIPE 由於文件描述符和進程替換 (SIGPIPE due to file descriptors and process substitution)







留言討論