C# Tutorial Beginners - Part II
Welcome back to another story of C# Tutorial for Beginners. This article is help to prove that string are object !!!
If you are interested to know then stay with me soooo lets get started .
Let’s see some example with there explanation to prove String are objects.
In this above code we assign integer , boolean , double and string. if you think strings are primitive type then my answer is no its not look at the below image then you clearly understand 👇
String is not primitive type so its a class that means string are object and you know very well that object are allocated in heap .
In the below scenario👇 :
Objects are allocated in heap while primitive data types allocated in stack. And remember guys heap memory deallocate or clear by garbage collector(GC).
How do you know that the string is object ?
with the help of this code we check any random number is generated or not. Next() will help to generate non negative random number. Now in order to know that it consuming how much RAM and so on there is one tool is given by internally in visual studio name is Performance Profiler.
Performance Profiler will help you to check the performance of the .net applications. In that one you can check how much of use of CPU, is that using Async & Await , you want to check how much of memory usage is there are any memory leaks and so on…
Now we are going through the .Net Object Allocation tracking. It shows the how many objects are allocated and when they are reclaimed by GC. Once I choose the start then it will be run and show some particular graphs as shown in below 👇.
There are two graphs which are happening here one is Live object and another one is Object delta(%change)
In live objects it shows how much memory is usage for newly created object. Now in above graph it shows only 3kb.
In the Object delta(%change) this green line indicates a new object is created and the red line indicates the object is deleted .
The blue line shown the linear (doesn’t move up down ) means the object are stored in stack
This report clearly says that how much live objects are created and there memory usage and so on …but it all shows (Allocations) in kb .
There is one collection tab is present and it shows how many time the garbage collector(GC) is perform or run .
Now Go through the String and check what happened :
Remember one thing is Y is referencing this X so Garbage collector is never be collected.
Previously the Y is deallocate by Garbage collector but this Y is referencing the X and X is member level is at the class level that means that the object will never be released .
Now we should see the rising graph as shown below 👇
You can see the objects are created but there is no red line is created that means garbage collector is run all the time but never be collected anything (So the object are not deleted)
We are here to prove the strings are object , previously what is happed it create objects of random(new random) because random is class that’s why the live objects are moving up and down as shown in image 1.1
I hope this guide has been helpful for you 😃!!!
To better understand the concepts of C # please do watch the below video👇. It will resolve your all doubts: