5

This is a follow up question to one of my previous questions.

I have a LoadingActivity which loads some graphics needed by all Activities and store it into a static class. I try to not load the LoadingActivity over again when pressing HOME and resume the app since it takes a lot of memory and runs out of it after several times whereby the graphics are already loaded, so no need to start the LoadingActivity again. My question is, how long does the static class live? Can I rely on it's availability after resuming the app, or may it be here since Android kills it due to memory issues or is it always here as long as the vm runs (that means as long as the phone is running)?

Community
  • 1
  • 1
Bevor
  • 8,396
  • 15
  • 77
  • 141
  • I believe they persist as long as your application process stays alive. But holding object references as Static is never a good idea in application development where device's memory is constrained. – waqaslam Sep 15 '14 at 20:41
  • 2
    Static classes are tied to the lifecycle of the application itself, which is at Android's whim AFAIK. NEVER ever rely on static classes. Period. – Machinarius Sep 15 '14 at 20:42
  • By "static class", do you mean references to "static objects"? You might want to clarify... it sounds like you are asking about classes declared with `static class`. – Alex Lockwood Sep 15 '14 at 20:55
  • @Machinarius Interesting! Java does not support static classes. Bevor, the question doesn't make any sense. Please clarify. As it is, voting to close. – Simon Sep 15 '14 at 21:34
  • @Simon I wrote that comment coming from a mostly-C# background where they DO exist (Been working a lot on Xamarin lately). Thanks for spotting the mistake. - OP: I meant static fields in classes. – Machinarius Sep 15 '14 at 21:51
  • @Simon A class containing static methods. – Bevor Sep 16 '14 at 07:11
  • I updated the question to clarify that. – Bevor Sep 16 '14 at 07:35

1 Answers1

6

As Simon points out, "static class" means different things in different languages, and Java does not have anything quite like the static classes in some other languages. But I don't think that's what you're talking about. You seem to be asking whether objects referenced by strong static references can be garbage collected. If so, the answer is no.

A class is represented by an object of class Class which is reachable via its ClassLoader. Anything referenced by the Class will therefore be reachable as long as the ClassLoader is reachable, which in the case of the system classloader is as long as the Java/Dalvik VM exists. But that is not as long as the phone runs, since an independent VM is created for each app. The entire process and VM in which an app is running may be killed whenever the app is in the background. When you return to the app, its classes will be reloaded.

If static fields are really the best choice, as opposed to a ContentProvider or foregound Service, then every time your app resumes you will need to check if the static references have been initialized and re-initialize them if they are null.

Community
  • 1
  • 1
Kevin Krumwiede
  • 9,868
  • 4
  • 34
  • 82
  • This is just wrong. There is no such thing as a static class, unless you are referring to a static inner class within an object. Are you talking about POJOs or Activities with static references? Or statically initialised class fields? Since Java does not have static classes, I am voting the answer down until you clarify. – Simon Sep 15 '14 at 21:38
  • In theory, a given ClassLoader and all of the classes it loaded could be unloaded by the VM. Android apps are loaded with a custom loader, not the "system" loader. In practice, Dalvik does not unload classes, so it's a moot point. – fadden Sep 16 '14 at 03:59