The note of learning ASP. NET [18] About. Net (GC) memory recycle Mechanism

Source: Internet
Author: User
In. net, no matter which type of variable occupies a space in the memory. Int, datetime, float, and other basic data types store actual data in the corresponding memory space, while the referenced variables are different, the memory space occupied by the referenced variable is not the actual object data, but the address pointing to the memory where the actual object is stored. This is called a pointer in C or C ++, the address that points to the memory where the actual object is stored. Generally, you can create a process for referencing a variable and assigning values to it (taking the object of the person class as an example): person personone = new person (); this statement is actually divided into three steps: (1) use the new command to create a new object in the memory (2) Create the person-type reference variable personone (the referenced variable must be assigned a value before use; otherwise, an error will be reported during compilation, if the referenced variable is declared and its value is null, It is not referenced to any object.) (3) Assign the information in the memory of the newly created object to the application variable personone. It can be seen that if we use personone = NULL after the above operation; assign personone to null, although the referenced variable is set to null, however, the previously referenced object actually exists in the memory. We don't actually worry about how to release the memory occupied by the object that is no longer in use .. The memory recycling mechanism in. net, that is, GC (Garbage Collector), is used to reclaim the memory space of referenced variables and the actually referenced objects. We need to pay attention to two questions: what conditions will the objects be recycled by GC and the memory space will be released? When Will GC recycle useless objects? (1) When an object in memory is no longer referenced by any referenced variable, it meets the condition for GC collection. For example, person personone = new person (); when we set personone to null, personone no longer points to any object, and at the same time, we cannot access the objects pointed to by personone again, therefore, this object is useless for us. (2) GC will not immediately recycle and release the memory when the object meets the recycle condition. It will judge to recycle the objects in the memory when the memory is insufficient. If the memory is large enough, it may never recycle objects that are still in the memory but are useless. After understanding the above issues, we can better control the use of resources in the programming process and better understand the usage of some methods.
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.