Android:系统什么时候卸载类?

发布于 2024-08-25 08:28:52 字数 1370 浏览 5 评论 0原文

这是一个非常奇怪的问题。我的应用程序运行得很好,但不知何故,如果我在应用程序运行时离开手机一两个小时,当我稍后返回时,我会收到以下错误:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尛丟丟 2024-09-01 08:28:52

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.)

番薯 2024-09-01 08:28:52

您正在使用自定义类加载器玩游戏吗?

我问,因为如果 yoga.YogaActivityyoga.database.Manager 是同一个 Android APK 文件中的标准 Java 类,那么内存中不应该有一个类,而另一个则不是。我从未见过 Android 一次性卸载类——至少,我以前从未遇到过这个特殊问题。

还:

我离开手机一两个小时
当我的应用程序运行时

您可能想澄清一下您的意思。你的意思是你把你的活动留在前台吗?您的意思是按 HOME 键然后在几小时后返回?你还有别的意思吗?

Are you playing games with custom classloaders?

I ask, because if yoga.YogaActivity and yoga.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:

I leave my phone for an hour or two
while my app is running

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文