Saturday, December 14, 2024

.NET String internal pool

Share

The CLR can optimize string management by maintaining an internal pool of string values known as an intern pool for each .NET application.

If the value being assigned to a string variable coincides with one of the strings already in the intern pool, no additional memory is created and the variable receives the address of the string value in the pool. As shown earlier, the compiler is capable of using the intern pool to optimize string initialization and have two string variables pointing to the same String object in memory. This optimization step isn’t performed at run time, though, because the search in the pool takes time and in most cases it would fail, adding overhead to the application without bringing any benefit.

' Prove that no optimization is performed at run time.

s1 = "1234"

s1 &= "5678"

s2 = "12345678"

' These two variables point to different String objects.

Console.WriteLine(s1 Is s2) ' => False

You can optimize string management by using the Intern static method. This method searches a string value in the intern pool and returns a reference to the pool element that contains the value if the value is already in the pool. If the search fails, the string is added to the pool and a reference to it is returned. Notice how you can “manually” optimize the preceding code snippet by using the String.Intern method:

s1 = "ABCD"

s1 &= "EFGH"

' Move S1 to the intern pool.

s1 = String.Intern(s1)

' Assign S2 a string constant (that we know is in the pool).

s2 = "ABCDEFGH"

' These two variables point to the same String object.

Console.WriteLine(s1 Is s2) ' => True

This optimization technique makes sense only if you’re working with long strings that appear in multiple portions of the applications. Another good time to use this technique is when you have many instances of a server-side component that contain similar string variables, such as a database connection string. Even if these strings don’t change during the program’s lifetime, they’re usually read from a file, and, therefore, the compiler can’t optimize their memory allocation automatically. Using the Intern method, you can help your application produce a smaller memory footprint. You can also use the IsInterned static method to check whether a string is in the intern pool (in which case the string itself is returned) or not (in which case the method returns Nothing):

' Continuing previous example

If Not String.IsInterned(s1) Is Nothing Then

     ' This block is executed because s1 is in the intern pool.

End If

Here’s another simple performance tip: try to gather multiple string concatenations in the same statement instead of spreading them across separate lines. The Visual Basic compiler can optimize multiple concatenation operations only if they’re in the same statement.

From Programming Microsoft Visual Basic 2005: The Language by Francesco Balena

Tag:

Add to Del.icio.us | Digg | Yahoo! My Web | Furl

Bookmark Murdok:

Kenny Tran is the author of the “Kenny and .NET” blog.

Table of contents

Read more

Local News