Memory Management
- Every application requires some amount of memory to run.
- That memory will be allocated in the primary memory (RAM).
- For every variable, individual memory will be allocated.
- The RAM contains two memory locations.
- --> Stack
- --> Heap
- Now, we need to understand, where the application memory is getting allocated in the RAM.
- The storage area in the RAM depends on the data type that you are using for declaring the variable in the program.
- The data types are two types in C#
- --> Value Types
- --> Reference Types
Value Type
- Def: Whenever a data type is designed based on a structure, it can be called as Value Type.
- All the standard data types built based on structures.
- You can see the list of data types and respective structures here:
- So finally, the following are value types in C#:
- Structures
- Enumerations
- sbyte, byte, short, ushort, int, uint, long, ulong, float, double, decimal, bool, char, decimal, DateTime
- Note 1: The value types can‘t be inherited.
- Note 2: All the value types (structures and enumerations), are derived from a common base class called System.ValueType.
REFERENCE TYPES:
- Def: Whenever a data type is designed based on a class, it can be called as Reference Type.
- The following are the reference types in C#:
- Classes
- Interfaces
- String, Object
- Reference types support inheritance.
- The Value Type variables will be allocated in the Stack.
- The Reference Type variables (objects) will be allocated in the Heap.
THE “SYSTEM.OBJECT” CLASS:
- This class acts as super class for all other classes (pre-defined / user-defined classes).
- So, it can be called as Ultimate base class.
- The Object class offers the following methods
PROGRAM OF SYSTEM.OBJECT CLASS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ObjectClassMethods
{
class Program
{
static void Main(string[] args)
{
int x = 100;
int y = 100;
Console.WriteLine("x is " + x);
Console.WriteLine("y is " + y);
Console.WriteLine();
if (x.Equals(y))
Console.WriteLine("x is equal to y");
else
Console.WriteLine("x is not equal to y");
Console.WriteLine("\nx is the type of " + x.GetType());
string s = x.ToString();
Console.WriteLine("\nThe integer value after converting into string is: " + s);
Console.Read();
}
}
}
GARBAGE COLLECTOR:
- This is one of the components of CLR (Common Language Runtime).
- This component is dedicated for de-allocating the un-used memory of the application, automatically.
- This uses Mark and Compact algorithm for clearing the un-used memory.
- Mark: Markup the un-used objects and push those objects towards up.
- Compact: Clear the marked objects memory.
Note: The above functionality is in-built in Garbage collector component. Anyhow, there is a provision for the programmer to command the garbage collector to perform garbage collection at run time, programmatically. Then use the following method from System.GC class. Syn: System.GC.Collect();





No comments:
Post a Comment