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

Article translations Article translations
Article ID: 834279 - View products that this article applies to.
Expand all | Collapse all

SUMMARY

This article discusses the different ways to dispose of the managed objects in Microsoft SQL Server 2000 Windows CE Edition, in Microsoft SQL Server 2005 Compact Edition, or in SQL Server 2005 Mobile 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, SQL Server 2005 Compact Edition, or SQL Server 2005 Mobile 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, SQL Server 2005 Compact Edition, or SQL Server 2005 Mobile 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, to SQL Server 2005 Compact Edition, or to SQL Server 2005 Mobile 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(<ConnectionString>);
    	 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(<ConnectionString>);
           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
    }
    finally
    { 
           // 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. 
           // By putting this in the finally clause we guarantee it will always be executed even if the exception is rethrown
           
           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.

REFERENCES

For more information about the IDisposable interface, visit the following Microsoft Web site:
http://msdn2.microsoft.com/en-us/library/system.idisposable(vs.71).aspx
For more information about the GC.Collect() method, visit the following Microsoft Web site:
http://msdn2.microsoft.com/en-us/library/aa903907(VS.71).aspx
For more information about the SqlCeConnection.Close method, visit the following Microsoft Web site:
http://msdn2.microsoft.com/en-us/library/system.data.sqlserverce.sqlceconnection.close(vs.71).aspx
For more information, click the following article numbers to view the articles in the Microsoft Knowledge Base:
824462 SqlCeCommand objects are not automatically disposed if you use a SqlCeDataAdapter object
326164 Dumpmem utility for viewing virtual address space on Pocket PC 2002
827837 FIX: SQL Server CE connection causes an out-of-memory condition when you create many SqlCeDataReader objects

Properties

Article ID: 834279 - Last Review: December 13, 2011 - Revision: 6.0
APPLIES TO
  • 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
Keywords: 
kbgarbagecollect kbmemory kbappdev kbcodesnippet kbcode kbinfo KB834279

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com