Rookie
Rookie

Reputation: 8800

static variables, what is their life span?

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.

Upvotes: 18

Views: 23918

Answers (9)

Simon Dorociak
Simon Dorociak

Reputation: 33515

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.

Upvotes: 3

T_V
T_V

Reputation: 17580

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  

Upvotes: 12

RTA
RTA

Reputation: 1299

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.

Upvotes: 2

Jigar Joshi
Jigar Joshi

Reputation: 240996

Static variable gets loaded when class is loaded by ClassLoader, and would be removed when it is Unloaded

Upvotes: 29

huseyin tugrul buyukisik
huseyin tugrul buyukisik

Reputation: 11926

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

Upvotes: 2

Thilo
Thilo

Reputation: 262842

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

Upvotes: 8

Dmitry Zagorulkin
Dmitry Zagorulkin

Reputation: 8548

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.

Upvotes: 3

bNd
bNd

Reputation: 7640

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.

Upvotes: 9

Kazekage Gaara
Kazekage Gaara

Reputation: 15052

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

Upvotes: 4

Related Questions