Tuesday, 2 July 2013

Understanding Heap and Reference type

Hello all,

In continuation to my previous post Understanding Stack and Value type, Now I am going to explain what is Heap memory? and what is Reference type? Let's start,

Basically here I mean Heap is memory. Reference type is data which are stored in this memory. Consider the below diagram,



Understanding Heap and Reference type


In the above code I declare two variables as data type object. As Microsoft definition "The object type is an alias for System.Object in the .NET Framework. You can assign values of any type to variables of type object." When the first line of code (object i=1;) executed, like value type here too the space allocated in Stack memory. But Stack memory having reference address only. Originally data stored in heap memory. For better understanding see image below,
Understanding Heap and Reference type


In the second line we just reassigned the i value to y, So the memory allocation should be like below,

Understanding Heap and Reference type

So above picture clearly shows, Reference types data would not reallocate the memory for reassign. 

Points to remember

  • Other than Dot net data types such as Object, user defined data types (Class) are called Reference types. 
  • Memory used to store the Reference types data is called Heap Memory. 
  • When the Program execution has stopped, Stack memory has cleared automatically. But Heap memory will not be cleared automatically. 
  • Heap memory is cleaned by Garbage Collector.
  • Generally value types are faster than Reference types, because in reference types boxing will occur

Understanding Stack and Value type

Welcome buddies,

In my previous post I have explained C# - Dot net datatypes. Now I am going to explain what is Stack, Heap memory? and what is Value type and Reference type? Let's start,

Basically here I mean Stack is memory. Value types is data which are stored in this memory. Consider the below diagram,


In the above code I declare two integer variable i,y and assign value 1 to i. Also reassign the i to y. In  C# - Dot net datatypes I mentioned integer datatype will get 32 bytes memory to represent the value. So when first code executed, a chunk of memory allocated which size would be 32 bytes. 

In the second line we just reassigned the i value to y, So the expected memory allocation should be like below,
But the actual allocation be like below image,
That means It allocates new chunk of memory for the y. So we conclude definition "When the values reassign instead of reuse the previously allocated memory, if new chunk of memory is created then the data is called Value type". In other words "All C# - Dot net datatypes are Value types and memory allocated for value types is called Stack memory". When the program execution has finished all allocated space in Stack memory deallocated automatically. 

In my next post I have explained What is Heap and Reference type?

Monday, 1 July 2013

Fundamentals of C# Dot Net Datatypes

Hi all,

Hope you have better knowledge about dot net framework on my previous post Understanding Dot Net Framework. Now I am going to explain C# dot net datatypes. Ok, before start I want to explain some fundamentals of data. Of course many of you already know, anyway lets start.


In the above picture computer memory represents "A" as eight bits, I mean one byte. We all know

8 bit = 1 byte
So in this way we can represents up to 

2=256 characters. 

Here range for the characters are 0 -255. In C# we have a datatype byte, it's stored exactly 0 to 255 characters. In other words It's represents ASCII.

OK, Now if we want to represents more than 256 characters, what do we do? For this C# provide another datatype char, it's takes two bytes to represents a character. When we use this char datatype, we can represents up to,

216 =65536

Here range for the characters are 0-65535. In other words, char datatype represents UNICODE. Series of char datatypes is called string. So we can categorize our data into three categories,
  1. Alphanumeric
  2. Numeric
  3. Others
byte, char, string datatypes are represents Alphanumeric data. C# also having numeric datatypes. Lets we see what are all datatypes in c# using below diagram,


Below table represents min value and max value of each datatypes and what values it may have,

