|
|
|
A .NET application can optimize string management by maintaining an internal pool of string values known as intern pool. 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. 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, which of course helps saving 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. |
You can optimize string management by using the Intern shared 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. See how you can "manually" optimize the preceding code snippet by using the String.Intern method: |
This optimization technique makes sense only if you're working with long strings and if you're working with 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 you application produce a smaller memory footprint. |
|
|
|
Submitted By :
Nayan Patel
(Member Since : 5/26/2004 12:23:06 PM)
|
|
|
Job Description :
He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting. |
View all (893) submissions by this author
(Birth Date : 7/14/1981 ) |
|
|