The JVM divided the memory into following sections.

1. Heap

2. Stack

3. Code

4. Static

This division of memory is required for its effective management.

  • The code section contains your bytecode.
  • The Stack section of memory contains methods, local variables and reference variables.
  • The Heap section contains Objects (may also contain reference variables).
  • The Static section contains Static data/methods.

Of all of the above 4 sections, you need to understand the allocation of memory in Stack & Heap the most, since it will affect your programming efforts.

Difference between Local and instance variable

Instance variable are declared inside a class but not inside a method

class Student{ 
int num; // num is  instance variable 
public void showData{}

Local variable are declared inside a method including method arguments.

public void sum(int a){

int x = int a +  3;

// a , x are local variables</strong>

}

The video demonstrates difference between stack and heap


Please be patient. The Video will load in some time. If you still face issue viewing video click here


Let's take an example to understand this better.

Consider that your main method calling method m1

public void m1{
int x=20
}

In the stack java, a frame will be created from method m1.

Java Stack and Heap

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

Java Stack and Heap

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

Java Stack and Heap

Java Stack and Heap

Variable b and c will also be created in a frame m2 in a stack.

public void m2(int b){
boolean c;
}

Java Stack and Heap

Same method m2 is calling method m3. Again a frame m3 is created on the top of the stack (see image below).

Java Stack and Heap

Java Stack and Heap

Now let say our method m3 is creating an object for class "Account," which has two instances variable int p and int q.

Account {
             Int p;
             Int q;
       }

Here is the code for m3

public void m3(){
	Account ref = new Account();
	// more code
}

The statement new Account() will create an object of account in heap.

Java Stack and Heap

The reference variable "ref" will be created in a stack java.

Java Stack and Heap

The assign "=" operator will make a reference variable to point to the object in the Heap.

Java Stack and Heap

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

Java Stack and Heap

The stack from method m3 will be flushed out.

Java Stack and Heap

Since the reference variable will no longer be pointing to the object in the heap, it would be eligible for garbage collection.

Java Stack and Heap

Once m2 has completed its execution. It will be poped 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 startpoint of the program. Which usually, is the "main" method.

Summary:

  • When a method is called , a frame is created on the top of stack.
  • Once a method has completed execution , flow of control returns to the calling method and its corresponding stack frame is flushed.
  • Local variables are created in the stack
  • Instance variables are created in the heap & are part of the object they belong to.
  • Reference variables are created in the stack.

Point to Ponder: What if Object has a reference as its instance variable?

public static void main(String args[]){ A parent = new A(); //more code } class A{ B child = new B(); int e; //more code } class B{ int c; int d; //more code } 

In this case , the reference variable "child" will be created in heap ,which in turn will be pointing to its object, something like the diagram shown below.

Java Stack and Heap

 

YOU MIGHT LIKE: