The document provides an overview of the Microsoft .NET Framework. It discusses how the .NET Framework uses garbage collection to automatically free up unused memory, dividing the heap memory into multiple generations. It describes how objects are initially created in Generation 0 and can get promoted to higher generations as they survive garbage collections. The document also provides examples of methods like GetTotalMemory() and Collect() to interact with the garbage collector, and discusses how the Common Type System allows .NET to support multiple programming languages through common data types.
Python Notes for mca i year students osmania university.docx
Introduction of c# day2
1. 2
Microsoft .NET Framework Overview
Presented By
Arun Kumar Singh & Team
Master of Computer Application
Microsoft® Certified Professional
MCP id- 7802107
www.aumcp2013.jimdo.com arunsingh026@hotmail.com
All Right Reserved@ AUMCP group of annamalai
university(Arun Singh)
2. Garbage Collection(Memory Allocate & Reallocate)
Garbage Collection in .NET framework
The Garbage collection is very important technique in the
.NET framework to free the unused objects in the memory and
free the space for next incoming process.
The garbage collection (GC) is new feature in Microsoft .NET
framework. When we have a class that represents an object in
the runtime that allocates a memory space in the heap
memory. All the behavior of that objects can be done in the
allotted memory in the heap. Once the activities related to that
object is get finished then it will be there as unused space in
the memory.
All Right Reserved@ aumcp group of annamalai
university(Arun Singh)
3. The earlier releases of Microsoft products have used a
method like once the process of that object get finished then
it will be cleared from the memory.
Microsoft was planning to introduce a method that should
automate the cleaning of unused memory space in the heap
after the life time of that object. Eventually they have
introduced a new technique "Garbage collection".
It is very important part in the .NET framework. Now it
handles this object clear in the memory implicitly. It
overcomes the existing explicit unused memory space
clearance.
All Right Reserved@ aumcp group of annamalai
university(Arun Singh)
4. Garbage Collection Works?
The heap memory is divided into number of generations.
Normally it is three generations. The Generation 0 is for short
live objects, Generation 1 is for medium live objects which
are moved from Generation 0. Generation 2 is for long live
objects.
When an object is created then it will allocate the memory
space which will be higher. It will be in the Generation 0 and
the memory allocation will be continuous without any space
between the generations of garbage collectors.
All Right Reserved@ aumcp group of annamalai
university(Arun Singh)
5. Managed Heap
Generation 0
Generation 1
Generation 2
All Right Reserved@ aumcp group of annamalai
university(Arun Singh)
6. How it’s
Work? Garbage Collection should be handled by the .NET framework.
Implicit
When object is created then it will be placed in the Generation 0. The
garbage collection uses an algorithm which checks the objects in the
generation, according to there generation CLR decided when it will be
removed from the memory.
The two kinds of objects. One is Live Objects and Dead Objects.
The Garbage collection algorithm collects all unused objects that are dead
objects in the generation. If the live objects running for long time then
based on that life time it will be moved to next generation.
The object cleaning in the generation will not take place exactly after the
life time over of the particular objects. It takes own time to implement the
sweeping algorithm to free the spaces to the process.
All Right Reserved@ aumcp group of annamalai university(Arun
Singh)
7. When it happens
The garbage collector periodically checks the heap memory to
reclaim the objects when the object has no valid references in the
memory.
When an object is created then it checks the available space in the
heap for that object if it is available then it will allocate the memory, if the
available space is not available to allot the space then it automatically
garbage collector collect the unused objects. If all object are valid
referenced then it gets additional space from the processor.
All Right Reserved@ aumcp group of annamalai
university(Arun Singh)
8. Example code to know more about Garbage Collection
MaxGeneration
This property in the GC class returns the total number of generations.
using System; The GC is class in which
class GCExample1 the MaxGeneartion ,which
{ are count the no of
public static void Main(string[] args) generation
{
try
{
Console.WriteLine("GC Maximum
Generations:" + GC.MaxGeneration);
}
catch (Exception oEx)
{
Console.WriteLine("Error:" + oEx.Message);
}
All Right Reserved@ aumcp group of annamalai
} university(Arun Singh)
9. GetTotalMemory and GetGeneration
using System;
class BaseGC
{
public void Display()
{
Console.WriteLine("Example Method");
} GettotalMemory is
} the method,which
are gives a total
class GCExample2
{
public static void Main(string[] args)
{
try
{
Console.WriteLine("Total Memory:" + GC.GetTotalMemory(false));
BaseGC oBaseGC = new BaseGC();
Console.WriteLine("BaseGC Generation is :" + GC.GetGeneration(oBaseGC));
Console.WriteLine("Total Memory:" + GC.GetTotalMemory(false));
} All Right Reserved@ aumcp group of annamalai
university(Arun Singh)
10. catch (Exception oEx)
{
Console.WriteLine("Error:" + oEx.Message);
}
}}
Result
Here GetTotalMemory shows the total number of memory occupied by
the various resources. Here I have added one more managed code
objects in the heap memory. After adding, the size of the memory has
increased.
The GetGeneration method will find out the particular managed object
in the which generation. Here it shows the Object oBaseGC in the
0th generation.
All Right Reserved@ aumcp group of annamalai
university(Arun Singh)
11. CollectionCount and Collect
using System;
class Cal
{
public int Add(int a, int b)
{
return (a + b);
}
public int Sub(int a, int b)
{
return (a - b);
}
public int Multi(int a, int b)
{
return (a * b);
}
public int Divide(int a, int b)
{
return (a / b);
} All Right Reserved@ aumcp group of annamalai university(Arun
} Singh)
12. class GCExample3
{
public static void Main(string[] args)
{
Cal oCalci = new Cal();
Console.WriteLine("Calci object is now on " + GC.GetGeneration(oCalci) + "
Generation");
Console.WriteLine("Garbage Collection Occured in 0th
Generation:" +GC.CollectionCount(0));
Console.WriteLine("Garbage Collection Occured in 1th
Generation:" +GC.CollectionCount(1));
Console.WriteLine("Garbage Collection Occured in 2th
Generation:" +GC.CollectionCount(2));
GC.Collect(0);
Console.WriteLine("Garbage Collection Occured in 0th
Generation:" +GC.CollectionCount(0));
}
}
All Right Reserved@ aumcp group of annamalai university(Arun
Singh)
13. Result
The CollectionCount helps us to find out the generation
wise garbage collection occurred. As we know there are
totally three generations in the garbage collector. Here I
have passed argument as one for know the first
generation. Initially it was 0. Then through the code I have
collected the unused objects in the 0th generation. Again I
have checked the CollectionCount in the 0thgeneration.
Now it says 1.
The Collect method used to collect the unreferenced
objects in the heap memory. It will clear the object and
reclaim the memory space.
All Right Reserved@ aumcp group of annamalai university(Arun
Singh)
14. Common Type System
(CTS)
As .NET Framework is language independent and support over 56
different programming languages, many programmers will write data types
in their own programming language.
For example, an integer variable in C# is written as int, whereas in Visual
Basic it is written as integer. Therefore in .NET Framework you have single
class called System.Int32 to interpret these variables. Similarly, for the
ArrayList data type .NET Framework has a common type called
System.Collections.ArrayList.
This system is called Common Type System. The types in .NET
Framework are the base on which .NET applications, components, and
controls are built. Common Type System in .NET Framework defines how
data types are going to be declared and managed in runtime.
All Right Reserved@ AUMCP group of annamalai
university(Arun Singh)
15. There are two general types of categories in .NET
Framework that Common Type System support.
Value Types . Value types directly a contain data and are user-
defined or built-in. they are placed in a stack or in order in a
structure.
E.g int a=8; //directly created
Reference Types. Reference types store a reference of the
value’s memory address. They are allocated in a heap
structure.
E.g as obj=new as(); //object created
All Right Reserved@ aumcp group of
annamalai university(Arun Singh)
17. Q & A Session
More Information Post Your Questions
on
www.aumcp2013.jimdo.co
m
aumcp2013@live.com
All Right Reserved@ aumcp group of annamalai
university(Arun Singh)
18. Thanks to All
All Right Reserved@ aumcp group of annamalai
university(Arun Singh)