1

Are the static members of a class - static variables, static blocks, and static methods stored in the Metaspace region after Java 8? If not, where are they stored?

There is no concrete information regarding the same elsewhere. All that I was able to get the information was that static variables are allocated memory at the time of class loading. But my question is where exactly is the memory being allocated - stack or heap or metaspace?

Thanks in advance!

trincot
  • 317,000
  • 35
  • 244
  • 286
  • 3
    @Thomas Please check the link provided in the answer (http://openjdk.java.net/jeps/122). Here, it says that **The proposed implementation will allocate class meta-data in native memory and move interned Strings and class statics to the Java heap.** – Syed Zabi Ulla Nov 26 '21 at 14:12
  • Note that this is specific to the JVM implementation, i.e. JEP 122 specifically states it applies to HotSpot. Other implementations like OpenJ9 don't have Metaspace so they'd store the static fields somewhere else (from what I could find OpenJ9 only uses heap memory). – Thomas Nov 29 '21 at 07:21

1 Answers1

1

The static variables are stored in the Heap itself.From Java 8 onwards the PermGen Space have been removed and new space named as MetaSpace is introduced which is not the part of Heap any more unlike the previous Permgen Space. Meta-Space is present on the native memory (memory provided by the OS to a particular Application for its own usage) and it now only stores the class meta-data.

The interned strings and static variables are moved into the heap itself.

For official information refer : JEP 122:Remove the Permanent Gen Space

Arthur Klezovich
  • 2,595
  • 1
  • 13
  • 17