SQL Server Basics-datatable. Dispose ();. Clear (); = NULL;

Source: Internet
Author: User

. The comparison of Clear () has no meaning, because it is only to empty the DataTable, in the heap to allocate memory, the general comparison is also more close () method, but the DataTable does not have this method

The difference between Dispose and null is interesting.

First of all
DataTable dt = new DataTable ();
You know DT on the stack, the new DataTable () object pointing to the managed heap

and dt = null, which means that DT does not point to any object, and the new DataTable () is still in memory, waiting for GC to be recycled (when is the garbage collection mechanism?)

and dt. Dispose () means the destruction of the new DataTable (); This object, but DT still points to the address of the object being destroyed; DT is not NULL at this time, but it cannot be used

You can look at the results of the following code
DataTable dt = new DataTable ();
Dt. Dispose ();
MessageBox.Show (dt = = null). ToString ());

In actual development, it is sufficient to set the DT to NULL, which can wait for GC to be recycled. If you want to use the Dispose () method, it is generally

using (DataTable dt = new DataTable ())
{

}

This implicitly calls the Dispose () method at the end of the curly brace, and DT becomes a local variable immediately out of scope, and there is no call error
===============================================================
How to take the initiative to recycle the =null?
===============================================================
Gc. Collect ();



All objects are considered recycled regardless of how long the object is in memory, but objects referenced in managed code are not recycled. Use this method to force the system to attempt to reclaim the maximum amount of memory available.

SQL Server Basics-datatable. Dispose ();. Clear (); = NULL;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.