In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Xiaobian to share with you what is the string resident pool in. NET, I believe most people still do not know how, so share this article for everyone's reference, I hope you have a lot of harvest after reading this article, let's go to understand it together!
In. NET, for the same string,. NET points them to the same address, they are the same instance. Strings in. NET are not updated. When you change a string variable, because of the immutability of strings,. NET actually creates a new string and points the variable address to the newly created string address.
Consider the following example:
using System;namespace ConsoleApp2{ class Program { static void Main(string[] args) { string str1 = "hello"; string str2 = "hello"; bool tf = object.ReferenceEquals(str1, str2); Console.WriteLine(tf); Console.ReadKey(); } }}
program execution result
From the execution results we can conclude that str1 and str2 point to the same memory object, they are the same instance.
In. NET, CLR silently maintains a table called Intern Pool. This table records all references to string instances declared using literals in code. This means that strings declared using literals will enter the pool, while strings declared otherwise will not enter the pool and will not automatically benefit from CLR's mechanism to prevent string redundancy.
Consider the following example
using System;using System.Text;namespace ConsoleApp2{ class Program { static void Main(string[] args) { StringBuilder sb = new StringBuilder(); sb.Append("he").Append("llo"); string str1 = "hello"; string str2 = sb.ToString(); bool tf = Object.ReferenceEquals(str1, str2); Console.WriteLine(tf); bool tf1 = str1 == str2; Console.WriteLine($"str1 and str2 have the same content: {tf1}"); Console.ReadKey(); } }}
program execution result
False is output here. Although str1 and str2 are the same string, since str2 is not declared literally, CLR allocates memory for ToString() return values without checking whether a string with the value "hello" already exists in the resident pool, so str2 does not point to objects in the resident pool.
If you want to force the CLR to check the resident pool to avoid redundant copies of strings, the designer of the String class provides a class method called Intern. Here is an example of this approach
using System;using System.Text;namespace ConsoleApp2{ class Program { static void Main(string[] args) { StringBuilder sb = new StringBuilder(); sb.Append("he").Append("llo"); string str1 = "hello"; //Force check string resident pool here string str2 = string.Intern(sb.ToString()); bool tf = Object.ReferenceEquals(str1, str2); //outputs True because the string already exists when checking the resident pool Console.WriteLine(tf); bool tf1 = str1 == str2; Console.WriteLine($"str1 and str2 have the same content: {tf1}"); Console.ReadKey(); } }}
program execution result
The Intern method takes a string as an argument and checks whether the string represented by the argument already exists in the resident pool. If it exists, it returns a reference to the string in that pool; otherwise, it adds a new string representing the same value to the pool and returns a reference to that string.
Note, however, that even if the Intern method finds a string with the same value in the resident pool, it doesn't save you a string memory allocation because the string as an argument has already been allocated memory once. The advantage of using the Intern method is that if the Intern method finds a string with the same value in the pool, there are two copies of the string in memory (one for the parameter and one from the pool), but over time, the copy referenced by the parameter is garbage collected, so there is no redundancy in memory for the string.
When you have a method in your program that creates and returns a long string in different contexts, and it returns the same string frequently during the program's run, you might want to consider using Intern to increase memory usage. It is also worth noting, however, that there is a side effect of using the Intern method to keep a string in the pool: even if there are no other references to the string in the pool, the string is not necessarily garbage collected. This means that even if the string in the pool is no longer useful, it may not be destroyed until the CLR terminates. When you use the Intern method, you should also consider this particular behavior.
That's all for the article "What is a string pool in. NET?" Thanks for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.