3

I want to know where static variables are stored in Java.
There are already few questions on SO, like: where is a static method and a static variable stored in java. In heap or in stack memory

The link states that static variables are stored on the heap.

But following is an en excerpt from a book by Bill Veners ("Inside the Java Virtual Machine"):

The Method Area
Inside a Java Virtual Machine instance, information about loaded types is stored in a logical area of memory called the method area. When the Java Virtual Machine loads a type, it uses a class loader to locate the appropriate class file. The class loader reads in the class file--a linear stream of binary data-- and passes it to the virtual machine. The virtual machine extracts information about the type from the binary data and stores the information in the method area. Memory for class (static) variables declared in the class is also taken from the method area.

It clearly states that when a class is loaded, static variables are stored in the method area. The method area is different from the heap as far as I know. So the book is in contradiction with the provided SO link.

Can someone please clarify the confusion?

Community
  • 1
  • 1
Mandroid
  • 6,200
  • 12
  • 64
  • 134

2 Answers2

1

In the JVM memory model the reference to and value of static variables are both stored in the method area which itself is in the heap.

The method area is created on virtual machine start-up. Although the method area is logically part of the heap, simple implementations may choose not to either garbage collect or compact it.

https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-2.html#jvms-2.5.4

Alain O'Dea
  • 21,033
  • 1
  • 58
  • 84
  • Where does it say that static variables are in the method area? – user207421 Jun 27 '15 at 18:34
  • It doesn't anywhere I can find outside the OP's quote from Bill Venners' book. I was mainly trying to clear up the ambiguity about method area and heap that was the topic of the question, Now I'm very curious to find this in the JVM Spec. – Alain O'Dea Jun 27 '15 at 18:45
  • I doubt that you will. The essence of the method area is that it contains read-only stuff, like the text segment. – user207421 Jun 27 '15 at 23:44
  • This might be a HotSpot JVM-specific implementation detail then. I'll have to review Venners' book. It's been a while. – Alain O'Dea Jun 28 '15 at 01:04
0

From JVM Documentaion:

Method Area

The Java Virtual Machine has a method area that is shared among all Java Virtual Machine threads. The method area is analogous to the storage area for compiled code of a conventional language or analogous to the "text" segment in an operating system process. It stores per-class structures such as the run-time constant pool, field and method data, and the code for methods and constructors, including the special methods (§2.9) used in class and instance initialization and interface initialization.

The method area is created on virtual machine start-up. Although the method area is logically part of the heap, simple implementations may choose not to either garbage collect or compact it. This version of the Java Virtual Machine specification does not mandate the location of the method area or the policies used to manage compiled code.

Community
  • 1
  • 1
ganeshvjy
  • 399
  • 1
  • 12