Article ID: 310348 - Last Review: May 13, 2007 - Revision: 3.3 How To Avoid the Boxing Penalty When You Use the DataReader in Visual Basic .NETThis article was previously published under Q310348 On This PageSUMMARY
This article describes how to use the Getxxx methods (such as GetChar, GetDouble, and GetInt32) to avoid the boxing penalty when you use the DataReader object. Description of the TechniqueWhen you use the Item property to read columns from a DataReader, the values are boxed and then unboxed. If the values are repeatedly boxed and unboxed, the heap quickly fills and increases the frequency of garbage collections. This also slightly impacts performance because Microsoft Visual Studio .NET converts and copies data more than necessary.NOTE: Boxing means that the data is copied onto the heap as System.Object. When you cast to a specific data type, the values are unboxed and copied onto your variables on the stack or into another object. To avoid the boxing penalty, use the Getxxx methods (such as GetChar, GetDouble, and GetInt32) that return the data as a simple data type instead of as System.Object. RequirementsThe following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
Create Project and Add Code
PitfallsThe disadvantage of using the Getxxx method is that you must check for NULL before you access the field. To check for NULL, use the IsDBNull method.REFERENCES
For more information about ADO.NET objects and syntax, see the following topic in the Microsoft .NET Framework Software Development Kit (SDK) documentation or MSDN Online:
Accessing Data with ADO.NET
For additional information about how to use QueryPerformanceCounter to time code in Visual Basic .NET, click the article number below to view the article in the Microsoft Knowledge Base:
http://msdn2.microsoft.com/en-us/library/e80y5yhx(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/e80y5yhx(vs.71).aspx) 306978
(http://support.microsoft.com/kb/306978/EN-US/
)
How To Use QueryPerformanceCounter to Time Code in Visual Basic .NET
| Article Translations
|
Back to the top
