Android:系统什么时候卸载类?
这是一个非常奇怪的问题。我的应用程序运行得很好,但不知何故,如果我在应用程序运行时离开手机一两个小时,当我稍后返回时,我会收到以下错误:
java.lang.NoClassDefFoundError: yoga.database.Manager
at
yoga.YogaActivity.openDatabase(YogaActivity.java:294)
at
yoga.YogaActivity.initData(YogaActivity.java:275)
at
yoga.YogaActivity.onCreate(YogaActivity.java:102)
at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
at android.app.ActivityThread.access$2100(ActivityThread.java:116)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4203)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
at dalvik.system.NativeStart.main(Native Method)
我知道我的 Yoga.database.Manager 类在应用程序启动时已加载,但不知何故,当我离开手机时,该类一定已被系统卸载。
Android 会在一定时间后自动卸载类吗?当我的课程被卸载时我该怎么办?
上述错误导致我的应用程序崩溃,但我可以轻松地重新启动它并且它运行得很好。
该问题主要发生在我运行 Android 1.6 的 HTC Magic 手机上。
This is a very weird problem. My app that runs just fine but somehow if I leave my phone for an hour or two while my app is running, I get the following error when I come back to it later:
java.lang.NoClassDefFoundError: yoga.database.Manager
at
yoga.YogaActivity.openDatabase(YogaActivity.java:294)
at
yoga.YogaActivity.initData(YogaActivity.java:275)
at
yoga.YogaActivity.onCreate(YogaActivity.java:102)
at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
at android.app.ActivityThread.access$2100(ActivityThread.java:116)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4203)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
at dalvik.system.NativeStart.main(Native Method)
I know for a fact that my yoga.database.Manager class was loaded at the time when the app was launched, but somehow the class must have been unloaded by the system as I left the phone.
Does Android unload classes automatically after certain periods? What can I do when my class gets unloaded?
The above error causes my app to crash, but I can easily re-launch it and it runs just fine.
The problem occurs mostly on my HTC Magic phone running Android 1.6.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Dalvik VM 当前不卸载类。如果确实如此,则只有当与特定类加载器关联的所有类都可以立即卸载时,它才能这样做,而在您的应用程序运行时则不会出现这种情况。
您需要检查 logcat 输出是否有导致此异常的错误。获得 NoClassDefFoundError 的一种方法是在相关类的初始化过程中出现故障;如果发生这种情况,日志中可能会留下痕迹。
(当然,现在 logcat 输出可能早已消失,但如果问题是可重复的,您下次会想要捕获它。)
The Dalvik VM doesn't currently unload classes. If it did, it would only be able to do so when all classes associated with a particular class loader could be unloaded at once, which will not be the case while your app is running.
You need to check the logcat output for errors leading up to this exception. One way to get a NoClassDefFoundError is for something to fail during initialization of the class in question; if that happened there would likely be a trail in the log.
(Of course, by now the logcat output is probably long gone, but if the problem is repeatable you'll want to capture it next time.)
您正在使用自定义类加载器玩游戏吗?
我问,因为如果
yoga.YogaActivity
和yoga.database.Manager
是同一个 Android APK 文件中的标准 Java 类,那么内存中不应该有一个类,而另一个则不是。我从未见过 Android 一次性卸载类——至少,我以前从未遇到过这个特殊问题。还:
您可能想澄清一下您的意思。你的意思是你把你的活动留在前台吗?您的意思是按 HOME 键然后在几小时后返回?你还有别的意思吗?
Are you playing games with custom classloaders?
I ask, because if
yoga.YogaActivity
andyoga.database.Manager
are standard Java classes in the same Android APK file, you should not have one in memory and the other not. I have never seen Android unload classes on a one-off basis -- leastways, I have never run into this particular problem before.Also:
You might want to clarify what you mean by this. Do you mean that you leave your activity in the foreground? Do you mean that you press HOME and then come back to it hours later? Do you mean something else?