Sunday, March 9, 2014

Memory Management

Memory Management

  1. Every application requires some amount of memory to run.
  2. That memory will be allocated in the primary memory (RAM).
  3. For every variable, individual memory will be allocated.
  4. The RAM contains two memory locations.
    • --> Stack
    • --> Heap
  5. Now, we need to understand, where the application memory is getting allocated in the RAM.
  6. The storage area in the RAM depends on the data type that you are using for declaring the variable in the program.
  7. The data types are two types in C#
    • --> Value Types
    • --> Reference Types
Value Type
  1. Def: Whenever a data type is designed based on a structure, it can be called as Value Type.
  2. All the standard data types built based on structures.
  3. You can see the list of data types and respective structures here:
  4. 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
  5. Note 1: The value types can‘t be inherited.
  6. Note 2: All the value types (structures and enumerations), are derived from a common base class called System.ValueType.
REFERENCE TYPES:
  1. Def: Whenever a data type is designed based on a class, it can be called as Reference Type.
  2. The following are the reference types in C#:
    • Classes
    • Interfaces
    • String, Object
  3. Reference types support inheritance.
  4. The Value Type variables will be allocated in the Stack.
  5. 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:
  1. This is one of the components of CLR (Common Language Runtime).
  2. This component is dedicated for de-allocating the un-used memory of the application, automatically.
  3. This uses Mark and Compact algorithm for clearing the un-used memory.
  4. Mark: Markup the un-used objects and push those objects towards up.
  5. 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();