In every programming language, memory is a vital resource and is also scarce in nature. Hence it’s essential that the memory is managed thoroughly without any leaks. Allocation and deallocation of memory is a critical task and requires a lot of care and consideration. In this article, we will understand the storage of static methods and static variables in Java.
Java Virtual Machine(JVM) is an engine that provides a run time environment to drive the java code. It converts the java byte code into machine language. The JVM has two primary functions. They are:
- It allows java programs to run on any device or OS to fulfil WORA (Write Once Run Anywhere) principle.
- It manages and optimizes program memory.
The JVM memory manager creates memory pools during the runtime of the program.
Let’s understand the concept of memory pools in java. There are two types of memory pools namely the stack memory and the heap memory. The main difference between stack memory and the heap memory is that the stack is used to store only the small datatypes whereas heap stores all the instances of the class. And, since java either implicitly or explicitly extends the class from Object.class, every class we create is a collection of objects. This also means that we cannot use a method or a variable unless we instantiate it with a new keyword. As soon as we instantiate the methods, JVM allocates some memory on the heap and it stores the address of the instance on the stack. After this, the methods and variables can be used. In order to get a better understanding of how the new keyword words, let’s take an example. Let’s take a bird class. Whenever a new bird is found, the number of birds need to be incremented.
Let’s try to implement this code:
Clearly, this method cannot be used to count the number of birds because at every instance, a new heap is being allocated and for all the instances, the number of birds is being initialized from 0 and is incremented to 1. Therefore, we are creating three independent birds where, in every bird object, the number of birds is 1. In order to avoid this, we initialize the methods and variables as static. This means that the variable or method is not changed for every new object creation. Since these methods and variables cannot be stored in a normal heap, they are stored in a special area called permanent generation(PermGen).
The main difference is that the heap is the auto growing space, with RAM memory as its constraints, whereas this PermGen has a fixed space allocation, and this is shared with all the instances.
Let’s implement this by changing the number to a static variable.
Note: In Java 5 and 6, PermGen space was used. But due to major changes in memory model in Java 8, storage specification has also been changed. Now a new memory space “MetaSpace” has been introduced where all the names fields of the class, methods of a class with the byte code of methods, constant pool, JIT optimizations are stored. The main reason for this change of PermGen in Java 8.0 is that it is tough to predict the size of PermGen, and it helps in improving garbage collection performance.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Understanding "static" in "public static void main" in Java
- Static and non static blank final variables in Java
- Difference between static and non-static variables in Java
- Static Variables in Java with Examples
- Understanding public static void main(String args) in Java
- Are static local variables allowed in Java?
- Static methods vs Instance methods in Java
- Why non-static variable cannot be referenced from a static method in Java
- Assigning values to static final variables in Java
- Why can't static methods be abstract in Java?
- Can we Overload or Override static methods in java ?
- Difference between static and non-static method in Java
- Types of Static Analysis Methods
- Internal static variable vs. External static variable with Examples in C
- static keyword in java
- Initialize a static Map using Java 9 Map.of()
- Static import in Java
- Static class in Java
- Static blocks in Java
- Static Control Flow in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.