Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android file descriptor leakage debuging

I our company we have a lot of ui-tests which are run on virtual/real devices. After running for some time tests randomly crashing, which I think is the result of file descriptor exceeding: I used

ls /proc/${PID}/fd | wc -l and lsof -p ${PID} but it did not help a lot - most of the rows in lsof looks like:

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]

So my question is: is there any android/java/linux instruments/utils to find the source of leakage?

P.S. System.gc() did not help

like image 774
Art Avatar asked Mar 30 '16 09:03

Art


People also ask

How do I know if file descriptor is leaking?

You can detect a file descriptor leak in two different ways: You may notice a lot of IOExceptions with the message “Too many open files.” During load testing, you periodically run a profiling script, such as lsof (on UNIX), and you notice that the list of file descriptors grows continually.

How do you find memory leaks in an application on the Android platform?

The Memory Profiler is a component in the Android Profiler that helps you identify memory leaks and memory churn that can lead to stutter, freezes, and even app crashes. It shows a realtime graph of your app's memory use and lets you capture a heap dump, force garbage collections, and track memory allocations.

What is ParcelFileDescriptor?

Converts a string representing a file mode, such as "rw", into a bitmask suitable for use with open(File, int) . String. toString() Returns a string representation of the object. static ParcelFileDescriptor.

What is a file handle leak?

A handle leak is a type of software bug that occurs when a computer program asks for a handle to a resource but does not free the handle when it is no longer used.


1 Answers

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
like image 135
Art Avatar answered Oct 12 '22 10:10

Art