美文网首页Android葵花宝典优化
Could not read input channel fil

Could not read input channel fil

作者: MrLee_3641 | 来源:发表于2017-08-23 23:43 被阅读2949次

    最近开发功能的时候monkey总是能跑出一个bug,

    java.lang.RuntimeException: Could not read input channel file descriptors from parcel.

    百思不得其解,认为是系统上面的bug,实时证明自己还是太年轻.现在开始分析一下这个bug产生的原因.

    一.为什么会产生句柄泄露?

    众所周知Android是linux内核,也就是可以理解linux下,一切资源都是句柄,每个进程都有自己的句柄上限,而超过了这个句柄上线,就会发生异常.一般android的App都是在单个进程下运行的,FD的句柄上限是1024,这个在后面的会说明.一切的重点都在proc(Processes,虚拟的目录,是系统内存的映射。可直接访问这个目录来获取系统信息。 )这个文件夹下的内容.

    这里有参考The proc File System.

    The Linux kernel has two primary functions: to control access to physical devices on the computer and to schedule when and how processes interact with these devices. The /proc/ directory — also called the proc file system — contains a hierarchy of special files which represent the current state of the kernel — allowing applications and users to peer into the kernel's view of the system.

    Within the /proc/ directory, one can find a wealth of information detailing the system hardware and any processes currently running. In addition, some of the files within the /proc/ directory tree can be manipulated by users and applications to communicate configuration changes to the kernel.

    Linux系统上的/proc目录是一种文件系统,即proc文件系统。与其它常见的文件系统不同的是,/proc是一种伪文件系统(也即虚拟文件系统),存储的是当前内核运行状态的一系列特殊文件,用户可以通过这些文件查看有关系统硬件及当前正在运行进程的信息,甚至可以通过更改其中某些文件来改变内核的运行状态。

    二.查看进程的句柄信息

    我们通过proc这个虚拟文件系统,可以获取到当前App的进程,然后查看具体的进程信息,当前App是否存在句柄泄露的问题.具体方法如下.

    public class MainActivity extends Activity {
    
    private PageView mPageView;
    
    private Button mButton;
    
            @Override
    
            protected void onCreate(Bundle savedInstanceState) {
    
                    super.onCreate(savedInstanceState);
    
                    setContentView(R.layout.activity_main);
    
                    exeCommand("ps");
    
            }
    
            private void exeCommand(String command){
    
                    Runtime runtime = Runtime.getRuntime();
    
                    try {
    
                          Process proc = runtime.exec(command);
    
                          BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
    
                          String line;
    
                          while ((line = in.readLine()) != null) {
    
                              System.out.println(line);
    
                          }
    
                    } catch (IOException e) {
    
                        e.printStackTrace();
    
            }
    
        }
    
    }
    

    这样我们看到在Android Studio的logcat中打印出了当前所有的进程号.

    进程号.png

    然后我们继续修改我们的代码.

    exeCommand("ps | grep com.behanga");
    

    com.behanga是我测试App的名字.

    app名字.png

    这是我们可以看到该App的进程号是16023,也就是说对应在proc中对应的文件夹为/proc/16023/,在这个文件夹下我们看到当前App的信息,好我们继续往下查看.
    这时我们打开terminal,输入

    adb shell
    

    进入adb shell,然后输入

    cd /proc/16023 && ls -al
    

    现在展示在我们面前的就是当前进程下信息.


    当前进程的info.png

    我们现在可以查看一下当前进程一些限制.

    cat limits
    
    最大打开文件数.png

    这里我们看到Max open fils的限制为1024,硬件限制为4096,也就是说我们当前打开文件句柄的最大数为1024.

    这时我们继续往下看,因为是要分析file descriptors的问题,那么自然要进入fd目录下一看究竟.

    cd fd && ls -al
    
    当前打开的句柄信息.png

    当前展示了所在进程下所有已经被打开的文件句柄.我们可以在当前文件夹下统计所有的句柄总数.

    ls | wc -l
    

    这时打印出所有的句柄总数为64.这时我们已经完成对当前进程的一些句柄信息查看.

    三.模拟句柄泄露情况

    如果我们要模拟句柄泄漏的情况,我们可以在App中启动一个HandlerThread,每次在onCreate的时候HandlerThread.start(),但是在onDestory()时,并不调用HandlerThread.quit()方法.重复进入.然后在terminal中统计FD的总数,查看是否有变化,如果FD的总数一直在增加,就可以确认你当前App中已经出现了句柄泄露的情况.

    四.如何避免句柄泄露的发生

    提高对编码意识,另外就是monkey的测试,很多时候会发现一些黑盒测试无法发现的问题.

    相关文章

      网友评论

      • LifeDavi:怎么找到,对应的泄漏点?
        MrLee_3641:在对应的activity里面 查看对应的FD 是否有泄露,再逐步分析具体是什么地方出现了该问题
      • 王黎聪:有深度 , 不错不错,给赞 ; 我也遇到了 ,搞了 一上午 发现 这个才是问题的根本

      本文标题:Could not read input channel fil

      本文链接:https://www.haomeiwen.com/subject/jzfsdxtx.html