Memory Allocation in Java

In this blog post, we'll dive into memory allocation in Java, and discuss how it works and how it can affect the performance of your Java applications.

There are mainly 5 types of memory allocations available in Java :

  1. Class Memory

  2. Heap Memory

  3. Stack Memory

  4. Programme Counter-Memory

  5. Native Method Stack Memory.

Java Heap memory and Stack memory are what we should know about in detail to make programs better for referencing and storing values.

Observe the below java program containing variables and objects, we will look at how memory would be allocated for this program.

class Test1 {
    int a; 
    int b;
    static int c;
}

class Test2 {
    Test1 t = new Test1();
}

Java Heap

The heap is a memory area where Java stores all objects. The heap is created when the JVM starts up and may increase or decrease in size during the runtime of the program. When an object is created in Java, it is allocated memory on the heap. The size of the object is determined by its class, and each object has a header that contains information about the object, such as its class, size, and other metadata. Java Heap is divided into two parts and it's managed by JVM:

  1. Young Space

  2. Old Space

In our java program, t is an object of Test1() and hence it would be stored in the heap.

Java Stack

The stack is a memory area where Java stores method invocations and local variables. Each time a method is called, a new frame is added to the stack. The frame contains the method's parameters and local variables, and it is removed from the stack when the method completes. The stack is much smaller than the heap, and it is used for short-lived data.

Integer a and b in class Test1 would be stored in stacks as they are local variables of Test1.

Stack vs Heap

The heap is where objects are stored, and the stack is where method invocations and local variables are stored. All objects in Java are created in the heap, and each object occupies a specific amount of memory based on its type.

Metaspace

Metaspace is the memory allocated for storing static variables and objects. It is managed by the native OS. Static int c would be stored in the metaspace.

Java Garbage Collection

The heap is managed by the Java Garbage Collector (GC), which automatically frees up memory that is no longer in use by the program. The GC checks object that is no longer in use and release the memory they occupy back to the heap. This process is called garbage collection, and it happens automatically in the background as the program runs. The GC ensures that there is enough memory available in the heap for new objects to be created. The young space in the heap is further divided into three parts - EDEN, Survivor 1 and Survivor 2. The null and unreferenced objects are moved from EDEN to Survivor 1 and then to Survivor 2 by garbage collection.

Memory Leakage in Java

Memory leaks are a common problem in Java applications. A memory leak occurs when an application fails to release memory that is no longer needed. This can cause the program to use more and more memory until it eventually crashes. In Java, memory leaks can occur when objects are not properly released by the program, or when there are circular references between objects. A memory leak can also occur if an object is created but not used.

This can be avoided by setting object references to null or using the finalize() method to release resources before an object is cleared by garbage collectors. Also one should avoid circular references between objects and should be careful to only create objects that are actually needed.

Conclusion

By being mindful of memory allocation, Java developers can create efficient and reliable Java applications. The heap is used to store objects, and the stack is used to store method invocations and local variables. One should be careful to avoid memory leaks by releasing resources when they are no longer needed and by avoiding circular references between objects.