Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String Constant Memory pool in C#

Tags:

string

c#

memory

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.

like image 242
Pradeep K M Avatar asked Sep 16 '13 15:09

Pradeep K M


People also ask

What is the string constant pool?

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.

Is string constant pool in heap memory?

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.

What is string pool explain with example?

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.

Where is string pool stored memory?

As the name suggests, String Pool in java is a pool of Strings stored in Java Heap Memory.


1 Answers

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.

like image 111
Rahul Tripathi Avatar answered Oct 23 '22 18:10

Rahul Tripathi