Android: When do classes get unloaded by the system?

前端 未结 2 408
小蘑菇
小蘑菇 2020-12-11 17:18

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 ba

相关标签:
2条回答
  • 2020-12-11 18:01

    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?

    0 讨论(0)
  • 2020-12-11 18:21

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

    0 讨论(0)
提交回复
热议问题