Static references are cleared–does Android unload classes at runtime if unused?

Both you (@Matthias) and Mark Murphy (@CommonsWare) are correct in what you say, but the gist seems lost. (The use of volatile is correct and classes are not unloaded.)

The crux of the question is where initialize is called from.

Here is what I think is happening:

  • You are calling initialize from an Activity *
  • Android needs more memory, kills the whole Process
  • Android restarts the Application and the top Activity
  • You call getInstance which will return null, as initialize was not called

Correct me if I’m wrong.


Update:
My assumption – that initialize is called from an Activity * – seems to have been wrong in this case. However, I’ll leave this answer up because that scenario is a common source of bugs.

Leave a Comment