Memory Management in JAVA
One of the most important features of Java is its memory management. Since Java uses automatic memory management, it becomes superior to those languages that do rely on programmers for their memory management. Having said that, although Java automates its memory management, it is very important to understand the functionality so that we do not end up in a situation where we start blaming JAVA for any out of memory because of our coding issues.
When we say JAVA memory, we mean JVM memory. JVM primarily defines 5 run-time data areas which are used in program execution:
Method Area | Heap Memory | Stack | PC Registers | Native Method Stack
Let us try to understand each one of them and the linkage between them:
1. Method Area:
- All the class data is available in the method area.
- Corresponding static variables associated with the class are also part of the Method Area.
- Its size can be customized as peruse.
- Apart from the class data, it also stores superclass name, interface name, and constructors information
- If the memory in the method area can not satisfy an allocation request, JVM throws an OutOfMemoryError.
2. Heap Area:
- All the object data is available in the Heap area.
- The instance variable will also be stored in Heap Area.
- It is very important for a programmer to understand the Heap Area as all the object-level information is present here and garbage collection keeps a check for this area only.
- Its size can be customized as per need.
- Heap is further divided into few parts which we will be discussing in detail along with garbage collection in our next blog.
- If more heap is required for the storage management system, JVM throws an OutOfMemoryError.
3. Stack Area:
- For every thread, a separate run-time stack is created and is stored in Stack Area.
- Thus, if we have "n" number of threads, "n" number of stacks will be created and will be stored in the stack area.
- Further, each entry in each stack contains three parts i.e. Local variable array, operand stack, and Frame data, and these three are collectively known as a stack frame.
- If more JVM stack memory is required than is permitted, JVM throws a StackOverflowError. On the other hand, if JVM tried to expand the Stack memory or if there is a shortage of memory during initial JVM stack creation for a new thread, JVM throws OutOfMemoryError.
Relation between Stack and Heap:
1 | String s = new String("abc"); |
- In the above syntax, an object of type String and value "abc" will be created in the Heap area.
- "s" will be created in Stack which will refer to the object created in the Heap area.
4. PC Registers:
- For each thread, a stack is created in the stack area and a PC register is created in PC Registers memory.
- Thus, if we have "n" number of threads, then again "n" number of stacks will be created in the stack area, and "n" number of PC registers will also be created.
- It holds the address of the currently executing instruction and it moves to the next instruction once the current instruction is executed.
Relation between Stack Area and PC Registers:
- For each thread created in Stack Area, a program counter is associated with it.
- PC register stores the address of the current running instruction.
5. Native Method Stack:
- It is similar to Stack Area but the difference is that the native methods of JAVA are stored in this area.
- If we deep dive into the architecture of JVM, we will find that there is a JNI (Java Native Interface) which deals with all the native methods.
- If more JVM native stack memory is required than is permitted, JVM throws a StackOverflowError. On the other hand, if JVM tried to expand the native stack memory or if there is a shortage of memory during the initial creation for a new thread, JVM throws OutOfMemoryError.
0 comments:
Post a Comment