In Java, static variables and static methods are associated with the class rather than instances of the class. This difference in association affects where they are stored in memory. Let’s break down how memory is managed for static members:
Memory Areas in Java
Java divides memory into several areas for better organization and management, such as:
- Heap: Where objects and instance variables are stored.
- Stack: Stores method call frames, local variables, and function call data.
- Metaspace (previously PermGen): Where class-level information (including static variables and methods) is stored.
- Code Cache: Used for Just-In-Time (JIT) compiled code.
Where Static Variables Are Stored
- Static Variables:
Static variables are stored in the Metaspace (introduced in Java 8, replacing PermGen) or Permanent Generation (for Java versions earlier than 8). - When a class is loaded by the JVM (Java Virtual Machine), all its static variables (also known as class variables) are stored in the Metaspace. They are allocated memory only once per class, not per instance.
- This memory stays allocated until the class is unloaded (which typically happens only when the class loader is garbage collected or the JVM shuts down).
Where Static Methods Are Stored
- Static Methods:
Static methods are also stored in the Metaspace along with class metadata. Since static methods belong to the class and not to any object instance, their information, like the method code, is loaded into the Metaspace during the class loading process. - Method Code: The bytecode of all methods (whether static or not) is stored in the Metaspace. However, static methods do not rely on instance-specific data and are directly associated with the class.
![static methods and static variable memory allocation](https://i0.wp.com/techbugger.com/wp-content/uploads/2024/10/image.png?resize=640%2C640&ssl=1)
How it Works:
- When a class is loaded by the ClassLoader, its static variables and methods are stored in the Metaspace.
- The JVM maintains a pool of all loaded classes and their static members in the Metaspace. This ensures that static members are accessible without creating an instance of the class.
Memory Layout in Summary:
- Heap: Stores object instances and non-static fields (instance variables).
- Stack: Stores local variables, method call information, and function call data during runtime.
- Metaspace: Stores class-level information such as static variables, static methods, method code, and other class metadata.
Example:
class MyClass {
static int staticVar = 100;
static void staticMethod() {
System.out.println("Static method called");
}
}
staticVar
is stored in Metaspace, and there is only one copy ofstaticVar
for all instances ofMyClass
.staticMethod()
is also stored in Metaspace, and it can be called using the class name without creating an instance:MyClass.staticMethod()
.
Characteristics of Static Variables and Methods:
- Lifetime: They live for the duration of the program, or until the class is unloaded.
- Memory: They consume memory in the Metaspace (for class metadata and static content), which is separate from the heap where instances are stored.
- Accessibility: Static members are accessible via the class name, and you don’t need an object instance to access them.
Conclusion
- Static variables and static methods are stored in the Metaspace (or PermGen in older versions of Java), along with other class-level information like class structures and method bytecode.
- These members are loaded and initialized when the class is loaded and remain in memory as long as the class is in use or until the JVM shuts down.