简体   繁体   中英

Where java static variables are stored in memory?

class A{
 static int i = 10;
 static int j = 20;

 static void getname(){

   }

}

Where will these variable be stored in memory ?

simply said , Static Variables are stored in HEAP . Classes and all of the data applying to classes (not instance data) is stored in the Permanent Generation section of the heap.

If you need elaborated answer , refer this

static allocation in java - heap, stack and permanent generation

First, static member variables are stored in the Permanent Generation area of heap.

Your example contains primitive type variables, they will be stored in the PermGen.

If those were object type variables, eg static Object x = new Object(); , then the reference x would be stored in PermGen whereas the Object itself would be placed in Young Generation of the heap.

I think for most implementations of some JVMS its particular to the PERM-GEM... but I have no proof.. the truth of the matter is... its up to the JVM where these values are stored. It is a variable... it could be stored in many different fashions depending on the JVM implementation.

If you are seeing memory problems, I would probably look at whats being assigned and not how its being assigned.

If you need more info, or your question is more implementation specific; lets rephrase your question and I will repost a better answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM