This article discusses memory management and associated Java concepts.
Memory is a valuable resource that is also quite finite in nature.
When something is finite in nature, it is critical to manage it well to ensure that it is used effectively.
Memory allocation and deallocation is a significant process that demands great attention and consideration. Because the programmer has direct access to memory in languages such as C, memory leaks are a real possibility.
However, in Java, memory management is a seamless autonomous process handled by the JVM (Garbage Collector, to be specific), so the programmer does not have to worry about memory allocation and deallocation.
To understand Memory Management in Java, we must know the following:
A) JVM Memory Structure
B) Working of Garbage Collector
JVM Memory Structure
Heap Area:
- Stores actual objects in the memory
- Memory allocation for all class instances and arrays
- It can be fixed or dynamic in size, depending on the system configuration.
Scanner sc = new Scanner(System.in);
The above statement creates the object of Scanner class which gets allocated to heap whereas the reference variable ‘sc’ gets pushed to the stack which we will see in the below section.
Method Area:
This memory is allocated for class structures, method data and constructor field data, and also for interfaces or special method used in class.
Can be of a fixed size or expanded as required by the computation. Needs not to be contiguous.
JVM Stacks:
- Used to store data and partial results which will be needed while returning value for method and performing dynamic linking.
- Stacks can either be of fixed or dynamic size. The size of a stack can be chosen independently when it is created.
- Eg: int a = 10, here reference variable 'a' will be stored in the Stack memory which points to the heap memory.
Native method Stacks:
Also called as C stacks, native method stacks are not written in Java language. This memory is allocated for each thread when its created.
Program counter (PC) registers:
- Each JVM thread which carries out the task of a specific method has a program counter register associated with it.
- The non native method has a PC which stores the address of the available JVM instruction whereas in a native method, the value of program counter is undefined. PC register is capable of storing the return address or a native pointer on some specific platform.
Garbage Collection:
- This process is initiated by the JVM and is completed or withheld according to the JVM garbage collection mechanism. It relieves the programmer's workload by conducting memory allocation and deallocation automatically.
- The garbage collection operation pauses the remainder of the processes or threads, which is costly in nature. This is an unpleasant situation for the user, but it is avoidable by employing numerous garbage collector-based methods. This process of applying an algorithm is known as Garbage Collector tuning, and it is critical for enhancing a program's speed.
- Another option is to use generational garbage collectors, which add an age field to objects that are allocated memory. As more items are generated, the garbage list expands, increasing the garbage collection time. Objects are classified and assigned a 'age' based on the number of clock cycles they have survived. The garbage collection effort is distributed in this manner.
Knowing how the program and its data are stored or arranged is critical since it helps the programmer develop optimal code in terms of resources and consumption. It also aids in the detection of memory leaks or inconsistencies, as well as the troubleshooting of memory-related issues.