Suggestion for improving C # program 5: assign a null value to the reference type and accelerate garbage collection

Source: Internet
Author: User

In the standard Dispose mode (see the previous blog"Implementation of standard Dispose mode in html "> C #"), Mentioned the need to release resources in a timely manner, but did not further elaborate on whether it is necessary to make the reference equal to null.

Some people think that null can help the garbage collection mechanism to discover and identify the object as garbage. Others think this is not helpful. Whether to assign a value to null is first mentioned in the method. Now, in order to better elaborate on the issues raised, we will write a Winform application. As follows:

        Private VoidButton#click (ObjectSender, EventArgs e)
{
Method1 ();
Method2 ();
}

Private VoidButton2_Click (ObjectSender, EventArgs e)
{
GC. Collect ();
}

Private VoidMethod1 ()
{
SimpleClass s= NewSimpleClass ("Method1");
S= Null;
// Other irrelevant work code (this comment is due to a question from a friend who responds to the reply)
}
Private VoidMethod2 ()
{
SimpleClass s= NewSimpleClass ("Method2");
}
}

ClassSimpleClass
{
StringM_text;

PublicSimpleClass (StringText)
{
M_text=Text;
}

~SimpleClass ()
{
MessageBox. Show (String. Format ("SimpleClass Disposed, tag: {0}", M_text ));
}
}

Click button 1 first, and then click button 2 to release. We will find that:

The object in the q method Method2 is released first, although it is called after Method1;

The object in the q method Method2 is released first, although it is not assigned null as the object reference in Method1;

There is a "root" concept in the CLR-managed application, static fields, method parameters, and local variables of the type can all be used as "root" (value types cannot be used as "root", and only reference type pointers can be used as "root ").

The local variables of the above two methods will create a "root" in the memory during code execution ". in a garbage collection, the garbage collector checks "root" upstream along the thread stack ". When the "root" in the method is checked, if no local variable is referenced in any part, no matter whether the value is null or not, it means that the "root" has been stopped. The garbage collector finds that the reference of the root is empty and marks that the root can be released. This also indicates that the memory space occupied by Simple objects can be released. Therefore, in the above example, it is meaningless to specify the value of s as null (this is also the case for parameter variables of methods ).

Furthermore, the JIT compiler is an optimized compiler. Whether or not we assign a null value to a local variable in the method, this statement will be ignored.:

View sourceprint?
s = null;

When we set the project to Release mode, the above line of code will not be compiled into the runtime.

Because of the above analysis, many people think that it is unnecessary to assign a null value to an object. However, in another case, you must assign a value to null for the variable in time. That is, static fields of the type.Assigning a value to a type object is null, which does not mean that the value to a static field of the type is null at the same time:

        private void button1_Click(object sender, EventArgs e)
{
SimpleClass s = new SimpleClass("test");
}

private void button2_Click(object sender, EventArgs e)
{
GC.Collect();
}
}

class SimpleClass
{
static AnotherSimpleClass asc = new AnotherSimpleClass();
string m_text;

public SimpleClass(string text)
{
m_text = text;
}

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.