Schema in dotnet framework source code (IX)--flyweight (mode of privilege)

Source: Internet
Author: User
Tags dotnet hash

The use of sharing technology to effectively support a large number of fine-grained objects.

When dealing with strings in. NET, there is an important mechanism, called the string-resident mechanism, in which the use of the element pattern is used. The CLR (Common language runtime) stores a string by maintaining a table called a detention pool that contains a reference to each unique string that is declared or created programmatically in a program. Therefore, an instance of a string with a specific value is only one in the system.

The process of using the pattern in the CLR to implement string hosting: a particular data structure is maintained internally within the CLR--we can think of it as a hash table, a string that most of the hash table maintainers Create (I'm not saying everything here, because there are exceptions). The key of this hash table corresponds to the corresponding string itself, and value is the reference to the memory block assigned to the string. This hash table is created when the CLR initializes. Generally, in the process of running a program, if you need to create a STRING,CLR will try to find the same string in the hash table based on this string hash code, and if found, assign the address of the found string directly to the corresponding variable. If not, creating a string,clr in the managed heap first creates the strng in the managed heap and creates a key-value Pair--key for the string itself in the hash table. The value bit is the memory address of this newly created string, which is assigned the most weight to the response variable.

The underlying implementation of intern (String str), isinterned (String str), which is related to the Strig class and the detention pool, can be more intuitive in understanding the above process.

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.