Category
Datatype
Range
Type of data
Alphanumeric
Byte
28 = 0 – 255
ASCII
Char
216 = 0 – 65535
UNICODE
String
Unlimited
Series of UNICODE
Numeric
Without Decimals
Signed numbers
Short / Int16
216 =  - 32768 to 32767
0 to 9
Int / Int32
232
0 to 9
Long/Int64
264
0 to 9
Unsigned numbers
Ushort/ Uint16
216 = 0 to 65535
0 to 9
Uint/Int32
232
0 to 9
Ulong/Int64
264
0 to 9
With Decimals
Float
Precision up to 7 decimals
0 to 9
Double
Precision up to 16 decimals
0 to 9
Decimal
Precision up to 32 decimals
0 to 9

Saturday, 29 June 2013

Understanding Dot Net Framework

Hi buddies,

Today I am going to explain about fundamentals of dot net as what I learnt. So if you know more than this article, I welcome your comments. Lets start,

courtesy: Shivprasad Koirala

The above picture clearly shows,

  1. When we build our application using compiler( csc.exe for c#, vbc.exe vb.net etc) it returns the IL code.
  2. IL code is nothing but CPU independent partially compiled code.
  3. CLR is heart of Dot Net framework which pulls this IL code and give it JIT
  4. JIT converts this IL code into executable instructions.

Why the language specific compilers such as csc.exe, vbc.exe etc provide half compiled code instead of full? because when we execute the exe this IL code converted as executable instructions by JIT (Just In Time compiler) in terms of optimized based on environment variables such as CPU capacity, RAM,OS,Memory etc...

When we learn about dot net framework we should know below listed things,


  1. IL Code
  2. Common Type System
  3. Common Language Specification
  4. Common Language Runtime
    1. Garbage Collection
    2. Code Access Security and Code Verification
      1. Evidences
      2. Permission Set
      3. Code Group
    3. Just In Time Compiler
      1. Normal JIT
      2. Ecno JIT
      3. Pre JIT
IL Code
  • Partially compiled CPU independent code
  • Reason for Partially compiled already explained in above
Common Type System
  • Generally Dot Net application is language independent.
  • To achieve the cross platform support Dot net framework uses CTS
  • So we can call a function in C# which is written in VB.net
Common Language Specification
  • This subset of CTS
  • Set rules or guidelines or specification needs to follow to achieve the cross platform support.
  • To enable this guidelines to developer, add [assembly:CLSComplient(True)] in Assemblyinfo.cs
Garbage Collection
  • It is a background process run by CLR
  • It is clear the unused objects in dot net application and free up the memory
  • It is having three generations gen0, gen1,gen2
  • These Generations are increased the GC performance
Code Access Security & Code Verification
  • It is security model, which grants or deny dot net assembly and exe depending on evidences.
  • Evidences is nothing but where these code come from, for example has it come from internet or intranet etc
  • Actually CAS is a three steps process
    1. Before Dot Net assembly or exe executing, CAS starts to collecting evidence values
    2. Based on evidence values permission are allocated via Permission Set
    3. These Permission Set and evidences grouped by Code Group such as local,intranet,internet etc
  • Microsoft has provide the visual studio command prompt utility "caspol.exe" to do this three steps process.
Just in Time Compiler (JIT)
  • As I already told JIT will convert the IL code into optimized executable instructions based on environment variables
  • Three kinds of JITs are there
    1. Normal JIT -  Complie IL code and put it in memory cache. This can can be speed up the execution time. Generally this kind of JIT is used in desktop, laptop. 
    2. Ecno JIT - Compile IL code and doesn't put it in memory cache. Means every time it's compile the IL code. Generally this kind of JIT is used in Windows mobile devices. 
    3. Pre JIT - Compile full IL code into executable instructions. We can use it when we deploy our application in production server.
      • Here we are not compile every time when execute the code. So it may be speed up our application performance.
      • But this same advantage also becomes disadvantage when we execute the exe in another machine, beacuse the compilation done based on the source machine's environment values. So it may derives performance 
  • We can compile the code through Pre JIT by using "NGEN.exe" command line utility
  • We could not choose either Normal JIT or Ecno JIT as our wish. It is handled by CLR.
That's it for now. Hope you will get some useful info.