Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of .net character temporary Storage Pool

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail the sample analysis of the .net character storage pool. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Strings have the characteristics of both value types and reference types in .net. Character types are also called immutable object types. String types account for a large proportion in use. Every time you use them, you have to open up a new space, which consumes a lot of memory, so Microsoft gives us a character temporary pool called String Intern Pool. I don't need to open up a new space when I reuse this string, I just need to get it from the pool.

When we create a new character, we first go to the staging pool to see if the character exists, and if not, save the new string to the staging pool.

Here are two methods of the string class

/ Abstract: / / retrieve the system's reference to the specified System.String. / / Parameter: / / str: / / string to search in the staging pool. / returns the result: / / if the str is temporarily saved, the system reference to it is returned; otherwise, a new reference to the string whose value is str is returned. / exception: / / T:System.ArgumentNullException: / / str is null. [SecuritySafeCritical] public static String Intern (String str); / Abstract: / / retrieve the reference to the specified System.String. / / Parameter: / / str: / / string to search in the staging pool. / return result: / / return a reference to str if it is in the staging pool of the common language runtime; otherwise, return null. / exception: / / T:System.ArgumentNullException: / / str is null. [SecuritySafeCritical] public static String IsInterned (String str)

The string staging pool (intern pool) is actually a hash table, the key is the literal amount of the string, and the value is a reference to the string object on the managed heap. When loading an assembly, different versions of CLR vary as to whether or not to retain the literal amount of strings in the assembly metadata, which is determined at compile time.

When we assign a literal value to a variable of string type, CLR will first go to the string pool to see if there is an identical string (case-sensitive). If so, the corresponding reference will be returned. If not, a new object will be created and added to the string pool to return a reference. However, string pooling is not used when assigning values to string variables at run time (for example, using the new keyword).

String a = "abc"; string b = "abc"; string c = new string (new char [] {'a', 'baked,' c'})

Console.WriteLine (a.Equals (b)); Console.WriteLine (a.Equals (c))

Console.WriteLine (object.ReferenceEquals (a, b)) / / trueConsole.WriteLine (object.ReferenceEquals (a, c)); / / false

Let's test a wave of performance against this.

1. Test 100,000 pieces of data character splicing

two。 Test different string concatenation

3. Test the same character stitching

The code is as follows:

/ / stitching Stopwatch sw1 = new Stopwatch () for the same character; string a = "a"; string b = string.Empty;sw1.Start (); for (int I = 0; I < 10000000; iSum +) {b + = a;} sw1.Stop (); Console.WriteLine ($"same character splicing time = {sw1.ElapsedMilliseconds}"); / / stitching Stopwatch sw2 = new Stopwatch () for different characters; string c = string.Empty;sw2.Start (); for (int I = 0) I < 10000000; iSum +) {/ / i.ToString (); c + = i.ToString ();} sw2.Stop (); Console.WriteLine ($"different character splicing time = {sw2.ElapsedMilliseconds}"); / / time to check the boxing Stopwatch sw3 = new Stopwatch (); string d = string.Empty;sw3.Start (); for (int I = 0; I < 10000000; iSum +) {i.ToString ();} sw3.Stop () Console.WriteLine ($"detect time consumed by packing {sw3.ElapsedMilliseconds}")

The results are as follows

Same character splicing time = 1695 different character splicing time = 26925 detection box consumption time 12

Summary: net strings exist in a variety of ways, we need to distinguish between run to create or compile-time to create, we often use string types in the program, for our commonly used strings should be defined using constants to ensure its reusability.

This is the end of the article on "sample analysis of .net character storage pool". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report