2

I want to learn where or how the string value that is written by like "Hello World" is stored.

For Example:

Example 1:

radLabel1.Text = "Hello";
radLabel2.Text = "Hello";
radLabel3.Text = "Hello";

Example 2:

string strTemp = "Hello";
radLabel1.Text = strTemp;
radLabel2.Text = strTemp;
radLabel3.Text = strTemp;

I have known second way is most useful and I am doing like this, but I want to know how the string of Example 1 is stored in RAM. I thought although they are the same, three places are created for them in RAM to store them. But in example 2 only one place is created for the string of Example 2 and using the address of this is accessed. Am I right ? Could you explain this or storing variables in RAM?

Christian St.
  • 1,751
  • 2
  • 22
  • 41
calynr
  • 1,264
  • 1
  • 11
  • 23
  • 2
    Strings are interned, that's the term you're looking for. There is only one copy of `"Hello"` stored in memory in your code. See [this wikipedia article](http://en.wikipedia.org/wiki/String_interning). – Benjamin Gruenbaum Apr 05 '14 at 10:27
  • 2
    A few bytes for a string is nothing, given most machines have many GB's of RAM. If you're looking to optimize your program, this is not the place. Also, read about string interning. See [Why only literal strings saved in the intern pool by default?](http://stackoverflow.com/questions/8509035/why-only-literal-strings-saved-in-the-intern-pool-by-default). The reason your second example is "most useful" is because if you want to change `"Hello"` into `"Goodbye"`, you only have to do so in one place. – CodeCaster Apr 05 '14 at 10:27
  • Strings are interned i.e. only one copy of it is stored in memory if they have same value. For strings in RAM, [Why Hackers Love String Data Type](http://www.codeproject.com/Articles/401220/Why-Hackers-Love-String-Data-Type) is an interesting read. – Octane Apr 05 '14 at 11:19

4 Answers4

6

The usual term that's flung around is that string literals are 'interned'. It is a pretty meaningless term and doesn't describe what is really going on that well.

A .NET assembly contains two chunks of data, the metadata and the IL. Metadata is primary used to describe the types in the assembly and contains resources. IL is the code that you wrote, translated to the Intermediate Language format.

The metadata section has 5 tables, one of these is called the "string table". Highly descriptive of what it contains, that's where your "Hello" string is stored. It is already in a format that's identical to the way strings are normally stored in the garbage collected heap, but with an extra flag in the object header that indicates that it is a string literal and not stored in the heap.

The assembly content is mapped into virtual memory by a memory mapped file, exact same animal as the .NET System.IO.MemoryMappedFiles.MemoryMapFile class. The strTemp object reference will be initialized by a single MOV instruction emitted by the jitter and stores a pointer that directly points at the string table entry in mapped view of the file. First time your program actually uses the string content, an operating system page fault ensures that the string will be present in RAM.

The garbage collector will find the strTemp object reference whenever it performs a collection. But will just ignore the reference, the flag in the object header says that it should since the string object isn't actually stored in the garbage collected heap. Which is really what "interning" means.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
0

In Example 2, you are creating a single string on the first line. Then you're just passing a reference to it to those 3 labels. As you have said, it's still a single object in the memory. However, since strings are immutable in C#, assigning a new string to radLabel2.Text won't change the value of the others, but will instead create a new string.

nphx
  • 1,426
  • 3
  • 19
  • 30
0

I thought although they are the same, three places are created for them in RAM to store them.

Strings are interned

when they are same string constants only one copy is maintained.That Means in your first example there is only one "Hello".

Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67
0

When you declared

   radLabel1.Text="Hello";

It creates a temp string with value Hello into somewhere in memory and pass the value to radLabel.text . And for radLabel2.Text and radLabel3.text, the program stil create temp 2 and temp 3 to store "Hello". Beause there is nothing to tell the program that they are the same.

But, when you declared

   string strTemp = "Hello";

It creates a string in a memory(of course),but different from "Hello".Because they created a name for it. This work like a desktop shortcut of your computer. So when you use it , you just called the shortcut, and it bypass the progress of creating a new string because the value is already defined.

Hope you understand

Poomrokc The 3years
  • 1,099
  • 2
  • 10
  • 23