. NET memory recovery and Dispose,close,finalize methods

Source: Internet
Author: User

A. NET object usage is generally divided into three kinds of situations:

1. Creating objects

2. Using objects

3. Releasing objects

Two. Creating objects

1. The creation of objects is actually divided into two steps: variable type declaring and initializing an object

2. Variable type declaration (declare), such as:

FileStream FS

This line of code creates a variable called FS in the current variable scope space (stack or heap), at least four bytes (because you want to save an object's address)

3. Initializing objects

Object must be initialized before it is used (calling its methods or properties).

As

FS = new FileStream (@ "C:\test.txt", FileMode.OpenOrCreate);

This line of code is divided into 3 steps:

A. Allocate a piece of memory in the managed heap that is equal to the sum of all the fields in the FileStream (excluding static, of course) plus the other Dongdong that Ms deems necessary.

B. Initialize the field of the object (the value type has all the bits initialized to 0, the object initialized to null, of course string is an exception, it is initialized to an empty string)

C. Call FileStream the corresponding constructor, where a private field of an unmanaged resource (file) is initialized.

Three. Using objects

Using an object is nothing, it means calling the object's methods (or attributes, and so on) to complete a function of course the method invoked to release the object should not be within this category (now referred to finalize, etc.)

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.