Normally Reference types as the name indicates, are allocated into managed heap were as value types remain in stack and will not be neither garbage collected nor referred to by pointers. But, in many cases, you must get a reference to an instance of a value type mainly because the calling method might be expecting a reference type, in many cases an “object” data type
So in all these scenarios a value type should be converted to a reference type by using a process called boxing. The process goes like this.
1. The amount of Memory needed by the value type is allocated from the managed heap, along with two more additional members which is needed for a reference type . That is type object pointer and the sync block index.
2. The value type's fields are copied to the newly allocated memory in heap.
3. The address of the object is returned. This address is now a reference to an object
And this returned address is passed to the calling function.
Whole of the above process of boxing is automatically taken care and necessary code is generated by the compilers like CSC, (the C# compiler) when value types need to be boxed. But this will definitely degrade and hurt the performance of your code application and memory management, also a garbage collection is needed to free the memory allocated while boxing. Better choice is to avoid writing code which will lead to boxing and use the Generic equivalents of objects and methods whenever it is possible, so that we can avoid boxing and get a compile time type safe code.
Converting a boxed reference type to a value type is called unboxing. For this the values in the reference type need to be copied to the stack. First, CLR check whether the reference type which is going to be boxed is null or a different undesired type when compared to the destination value type. If yes then appropriate exceptions are thrown. Else the address of raw value type (fields) contained within an object is obtained and the values of these fields are copied from the heap to the stack-based value type instance.
So in all these scenarios a value type should be converted to a reference type by using a process called boxing. The process goes like this.
1. The amount of Memory needed by the value type is allocated from the managed heap, along with two more additional members which is needed for a reference type . That is type object pointer and the sync block index.
2. The value type's fields are copied to the newly allocated memory in heap.
3. The address of the object is returned. This address is now a reference to an object
And this returned address is passed to the calling function.
Whole of the above process of boxing is automatically taken care and necessary code is generated by the compilers like CSC, (the C# compiler) when value types need to be boxed. But this will definitely degrade and hurt the performance of your code application and memory management, also a garbage collection is needed to free the memory allocated while boxing. Better choice is to avoid writing code which will lead to boxing and use the Generic equivalents of objects and methods whenever it is possible, so that we can avoid boxing and get a compile time type safe code.
Converting a boxed reference type to a value type is called unboxing. For this the values in the reference type need to be copied to the stack. First, CLR check whether the reference type which is going to be boxed is null or a different undesired type when compared to the destination value type. If yes then appropriate exceptions are thrown. Else the address of raw value type (fields) contained within an object is obtained and the values of these fields are copied from the heap to the stack-based value type instance.
In .Net 2.0, CLR allows you to unbox a value type into a nullable version of the same value type.
Unboxing is not the exact opposite of boxing. The unboxing operation is much less costly than boxing.
so you should be aware of when the compiler generates code to perform the boxing /unboxing operations automatically and try to write code that minimizes this code generation, so that you can optimize your application performance .
0 comments:
Post a Comment