Thanks for visiting Guitar Keys. Please remember to bookmark this page String Natural. If you found what your looking for, then don"t forget to click an appreciation button above for this post.
String Natural
C# in a string of memory allocation and resident pool
Just started learning C # when you hear the CLR String class has a special memory management mechanism: Sometimes, it's clear statement of two of the String class objects, but they are chosen to point to the same instance. As follows:
String s1 = "Hello";
String s2 = "Hello";
/ / s2 and s1 are the actual value of Hello
bool same = (object) s1 == (object) s2;
/ / Here compare s1, s2 are referring to the same object instance
/ / So we can not write bool same = s1 == s2;
// Because the String class overloads the == operator to compare String objects containing the actual value Here the same will be assigned to true.NIKON D3000 Charger. That is really s1 and s2 refer to the same String object. Of course, it should be noted that the unity of s1 and s2 are assigned to the same string Hello, this is the reason for the above scenario.
Now our initial conclusion, when more than one string variable contains the actual value of the same string, CLR may not allocate memory for them repeatedly, but make them all point to the same String object instance. (Here I say possible because in some cases, the occurrence of a string will do the actual value in memory at the same time there are multiple copies, please read on.)
We know, String class has many special places, one of which is that it will not change (immutable). This shows that each time we operate on a String object (for example, use Trim, Replace and other methods), not really an instance of the String object to be modified, but returns a new String object instance as the operation performed results. Once an instance of String objects generated to death will not be changed!
Based on characteristics such as the String class, CLR so that the actual value of the same string variable to point to the same String instance is a completely reasonable. Because the use of any reference to an instance of String modifications carried out are not truly affect the state of the instance, it does not affect all other instances of the reference point to the string represented by the actual value. CLR String class that manage memory allocation, memory usage can be optimized to avoid redundant data contained in memory.
To achieve this mechanism, CLR quietly maintains a pool called the resident (Intern Pool) table. This table records all the code used in the string literal reference to the instance declaration. This shows that using a string literal declaration will enter the presence of the pool, while the other string and will not enter the statement, it does not automatically enjoy the CLR string of redundant mechanisms to prevent the benefits. This is what I mentioned above, in some cases, the occurrence of a string will do the actual value in memory at the same time there are multiple copies of the example. Consider this example: StringBuilder sb =new StringBuilder();
sb.Append("He").Append("llo");
string s1 ="Hello";
string s2 = sb.ToString();
bool same = (object) s1 == (object) s2; When same is not true, because although the s1, s2 represents the same string as s2, but not by literal statement, CLR for sb.ToString () method's return value to allocate memory, it does not to reside in the pool to check whether there already exists a string value Hello, so do not let s2 point to the presence of the natural pool of objects.
To allow programmers to check the presence of mandatory CLR pool to avoid redundant copy of the string, String class designers to provide a class method called Intern. Here is an example of this approach: StringBuilder sb =new StringBuilder();
sb.Append("He").Append("llo");
string s1 ="Hello";
string s2 = String.Intern(sb.ToString());
bool same = (object) s1 == (object) s2; Well, same is true of. Intern method takes a string as a parameter, it will check the pool in the presence of said parameter string. If there is, it returns a string that resides in the pool of reference; otherwise reside in the pool to add a new string representing the same value, and returns reference to the string. Note, however, even if the Intern method resides in the pool to find the same value of the string, the string does not allow you to save a memory allocation operation, because the string as a parameter has been assigned a memory. The method has the advantage of using the Intern, Intern method resides in the pool if you find the same value of the string, in memory at this time although there are two copies of the string (a is the parameter, is the presence of a pool in), but as time goes by, the argument refers to a copy that will be garbage out, so the memory for the string does not exist a redundancy.
When your program there is a method, Nikon EN-EL3 Battery.according to different contexts to create and return a long string, and in the process of running it will always return the same string, you may have to think about using the Intern method to improve the memory utilization of.
But also worth noting that a string using the Intern method to survive in the presence of the pool also has a side effect: even if there is no other reference point to the presence of a pool of strings, this string is still not necessarily be garbage recycling out. That resides in the pool even if the string has been useless, it may have to wait until the end when the CLR is destroyed. When you use the Intern method, should also take into account this particular behavior.
About the Author
C # development in 5 years
http://www.iibattery.com/nikon-en-el15-digital-camera-battery-iigl.html

If you are looking for a different item here are a list of related products on Guitar Keys, please check out the following:




























































































