Java Memory Management

Java is a well crafted language. From the early days of Oak to modern Java, there are lots of emphasis given to make the language more programmer friendly. Unlike other low/medium level languages like C or C++, the memory management mechanism of Java is more programmer independent. We as programmers do not have to worry about freeing the allocated memory and allocate it to other memory objects using explicit memory handling mechanisms.

The Java program memory mainly divides into two: stack and the heap. Stack is a memory area for memory references and storing the primitive variables used in the code. It is block scoped. As the name suggests stack is a Last in First Out data structure. The data from the stack will get popped out whenever the program execution exiting the scope of that block. In a multi threaded environment, every thread has its own stack and the data inside the stack is non shareable. Whenever we are calling a method, we are passing the copy of a primitive variable. So it creates a new entry in the stack (Passing by value).

Contrary to stack, heap is a very large memory area. Heap is used to store the java objects generated during the life time of an application. It is a shared space between multiple threads. For every object in a heap there should be a reference present in the stack, for using the heap object in the program. Unlike primitives, objects are passed by reference in Java. Means, in a method call we are passing a reference of already existing heap object (Passing by reference).

Consider below code snippet:

public void displayCustomer(){

String customerName = "John";

Customer cust = new Customer(customerName);

int customerId = getCustomerId(cust);

}

public getCustomerId(Customer customer){

int length = customer.getName().length();

return length;

}

  1. String customerName = "John";

It creates a reference in the stack called 'customerName' and this reference will be pointing to the String object created in heap with content 'John'.

  1. Customer cust = new Customer(customerName);

This line creates an object of type Customer in the heap memory and a reference 'cust' will be created in the stack.

  1. String customerId = getCustomerId(cust);

This line calls a method, by passing the reference of object of Customer type.

4. public getCustomerId(Customer customer)

Here new reference will be created in the stack pointing to the same Customer object created.

5. int length = customer.getName().length();

In this line getName() method will be create a temporary reference to the Customer.name string. The length() mathod returns the length of the name and store it in the length variable.

6. return length;

Program execution will be returned to the calling method. All the entries present in the stack for the getCustomerId() method will be removed (Customer, length).

7. String customerId = getCustomerId(cust);

customerId entry will be created in the stack and it will store the return value of the getCustomerId() method.