Sunday 13 November 2016

// // Leave a Comment

What happens in memory when you run Java Program.

As we are knowing java is object oriented programming.when you are writing java program with class then when you are going to run java program you have to provide memory to that class.
As class in only skeleton it doesn't possess any memory so to give that memory you have to create object of that class.
we will further discuss that thing.

As we know java possesses two types of memory one is called as 
-stack memory
and another is 
-heap Memory 
 As stack memory holding memory for methods.
and heap memory holding memory for objects.

Let us consider simple java program.

  1.     class Student{  
  2.  int id;//data member (also instance variable)  
  3.  String name;//data member(also instance variable)  
  4.   Static int a;
  5.  public static void main(String args[]){  
  6.   Student s1=new Student();//creating an object of Student  
  7.   System.out.println(s1.id);  
  8.   System.out.println(s1.name);  
  9.  }  
  10. }
In the above java program  we are able to see student class contains one method that is public static void main() as it is static means its memory location will be fixed throughout the execution of program.memory location allocated to static method will be fixed and Java Virtual Machine cannot change or replace its memory location throughout the program.
as main() is method so it will get memory space from stack memory.
at the line number 6 you can able to see that we created object for the same class.
now the s1 is instance of student class.
Let us discuss about object creation 
Student s1=new Student();
in the above line Student is class name and s1 is object name or you can say it as reference variable.

when you use new keyword in program after = symbol it will create memory space for the object.
as we can able to see in diagram that heap memory having object s1 and the id and and name are instance variable in that. so now you will able to refer those variable with reference of s1.
so id becomes s1.id and name becomes name.id.
Student() is constructor so we will discuss about that later on.
but in the program at line number 4 there is static variable a.
so we will discuss about it.
as we know that to use static variable there is no need to create object for the same.
but where is the memory location for that.
we will try to find out answer for the same.
now we will discuss about heap memory in the details.
Heap memory divided into three major parts
-Young Generation Memory 
-Old Generation Memory
-Permanent Generation Memory
As you can able to see in the above figure newly created objects resides in Young Generation memory. If objects present in system for long time so the state of objects is maintained and stored by Old Generation memory. When any class posses static variable then it get store in Permanent Generation Memory and memory location for static variable will be fixed throughout the program.








0 comments :

Post a Comment