157 recommendations for writing high-quality code to improve C # programs-Recommendation 53: Assign null to object references that are no longer used if necessary

Source: Internet
Author: User

Recommendation 53: An object reference that is no longer used should be assigned null if necessary

In a CLR-managed application, there is a concept of "root" in which static fields of type, method parameters, and local variables can exist as "roots" (value types cannot be "roots" and only pointers of reference types can act as "roots").

A local variable creates a "root" in memory during the execution of the code. In a garbage collection, the garbage collector checks the "root" of the thread stack upstream (after the thread stack is checked, it also checks the root collection of all the static fields of the reference type Object). When "root" within a method is detected, if it is found that no local variable is referenced, it means that the "root" has been stopped, regardless of whether it has been explicitly assigned a value of NULL. The garbage collector then discovers that the reference to the root is empty and that the root can be freed, which also means that the memory space occupied by the type object can be freed. So specifying NULL for a local variable is meaningless, and the parameter variable for the method is also the case.

The JIT compiler is an optimized compiler, whether or not we assign a local variable to null within a method, such as the following statement:

            SampleClass c1=New  SampleClass ();             null;

Medium C1 = null; This sentence will be ignored. If we set the project to release mode, this statement will not be compiled into the runtime at all.

It is because of the above analysis that many people think that assigning an object to null is completely unnecessary. However, in another case, be careful to assign a variable to NULL in a timely manner, which is the static field of the type. Assigning a type object to null does not imply that the static field of the type is assigned null at the same time. When the type object is recycled, the static field of the type is not recycled.

The reason that a static field is not freed (and the assignment of a null statement is not optimized by the runtime compiler as a local variable) is because the "root" is always present once the static field is created. Therefore, the garbage collector always does not think it is a garbage. Non-static fields do not have such a problem.

In the actual work, once we feel that our static reference type parameters occupy a large amount of memory, and will not be used after use, you can immediately assign it to null. This may not be necessary, but it is a good habit. Imagine a system in which static variables that occasionally appear in a class are silently in memory, and once created, they never leave. Therefore, use as few static variables as possible.

Turn from: 157 recommendations for writing high-quality code to improve C # programs Minjia

157 recommendations for writing high-quality code to improve C # programs-Recommendation 53: Assign null to object references that are no longer used if necessary

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.