今天收到了用户反馈app发帖会闪退,了解用户的手机是oppo和app版本后,就去听云bug列表查看。定位到听云bug如下:
java.util.concurrent.TimeoutException: android.content.res.AssetManager.finalize() timed out after 10 seconds
android.content.res.AssetManager.destroy(Native Method)
android.content.res.AssetManager.finalize(AssetManager.java:574)
java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:197)
java.lang.Daemons$FinalizerDaemon.run(Daemons.java:176)
java.lang.Thread.run(Thread.java:818)
啥都看不出来,而且还纳闷为什么链接超时会引起闪退。。。
没办法,只能追踪报错的地方了java.lang.Daemons
private static void finalizerTimedOut(Object object) {
// The current object has exceeded the finalization deadline; abort!
String message = object.getClass().getName() + ".finalize() timed out after "
+ (MAX_FINALIZE_NANOS / NANOS_PER_SECOND) + " seconds";
Exception syntheticException = new TimeoutException(message);
// We use the stack from where finalize() was running to show where it was stuck.
syntheticException.setStackTrace(FinalizerDaemon.INSTANCE.getStackTrace());
Thread.UncaughtExceptionHandler h = Thread.getDefaultUncaughtExceptionHandler();
// Send SIGQUIT to get native stack traces.
try {
Os.kill(Os.getpid(), OsConstants.SIGQUIT);
// Sleep a few seconds to let the stack traces print.
Thread.sleep(5000);
} catch (Exception e) {
System.logE("failed to send SIGQUIT", e);
} catch (OutOfMemoryError ignored) {
// May occur while trying to allocate the exception.
}
if (h == null) {
// If we have no handler, log and exit.
System.logE(message, syntheticException);
System.exit(2);
}
// Otherwise call the handler to do crash reporting.
// We don't just throw because we're not the thread that
// timed out; we're the thread that detected it.
h.uncaughtException(Thread.currentThread(), syntheticException);
}
可以看出我们的报错信息android.content.res.AssetManager.finalize() timed out after 10 seconds
其实就是finalizerTimedOut()
方法中的message。
而当UncaughtExceptionHandler
异常为null的时候,系统会抛出异常并退出app。
而解决方法有两种:
第一种是调用java.lang.Daemons类中的stop方法
然后在重新启动守护线程。
/**
* 解决oppoR9 TimeoutExceptions
*/
public void fix() {
try {
Class clazz = Class.forName("java.lang.Daemons$FinalizerWatchdogDaemon");
Method method = clazz.getSuperclass().getDeclaredMethod("stop");
method.setAccessible(true);
Field field = clazz.getDeclaredField("INSTANCE");
field.setAccessible(true);
method.invoke(field.get(null));
}
catch (Throwable e) {
e.printStackTrace();
}
}
第二种方法将timeout时间设置超长
try {
Class<?> c = Class.forName("java.lang.Daemons");
Field maxField = c.getDeclaredField("MAX_FINALIZE_NANOS");
maxField.setAccessible(true);
maxField.set(null, Long.MAX_VALUE);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
这里只给了解决方法和关键代码,如果还想了解的更加彻底查看本文参考:
本文参考:
http://www.courtier.cc/2017/04/28/关于timeoutexception这件小事/