Learn C# with OOPS Concept(Memory Management)

abhishek kumarabhishek kumar
6 min read

Lets understand memory management with simple C# Program as given below:-

class Employee

{

int Employee_Id; //member variables/instance variable of a class

string Employee_Name;

public static void Main()

{

Employee objEmployee=new Employee();

}

}

Question-1. Why we write class?

Ans.

  • In coding world, class is used to define the structure for the program.

  • Within the structure we can have multiple types of variables(instance, static etc), functions/methods, Constructor etc.

  • Every class which we define in coding is a sub-class of Object class(we have already discussed in Part-1).

  • The variables that we declare within the class scope is known as Member variable or instance variables of a class.

  • We can declare multiple types of variable as many and their data types are unpredictable, unpredictable means data-type may or may not be fixed.

  • Due to the above point, we can say that class is also known as reference type due to unpredictable data-types of variables.

  • The class that we created is a user defined data type.

  • Now, the Question arise where will be the memory of a class is allocated in RAM? and why different parts of codes are allocated in different parts of RAM?

  • Let's say having an items in home is not important, the important is how you manage and place the item in different parts of home into manageable and structure way, that's why memory management comes into picture in terms of programming.

  1. Every Programmer must have a knowledge of 4 (four) types of memory that are in RAM, the types are:-
  • Stack Memory.

  • Heap Memory.

  • Global Memory.

  • Cache Memory.

Let's understand Top-3 Memory as a programmer.

  1. Stack Memory
  • Stack Memory says i will store only 2-types of item:-

  • Variables which are fixed in size are stored in stack memory. Primitive types or value types variables:- integer, float, double, decimal etc are stored in stack memory.

  • function()/method() execution are done in stack memory.

  • Variables that are declared within the function as a parameter are stored in stack memory.

  • At last whenever the execution/task is completed, the Operating System will clean the stack memory for further process.

  1. Heap Memory:-
  • Heap memory says i will store those types of item which are not fixed by size, means heap memory store that items which are dynamic in nature.

  • We can also say that heap memory is a dynamic memory, means whenever the size of heap is full or shortage of memory than it can take the memory by hard drive.

  • class and string variables are stored in heap memory.

  • Why string is stored in Heap memory?, as we know string is primitive type(value type) but its size is not fixed. string is exception type, when we write a name we don't know the length of name and sometime names can be shorter or longer, that why string is also known as reference type and that is the reason it is stored in heap memory.

  1. Global Memory:-
  • In heap we have a upper/top section of memory and this memory is known as global memory.

  • For example we have refrigerator in which we have upper section for Ice cubes section or vegetable section likewise we have upper section for global memory in heap memory.

  • Variables which are global in nature are stored in global memory. example:- Constant variables, static variables, static read-only variables etc are stored in global memory. we will talk about variable part in next section.

Now lets dive deep into memory management by below line of code:-

class Employee

{

int EmployeeId; //member variables/instance variable of a class

string? EmployeeName;

public static void Main()

{

Employee objEmployee=new Employee();

}

}

Note:- There is question arise that, in which part of memory the variables that we declare inside the class are going to stored?

  1. As above points, we define that fixed variable(int, float,double) will be stored in stack and string variables are going to stored in Heap, but that's not true...you know why?

  2. The variables that we declare within the class are going to stored in Heap memory, because the variables are instance variables or member variables of a class and these variable are declare within the class scope.

  • Let's understand with real world example, Every house have a kitchen and in that kitchen we have stored kitchen related items which are used for cooking purpose. We can't store the kitchen related items in other part of the house. if Someone need kitchen related item or want to cook he/she needs to be go in the kitchen.

  • In terms of Employee, every employee have their id, Name, PhoneNumber etc which are private and if someone want to access these information need to get permission or some type of access mechanism from employee class.

  1. Now we can also say, class is a structure-block which consists member variables or instance variables, functions, constructor etc which are logically associated with the class name.

  2. Now the question is at what time the memory of class block is allocated in the Heap Memory?

  3. Let's break down the above code, within the code we have created an object for the class Employee in the Main() function/method:-

Employee objEmployee=new Employee();

Lets understand the above line of code from right to left:-

  1. new Employee();
  • when we write "new" along with "Employee()", block memory will be allocated for a class Employee in Heap and at this moment Default constructor will be called for a class.

  • "Employee()" it a default constructor. Why we called Default constructor?, because every class have its own behavior like a Doctor, Engineer have some own behavior.

  • class default/own behavior is declared as :- Employee();----> in terms of Employee class. We will understand constructor in later parts.

  • The main work of default constructor is that it allocate the variables that we declare inside the class within the class block memory which is itself stored in heap memory.

  • That's why variables that are inside the class are called member variables/instance variables of a class.

  • But keep in mind instance variables and instance readonly variables of a class are stored in Heap memory and constant variables, static variables and static readonly variables are stored in Global memory because these variables are global in nature (we will discuss later about variables).

"=" equal operator

  • What "=" equal operator do is that it assign a address of the class block memory which are created in Heap memory and that address is fixed can't be changeable like every house has address that can't changeable.

  • what "objEmployee" is?

  • "objEmployee" is a object that points to a address of a class Employee that are created by "=" operator.

  • "objEmployee" is pointer/reference pointer which refer to class address.

  • Keep in mind that object of a class is stored in stack memory, and that object point/refer to the address of a class. "objEmployee" is a object of class Employee and it is store in stack memory and from that it points to address of a class Employee.

  • Because of the pointer of a class Employee, we can access the class members like:- variables, functions/method through an "objEmployee" which is a object of class.

  • Let's see an Simple diagram of Memory Management of a C# code:-

By above Image you can understand more about memory management that are defined in above points in detail.

In Next part we will discuss on Types of Variables.

Hope you understand well and like about memory management of a program.

For Part-1 in which we discuss Object Class(Base class for every user defined class), How Code runs, compiled, understanding of Main() function, understanding of System Library.....click on this link :- Part-1

0
Subscribe to my newsletter

Read articles from abhishek kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

abhishek kumar
abhishek kumar

Serves 5+ years in IT field as a Website Administrator, E-Office Administrator, Web Developer, Full stack developer in different phase of Software Development Life Cycle(SDLC) including development, Implementation and testing.