This code snippet is from C# in Depth
static bool AreReferencesEqual<T>(T first, T second)
where T : class
{
return first == second;
}
static void Main()
{
string name = "Jon";
string intro1 = "My name is " + name;
string intro2 = "My name is " + name;
Console.WriteLin...
From a brief look using Reflector, it looks like String.Substring() allocates memory for each substring. Am I correct that this is the case? I thought that wouldn't be necessary since strings are immutable.
My underlying goal was to create a IEnumerable<string> Split(this String, Char) extension method that allocates no additional me...
If you are parsing, lets just say HTML, once you read the element name, will it be beneficial to intern it? The logic here is that this parser will parse the same strings (element names) over and over again? And several documents will be parsed.
Theory:
// elemName is checked for null.
MarkupNode node = new MarkupNode()
{
Name = St...
If I have a HashMap that looks like this:
HashMap<String, MyObject>
where the String key is a field in MyObject, does this string value get stored twice?
So when I add entries:
_myMap.put(myObj.getName(), myObj);
Am I using double the String size in terms of memory? Or does Java do something clever behind the scenes?
Thanks
...
I am trying to understand string interning and why is doesn't seem to work in my example. The point of the example is to show Example 1 uses less (a lot less memory) as it should only have 10 strings in memory. However, in the code below both example use roughly the same amount of memory (virtual size and working set).
Please advice why...
Does Python have a pool of all strings and are they (strings) singletons there?
More precise, in the following code one or two strings were created in memory:
a = str(num)
b = str(num)
?
...
In the example below I am interning the string in the constructor which is fine. However when i deserialise the object from the binary formatter I don't think the string will be interned as the constructor should be called. How should I be ensuring the _name string is interned? ... or will it be interned ok?
Edit: So it seems to work (i...
I'm curious. The scenario is a web app/site with e.g. 100's of concurrent connections and many (20?) page loads per second.
If the app needs to server a formatted string
string.Format("Hello, {0}", username);
Will the "Hello, {0}" be interned? Or would it only be interned with
string hello = "Hello, {0}";
string.Format(hello, userna...
I see the follow pattern occurring quite frequently:
b->last = ngx_cpymem(b->last, "</pre><hr>", sizeof("</pre><hr>") - 1);
Notice that the literal string is used twice. The extract is from the nginx source-base.
The compiler should be able to merge these literals when it is encountered within the compilation unit.
My questions a...
I've seen many primitive examples describing how String intern()'ing works, but I have yet to see a real-life use-case that would benefit from it.
The only situation that I can dream up is having a web service that receives a considerable amount of requests, each being very similar in nature due to a rigid schema. By intern()'ing the re...
When should one compare Strings as objects and when should one use their equals method? To make sure, I always use equals, but that doesn't seem very efficient. In what situations can I be certain that string1 == string2 is a safe to use?
Thanks!
...