Android "Package manager has died"
感谢http://blog.csdn.net/xxooyc/article/details/50162523
开发中有时会碰到这种crash,不太好定位错误。
04-28 18:25:30.956 12107-12138/? E/JavaBinder: !!! FAILED BINDER TRANSACTION !!!
04-28 18:25:30.957 12107-12138/? E/AndroidRuntime: FATAL EXCEPTION: Thread-2163
Process: com.android.xiaokecong.packagemanagerdemo, PID: 12107
java.lang.RuntimeException: Package manager has died
at android.app.ApplicationPackageManager.getInstalledPackages(ApplicationPackageManager.java:521)
at android.app.ApplicationPackageManager.getInstalledPackages(ApplicationPackageManager.java:511)
at com.android.xiaokecong.packagemanagerdemo.MainActivity$2.run(MainActivity.java:60)
Caused by: android.os.TransactionTooLargeException
at android.os.BinderProxy.transactNative(Native Method)
at android.os.BinderProxy.transact(Binder.java:508)
at android.content.pm.IPackageManager$Stub$Proxy.getInstalledPackages(IPackageManager.java:2683)
at android.app.ApplicationPackageManager.getInstalledPackages(ApplicationPackageManager.java:518)
at android.app.ApplicationPackageManager.getInstalledPackages(ApplicationPackageManager.java:511)
at com.android.xiaokecong.packagemanagerdemo.MainActivity$2.run(MainActivity.java:60)
上面提到的博客中从Android源码中找到了这个异常的出处:
frameworks/base/core/Java/Android/app/ApplicationPackageManager.java:
102 @Override
103 public PackageInfo getPackageInfo(String packageName, int flags)
104 throws NameNotFoundException {
105 try {
106 PackageInfo pi = mPM.getPackageInfo(packageName, flags, mContext.getUserId());
107 if (pi != null) {
108 return pi;
109 }
110 } catch (RemoteException e) {
111 throw new RuntimeException("Package manager has died", e);
112 }
113
114 throw new NameNotFoundException(packageName);
115 }
这是Android中的Binder调用,catch的异常是RemoteException,由打印的crash信息可以看出是因为
Caused by: android.os.TransactionTooLargeException
先发生了TransactionTooLargeException,由字面意思可以看出是超出了某个内存限制。Android源码中:frameworks/base/core/jni/android_util_Binder.cpp:
682 case FAILED_TRANSACTION:
683 ALOGE("!!! FAILED BINDER TRANSACTION !!!");
684 // TransactionTooLargeException is a checked exception, only throw from certain methods.
685 // FIXME: Transaction too large is the most common reason for FAILED_TRANSACTION
686 // but it is not the only one. The Binder driver can return BR_FAILED_REPLY
687 // for other reasons also, such as if the transaction is malformed or
688 // refers to an FD that has been closed. We should change the driver
689 // to enable us to distinguish these cases in the future.
690 jniThrowException(env, canThrowRemoteException
691 ? "android/os/TransactionTooLargeException"
692 : "java/lang/RuntimeException", NULL);
693 break;
这就可以看出Binder的使用超出了一个进程的使用就会抛出这个异常,如果是其他原因就会抛出RuntimeException.
一个Binder的内存限制大小约为1M:
frameworks/native/libs/binder/ProcessState.cpp:
#define BINDER_VM_SIZE ((1*1024*1024) - (4096 *2))
也就是说一个进程使用的Binder内容大小超出1M就会抛出这个crash,如果此时正在使用getPackageInfo,就一定会抛出”Package manager has died”这个异常。
/**
* 100个线程进行Binder调用
*/
private void test() {
for (int i = 0; i < 100; i++) {
new Thread() {
@Override
public void run() {
List<PackageInfo> list = getPackageManager().getInstalledPackages(GET_ACTIVITIES);
}
}.start();
}
}
这里写一个例子,创建一个100个线程,都进行getInstalledPackages()的Binder操作,运行起来,crash的结果跟最上面的图一样。
猜想,如果多个线程同步执行这一操作,是不是能够避免呢?因为某个时间点只有一个线程操作getPackageInfo(),因为一个线程用完了就会释放资源,理论上可以的。
使用synchronized关键字将getPackageInfo()这样的Binder操作包裹起来即可,实验结果如逾期所想。HelloWorld!
完。
上一篇: 2020-11-27
下一篇: 礼物的最大价值 (动态规划)