10

Everybody knows that in .Net framework String objects are directly stored in heap memory

I am just trying to understand if there is any reserved memory in .Net framework for Strings. In java there is a reserved memory for strings called SCMP(String Constant Memory Pool) where strings are initialized and garbage collected just like other objects in heap memory.

Pradeep K M
  • 1,461
  • 2
  • 10
  • 17
  • 1
    I think Java has such a feature because in the old days, we had mobile devices with limited memory. Now we don't need such a feature. it's much simpler for the compiler and garbage collector. – King King Sep 16 '13 at 15:30
  • Related http://stackoverflow.com/questions/372547/where-do-java-and-net-string-literals-reside/372559#372559 – Brian Rasmussen Sep 16 '13 at 17:00

3 Answers3

6

I dont think there is anything like that in .Net.

Instead I have read this and its interesting how Strings are used:

The CLR maintains a table called the intern pool that contains the literal strings in a program. This ensures that repeated use of the same constant strings in your code will utilize the same string reference. The System.String class provides an Intern method that ensures a string is in the intern pool and returns the reference to it.

Also check this MSDN:-

We have seen numerous scenarios where the managed heap contains the same string repeated thousands of times. The result is a big working set where much of the memory is consumed by strings. In this situation, it is often better to use string interning.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
2

.Net framework conserves string storage in a table, Intern Pool. You may see:

String interning and String.Empty - Eric Lippert

If you have two identical string literals in one compilation unit then the code we generate ensures that only one string object is created by the CLR for all instances of that literal within the assembly. This optimization is called "string interning".

String Interning .Net framework

The common language runtime conserves string storage by maintaining a table, called the intern pool, that contains a single reference to each unique literal string declared or created programmatically in your program. Consequently, an instance of a literal string with a particular value only exists once in the system.

Habib
  • 219,104
  • 29
  • 407
  • 436
1

In .NET literal strings are "interned" so that there is only one copy of each literal string.

See http://msdn.microsoft.com/en-us/library/system.string.intern.aspx

Joel Lee
  • 3,656
  • 1
  • 18
  • 21
  • This answer is misleading. *Literal* strings are interned. Dynamically-constructed strings are allocated on the heap as ordinary objects, and are not necessarily unique. Quick example: `Object.ReferenceEquals("abc", "ABC".ToLowerInvariant())` evaluates as `false`, since only the first string is interned. – Douglas Sep 16 '13 at 15:46
  • 1
    @Douglas - No intent to mislead. The answer says that there is one copy of each *literal* string. The cited MSDN reference makes it very clear that programatically constructed strings are not interned, unless you specifically force them to be interned using the `String.Intern` method. – Joel Lee Sep 16 '13 at 15:59
  • 1
    Also, just because a string is interned doesn't mean that the strings memory is in a separately allocated place. There's no reason it can't just be in regular heap memory. The point is that the compiler keeps track of all of those strings and ensures the same reference is used for all literals, they don't need to be in a separate memory pool to do that. – Servy Sep 16 '13 at 16:01
  • 3
    This might be just pedantry from my part, but I said "misleading" because the first part of your answer could be misinterpreted as implying that all strings are interned. – Douglas Sep 16 '13 at 16:49
  • @Douglas - Corrected, thanks. What I meant was that .NET uses interning to prevent duplication of literals. As with code, its always better to have someone else proofread. – Joel Lee Sep 17 '13 at 17:57