当前位置: 代码迷 >> 综合 >> Binder 内存超出导致 Package manager has died 实验小结
  详细解决方案

Binder 内存超出导致 Package manager has died 实验小结

热度:44   发布时间:2023-12-14 01:41:26.0

具体分析 Package manager has died https://blog.csdn.net/xxooyc/article/details/50162523

实验过程:

第一步:点击helloTv,不捕获throwable异常,程序崩溃,出现package manager has died异常

第二步:捕获throable异常,点击helloTv,程序不再崩溃

第三步:等for 循环结束后,点击helloTv2,可以正常输出

实验结论:

1.Package manager has died 在多线程并发情况下可能出现

2.通过捕获throwable可以捕获当时的binder异常,也就是package Manager has died异常

3.当并发结束后,binder内存释放后恢复正常,此时调用packageManager会恢复正常

4.控制并发是好的解决方案,其次是捕获异常

实验代码:

 findViewById(R.id.helloTv).setOnClickListener(new View.OnClickListener() {@Override
        public void onClick(View view) {for (int i = 0;i<1000;i++){final int n = i;
                new Thread(new Runnable() {@Override
                    public void run() {try {PackageManager packageManager = getPackageManager();
                            String versioName =  packageManager.getPackageInfo(getApplicationContext().getPackageName(), 0).versionName;
                            Thread.sleep(10000L);
                            System.out.println("ssssssssssssss:"+n+"  "+versioName);
                        } catch (PackageManager.NameNotFoundException e) {e.printStackTrace();
                        } catch (InterruptedException e) {e.printStackTrace();
                        }catch (Throwable e){e.printStackTrace();
                        }}}).start();
            }}});

    findViewById(R.id.helloTv2).setOnClickListener(new View.OnClickListener() {@Override
        public void onClick(View view) {PackageManager packageManager = getPackageManager();
            try {String versioName =  packageManager.getPackageInfo(getApplicationContext().getPackageName(), 0).versionName;
                System.out.println("ssssssssssssssfff:"+versioName);

            } catch (PackageManager.NameNotFoundException e) {e.printStackTrace();
            }}});
}



  相关解决方案