How Reference and Value types are stored in memory ?


Introduction:

There are basically two types of variables in Common Type System ?

1) Value Type - Get Stored on Stack Memory.
2) Reference Type - Get Stored on Managed Heap Memory

How this Works?
Value types get stored on stack and shared the same process memory, however the Reference Types get stored on Heap and their memory address value gets stored on the stack as value type.

Question: When an object (child Object) is instantiated inside an object (parent Object) where does it get stored?
Answer: The value of child object (Reference type) gets stored in another random memory area on the managed heap and its reference (memory address value) gets stored in the managed heap of the Parent Object as Value Type.

The value of child object (Value type) gets stored in the Parent object's memory area of the managed heap.

For Example:

Module Module1 'Main Entry
    Sub Main()
        Dim i As Integer = 50
        Dim objA As classA = New ClassA
        Dim j As Integer = 25
    End Sub
End Module

Public Class ClassA 'Parent Class
    Dim obj As New ClassB
    Dim MoreInfo As String = "sample" 
End Class 

Public Class ClassB 'Child Class
    Public name As String
    Public age As Integer 
End Class

As you can determine from the figure, that value of the ObjA gets stored in the Managed Heap and its memory address value gets stored on the Stack. But, in case of ObjB the value of the ObjB gets stored at another location in the Managed Heap and the memory address value of this location gets stored as a value type in ObjA Managed Heap.

 

Comments (7)
  1. thanks says:

    well,that’s pretty

  2. Gokhan says:

    Please change the color schema of your blog. Seriously, please! Great info, on the other hand.

  3. ac says:

    my eyes are bleeding!

  4. Daniel Moth says:

    Blog link of the week 48

  5. avnrao says:

    thats a good information on value and reference types storage. so all member fields and property fields get stored on heap though they are value types. The parameters/value types declared in methods in the type are stored on stack (true??).

    so this gives a thought on whether to define a field as member field/property or pass it as a parameter to the method.

    consider this..

    public class ClassB

    {

    public string name;

    public int age;

    public int PrintAge()

    {

    Console.WriteLine("Age – " + age.ToString());

    }

    public int PrintAge(int paramAge)

    {

    Console.WriteLine("Age – " + paramAge.ToString());

    }

    }

    PrintAge execution when a parameter is passed, would be faster to PrintAge(). isnt?? simply because age (the member field) is stored on heap where as the parameter to method paramAge will be stored on stack. any corrections?

    am unable to see the figure in the blog..am i already gone blind?? 🙂

  6. Not as such, figure will be there soon. I apolize for the same.

Comments are closed.

Skip to main content