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.
The String constant pool is a special memory area. When we declare a String literal, the JVM creates the object in the pool and stores its reference on the stack. Before creating each String object in memory, the JVM performs some steps to decrease the memory overhead.
String pool is a storage space in the Java heap memory where string literals are stored. It is also known as String Constant Pool or String Intern Pool. It is privately maintained by the Java String class.
A string constant pool is a separate place in the heap memory where the values of all the strings which are defined in the program are stored. When we declare a string, an object of type String is created in the stack, while an instance with the value of the string is created in the heap.
As the name suggests, String Pool in java is a pool of Strings stored in Java Heap Memory.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With