I am using static variables in my app, lots of them. My question is, when I exit the app will they be still in memory..? If yes, how can I correct this. Thanks in advance.
-
3What do you mean by "signout"? Terminate the program/process/JVM? – Thilo Jun 21 '12 at 08:56
-
1@Thilo I think he is talking about Android apps... Please specify that, Raghav. – brimborium Jun 21 '12 at 08:58
-
3can you give more examples ? or i dont believe this question will live long – kommradHomer Jun 21 '12 at 08:59
-
3Low-quality questions always seem to attract low-quality answers... and this one is no exception. Voting to close. – Jean-François Corbett Jun 21 '12 at 09:07
-
yes specifically android, but I thought the concept would be same for static variables. – Rookie Jun 21 '12 at 09:23
-
2@Jean-François Corbett I think there are lots of good quality answers to this low quality question. – Rookie Jun 21 '12 at 09:24
-
@Raghev: yes, answer quality is good, but quite scattershot. No one really knows what the question is. Voting to close. – Thilo Jun 21 '12 at 10:44
-
I'm just asking will the static variables be in memory when the app exits. Is it confusing? – Rookie Jun 21 '12 at 10:46
-
@Raghav can you specify more the word app? Is it nromal java palication, that you start with java command starting new JVM for it? – Hurda Jun 21 '12 at 10:52
-
1It is an android application. – Rookie Jun 21 '12 at 10:57
-
1I think I've confused everyone..I think I'm not able to clear myself – Rookie Jun 21 '12 at 10:58
9 Answers
Static variable gets loaded when class is loaded by ClassLoader, and would be removed when it is Unloaded
For the Next Readers of this question-
As Everybody said in the answer that static variables are class variables. They remain in the memory until the class is not unload from JVM.
In Android you have seen that when we close any application then it does not close completely, It remains in the recent application stack, That you can see by long press the home button(On Most Devices).
Android itself kicked out those recent apps when the other app needs memory
In Android, static variable unload when-
-You force stop your app.
-Application crashes.
-You clear your app data.
-Switch off your Device.
-Android kicked out recent app

- 17,440
- 6
- 36
- 48
The static variable will live as long as the class is loaded in the JVM. When there are no more instances of the class being ran in the JVM the class will be unloaded and the static variable will be eligable for garbage collection.

- 7,512
- 7
- 39
- 72
-
10The class will not be unloaded, even when there are no instances left. It is only unloaded when the whole classloader goes away. – Thilo Jun 21 '12 at 09:00
In addition to the other answers, also note that if those static "variables" are actually "static final" primitive constants, then they don't really exist as separate entities at all, but their value gets compiled right into all the classes that use them (not just the one that defines them).

- 257,207
- 101
- 511
- 656
Static variables are associated with a class and they will live as long as the class is in the memory(which ceases to exist once your application terminates).

- 14,972
- 14
- 61
- 108
Sometimes, you want to have variables that are common to all objects. This is accomplished with the static modifier
. Fields that have the static modifier in their declaration are called static fields or class variables
. They are associated with the class, rather than with any object
.Every instance of the class shares a class variable, which is in
one fixed location in memory. Any object can change the value of a class variable, but class variables can also be manipulated without creating an instance of the class.
When instance is not use, garbage collector will be destroy it. it means that your instance will erased from memory.

- 8,370
- 4
- 37
- 60
I am using static variables in my app, lots of them.
Static variables are immune to automatic memory management and you should set them to null in the onDestroy
method (Android). They belong to a class for sure and it works exactly as pointed out by @Jigar Joshi.

- 5,965
- 14
- 31
- 57

- 33,374
- 10
- 68
- 106
-
3
-
2sry man, my fault but also its same in classic java, you must set them to null. – Simon Dorociak Jun 21 '12 at 09:01
if it is C/C++, and if you didnt collect the garbages, you should use a memory management program. İf it is java, close any "javaw" programs from memory and close jvm

- 11,469
- 4
- 45
- 97
-
2even in C/C++, when the process terminates (if this is what this question is about), the OS releases all memory. – Thilo Jun 21 '12 at 09:04
static variable's are called class variable and in way of scope they loaded when the class is loaded and unloaded when class is unloaded. for example a class variable like
private int classinVar;
is automatically initialized by its default value when class loaded, and same concept is with signout when you get signout then that class would go out of context with its static field.

- 1,251
- 2
- 14
- 33