7

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.

2 Answers 2

9

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

3
  • Thank you for your input! I'll look into the logcat.
    – user277827
    Mar 20, 2010 at 10:43
  • @fadden, Do you have a source/cite for "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" ?
    – Pacerier
    Nov 20, 2014 at 9:11
  • 4
    For the first part, I am the source -- I was generally responsible for the Dalvik runtime at the time (and continued to be until early 2011, shortly before development on the runtime essentially stopped to focus on what is now ART). The second part is just the way class loaders work. For a class to be unloaded, there must be no references to it, but every class is referenced by the class loader, and every class references its loader. So until a loader and all of the classes it loaded are unreachable, the GC won't discard them. I don't have an authoritative reference to cite for that.
    – fadden
    Nov 20, 2014 at 17:11
2

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?

7
  • Hi, thank you for your response. I'm not playing with custom classloaders, and yoga.YogaActivity and yoga.database.Manager are standard Java classes in the same Android APK file. As for leaving the phone, I left the phone while the YogaActivity was in the foreground. After a while, the phone's screen turned off automatically and (presumably) the phone went to sleep. When I wake up the phone, the foreground activity (YogaActivity) should show up on the screen but the app crashed with the NoClassDefFoundError instead.
    – user277827
    Mar 17, 2010 at 7:01
  • 2
    I have encountered similar probs. In my app, a class has static variables. After starting my app and then turning off the screen, a while later I find the app in an impossible state. Specifically, a variable "public static boolean isInitialized = false;" which is modified in only one place, to a true, was found to be false, even though the true assignment had clearly been executed. Symptoms were consistent with the class holding the static being unloaded and reloaded during a long period where the activity that ref'd the class was killed, and then recreated later. Jul 10, 2011 at 5:44
  • 4
    @Mesocyclone: Most likely your process was simply terminated and later restarted. That happens all the time. Never never never never never rely on static data members for anything more than a cache or other transient spot for data. Jul 10, 2011 at 11:37
  • 2
    @Arvis: As I wrote, never never never never never rely on static data members for anything more than a cache or other transient spot for data. "Rely on" files, databases, etc. for persistent data. Dec 14, 2012 at 15:31
  • 2
    @DavidT.: Those too go "poof" when the process is terminated. In particular, a static thread pool is a massive code smell. Dec 16, 2013 at 12:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.