<< August 2004 >>
Sun Mon Tue Wed Thu Fri Sat
01 02 03 04 05 06 07
08 09 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31


If you want to be updated on this weblog Enter your email here:



rss feed



Aug 10, 2004
How Value and Reference Type 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.

 

  


Posted at 07:06 pm by whidbey

B.Basker
August 25, 2004   03:56 PM PDT
 
Hi saurabh,
Very good diagramatic representation.
avni
August 20, 2004   09:57 PM PDT
 
an informative article
chitransh
August 18, 2004   10:51 AM PDT
 
really, very clear picture... it shows
Rajesh Dagar
August 17, 2004   07:02 PM PDT
 
This is very informative article , keep it up
Neha
August 10, 2004   07:21 PM PDT
 
Nice Illustration. This clarifies most of my doubts.

Thanks Saurabh
 

Leave a Comment:

Name


Homepage (optional)


Comments




Previous Entry Home Next Entry