Microsoft KB Archive/834279

= Methods to dispose of SQL Server CE or SQL Server 2005 Compact Edition managed objects from memory =

Article ID: 834279

Article Last Modified on 4/25/2007

-

APPLIES TO


 * Microsoft SQL Server 2000 Windows CE Edition 2.0
 * Microsoft .NET Compact Framework
 * Microsoft Visual Studio .NET 2003 Enterprise Architect
 * Microsoft Visual Studio .NET 2003 Professional Edition
 * Microsoft Visual Studio .NET 2003 Enterprise Developer
 * Microsoft Visual Studio .NET 2003 Academic Edition
 * Microsoft SQL Server 2005 Compact Edition

-



SUMMARY
This article discusses the different ways to dispose of the managed objects in Microsoft SQL Server 2000 Windows CE Edition or in Microsoft SQL Server 2005 Compact Edition that are used in applications that you create by using Microsoft Visual Studio .NET that use the Microsoft .NET Compact Framework.



MORE INFORMATION
The .NET Compact Framework Data Provider for SQL Server CE or SQL Server 2005 Compact Edition supports a collection of classes, including the SqlCeCommand class, the SqlCeConnection class, and the SqlCeDataReader class. You can use the objects that are the instances of these classes to gain access to a SQL Server CE or SQL Server 2005 Compact Edition database from devices that are running Microsoft Windows CE in a managed environment. However, you must release the memory that is allocated to the objects if the objects are no longer required.

You can use the following methods to release the memory that is allocated to the objects of the classes.

Note Microsoft recommends that you use the Close method or the Dispose method and the following coding style to release the memory for the objects of the .NET classes that contain native references.   Use the Close method or the Dispose method. All the classes that contain native references to SQL Server CE or to SQL Server 2005 Compact Edition implement the IDisposable interface. Because these classes implement the IDisposable interface, the Dispose method of the IDisposable interface can be used to release the memory for the managed object.

These classes also implement a Close method that is similar to the Dispose method. Therefore, you can call either the Close method or the Dispose method to release the memory that is allocated to the object. However, if you call the Close method, you do not have to call the Dispose method.

For example, when you use the SqlCeDataAdapter class to populate a dataset, you must explicitly dispose of all the associated SqlCeCommand instances that represent SELECT commands, INSERT commands, UPDATE commands, or DELETE commands. You can use the following coding style for any .NET class that contains native references: SqlCeConnection conn = null; try {    conn = new SqlCeConnection(); conn.Open; // Work with the connection object } catch (Exception en) {   // Handle the exception or rethrow it } finally {   // Always release the native references in the finally clause // The statements in finally clause are guaranteed to run

if (null != conn) conn.Close; }   Use the Collect method of the GC Garbage Collector class.

Note It is extremely expensive to run Collect method of the GC Garbage Collector class. Therefore, Microsoft recommends that you use of the Collect method of the GC Garbage Collector class only when you must do so.

Use the following coding style to call the Collect method of the Garbage Collector in your application: try {   SqlCeConnection conn = null; conn = new SqlCeConnection(); conn.Open;

// The connection instance goes out of scope and effectively loses native references here // The conn object is only disposed of when the Garbage Collector starts finalizing objects, // but this only occurs under memory pressure. // If many objects are allocated like this, the performance of the program decreases. } catch (Exception en) {   // Handle the exception or rethrow it } // If GC.Collect is called, the Garbage Collector is forced to run all the finalizers in the finalization queue. //Therefore, the connection instance that was previously lost is disposed of here by the Collect method of the GC Garbage Collector class. GC.Collect; If you use the coding style in the first bullet point in this list, you do not have to explicitly invoke the Collect method of the Garbage Collector. 

