Welcome to Questpond's youtube channel
Today we are discussing a very important statement Strings are Immutable.
What does this word Immutable means? Immutable means things which cannot change
and Mutable means which can change. Here is a simple program I have declaring
a string str and there is a extremely big loop out here. This big loop is looping till
this big value of the number. Whatever is the number I am pusing it to the String.
Many people think that this looks very easy to understand.
When the first loop runs this string will have value 0, when the second loop runs this
will have value 1 and in the third loop the string will have value 2 and so on. But there
is cache to it, it is not exactly like this. Internally what happens is first loop runs
the str value is 0, the second loop runs and now the previous str value 0 is maintained,
it is there internally and a new str value is created and in that 1 is pushed. The value
of str which I've allocated it does not change that which means that str is now is Immutable
it cannot be changed. To change the existing value create a fresh
copy. Now the third loop runs again str=o is there. str="1" is also there. We does not
change str="1", we create a fresh copy str="2". Whenever we allocate a value to same variable
str it does not change the old value it keeps that copy separately and creates a fresh copy
that means the previous value of the strings are not changed. That's why lot of people
says Strings are Immutable. We will see does it really happens like can
we see with our eyes all this. This is our program and now will run the profiler. There
is something called as Performance Profiler in Visual Studio which helps us to give an
insgiht of what is happing with our application. Go to Profiler and look into memory usage,
if the memory usage keeps on increasing that means there are different copies of string
getting created internally. Here
it is showing 12 MB of memory and see the red signs at
the top which says Garbage Collector is running cleaning up things that's why we see the memory
constant but internally lot of objects of string are getting created.
Strings are objects and objects are cleared by Garbage collector. This small red sign
indicates that garbage collector completed, garbage collector is running cleaning, running
and cleaning. new fresh objects of strings are getting created that's why we see this
12 MB of memory constantly. Definitely internally strings are creating
fresh copies that's why garbage collector has to work so hard to clear the objects.
There is something called as String Interning. If we allocate the same value it will see
that you are always allocating Questpond . So it will not create fresh copies and keep
this word Questpond in some global shared variable and wherever you say Questpond it
will just refer to that memory location. It will not create fresh copies and increase
the performance. If we now run the profiler we should not see grabage collector working
so hard. should not see those red signs. Go to profiler again and say start. It is
saying 7 MB and we don't see those red signs at the top
which we were seeing previously. Basically Strings are Immutable, they have
a thing called as string interning wherein if we keep allocating same value again and
again. It just maintains one global address for that value and all the strings refer to
that. Why does string creates fresh fresh objects?
what is so great about creating these fresh objects, why not just have one value and update
to it? Why by behavior strings are immutable? In the comment you can put down why in C#
are strings immutable? what is the benfit of creating so many copies and why not just
have one copy and update that. Let us have healthy discussion down on the youtube comment.