Memory Management in Java : pt-3

Objects and Methods

This will be the third article in my series of articles on Memory Management in Java, I will be talking about how objects and methods are allocated space in Java.

For terminology, please refer to my previous articles on this Java Memory Management series.

Object

So, let's begin with an example of objects, it will be easier to understand -

class B {
    int var2;
    int var3;
    // more code
}

class A { 
    B child = new B();
    int var1;
    // more code
}

public static void main(String[] args) {
    A parent = new A();

    B obj1 = new B();
    B obj2;

    obj2 = obj1;  // creates a shallow copy
    obj1 = null;   // heap memory of obj1 is free

    // more code
 }

JVM first of all will load all the classes and will create .class files for all of them. Now, when we execute the code. JVM first of all will look for the main() method and will begin the execution from there.

Now after the first line of code, the reference to parent object is created in the stack in the main() method's frame. The non-static variables namely var1 and reference to object child will be allocated memory in the heap, alongside the non-static members var2 and var3 in the heap, the class B members.

1.jpeg

Then, the parent object reference will be flushed from the memory and obj1 and obj2 will be created.

B obj1 = new B();
B obj2;

This will result in the below memory map -

2.jpeg

obj2 = obj1;

This line means that we are assigning the reference of obj1 to obj2. This is known as shallow copy. No actual memory location is reserved.

This will result in the below-shown memory map -

3.jpeg

After the last statement - obj2 = null;

The reference from obj2 to memory location of obj1 is deleted and obj2 is pointing to null value. Thus obj2 is flushed from the memory and only obj1 remains.

obj1.jpg

After the obj1 is also flushed from the memory and flow of program is over.

Method

We'll take up an example here as well.

class Account {
    int p;
    int q;
}
class Student {
    public void m1() {
        int x = 20;
        System.out.println(x);
        m2(10);
    }

    public void m2(int b){
        boolean c = true;
        if(c == true)
            m3();
    }

    public void m3() {
        Account ref = new Account();
        System.out.println(ref.p);
    }
    public static void main(String[] args) {
        Student s1 = new Student();
        s1.m1();
    }
}

Here also same, JVM first of all will load all the classes and will create .class files for all of them. Now, when we execute the code. JVM first of all will look for the main() method and will begin the execution from there.

Herethe main() method calling method m1().

In stack area, a frame will be created from method m1().

1.jpeg

The variable X in m1 will also be created in the frame for m1 in the stack. (See image below).

2.jpeg

Method m1 is calling method m2. In the stack java, a new frame is created for m2 on top of the frame m1.

3.jpeg

Variable b and c will also be created in a frame m2 in a stack. Same method m2 is calling method m3. Again a frame m3 is created on the top of the stack (see image below).

4.jpeg

Now let say our method m3 is creating an object for class “Account,” which has two instances variable int p and int q. The statement new Account() will create an object of account in heap area. The reference variable “ref” will be created in a stack java. The assignment “=” operator will make a reference variable to point to the object in the Heap area.

5.jpeg

Once the method has completed its execution. The flow of control will go back to the calling method. Which in this case is method m2.

The stack from method m3 will be flushed out. Since the reference variable will no longer be pointing to the object in the heap, it would be eligible for garbage collection.

Once method m2 has completed its execution. It will be popped out of the stack, and all its variable will be flushed and no longer be available for use. Likewise for method m1.

Eventually, the flow of control will return to the start point of the program. Which usually, is the “main” method.

So, this is how Java manages the memory map for methods. Thank you for reading. Keep learning !