Great primer, I hope all of our teams have watched this or been versed in this. It would be great if you could cover string interning as well as this is a very important aspect of how strings are managed by the CLR, and changes how you describe string handling by the CLR by way of the intern pool.
@@Matlus Honestly I tried watching it but it's too advance, and I understand very few things from it. Not your fault but I my self is not well enough to understand such beautiful talk.
@@saurabhchauhan232 Thank for the feedback Saurabh. Yes, Part 2 is quite a bit advanced and delves into the hardware, so it's not for everyone. Don't worry about it though. As you spend more time in software, you may start to understand (or have a need to understand) the hardware.
Nice give and great presentation! I agree completely with your analogy of car driving. It amazes me how often, as an intermediate developer growing his skills, I come across senior devs and architects who haven't taken time out to understand the internals of their languages and tools. I don't expect everyone to dig deep into internals but in code reviews it's irritating when my code is questioned or called wrong because the reviewer lacks the detailed understanding of components /design and assume I'm wrong because I'm less experienced. As new frameworks and tools keep coming out, devs are less and less encouraged to understand and instead choose the easier route of finding/adding another dependency yo abstract the details away. This of course sometimes adds costs to the organization and in nearly all cases adds more complexity to the entire system and yet another possible point of failure.
Thank you Kevin! How true!! all of your observations match with mine as well. The dependencies, the not wanting to know how things actually work and not listening to a dev just because they have less experience than you etc. It's a sad state of affairs really. But if we each do our part, maybe we can collectively affect a change :).
Wao...you make my day @shiv .... I had a lot of confusion regarding memory management but now i have basic understanding of how things works under the hood......and i am looking forward for part 2....Thanks...:)
I think there is a mistake on the slide at 30:42. Type Object Pointer belongs to the object (same as Sync Block Index). Method table is part of Type object.
The Type object pointer is a pointer to the type instance. All objects will have a pointer and they'll all point to the same type instance. The slide could be wrong (I haven't seen it in a while) but as long as you get the concept :) There are two method tables. One for instance methods and one for static methods. Of course this is just a mental model to have the actual implementation is subject to change at any time.
Superb information. From where did you get this information ? Is it published by microsoft? Does the latest .net core framework uses the same principle ?
Hi Techy. Thank you! Looking for memory leaks is an VERY involved process, requiring an in-depth understanding of .NET and memory management. But you're go to have to start with getting a memory dump of the process while the problem is occurring. Visual Studio (new versions) do have the ability to open memory dumps, but you're going to have to learn to decipher what you're seeing as well. You can also use WinDbg and SonOfStrike. Of course prevention is better than cure. A lot of my videos in the Programming With Intent playlist show you what I have learnt as best practices over my career. Following those practices will prevent such issues to a large extent.
Very well explain Sir. for .net core GC working has been changed like workstation gc and server gc works different. When run .Net core app inside container with limited memory allocation then programmer need to choose which gc should be used based on hardware allocation. Could you make a video of how gc working and memory management has been changed in.net core. Thanks 🙏
The choice of which GC to use is clear. Long running applications (like ASP.NET or Background services etc.) should use server GC. Desktop applications should use the workstation GC. The changes are well documented so unless there is something you don't understand (and you tell me) I don't see any value on making a video on this. This video explains what is going on under the hood so C# programmers can be smarter about how they write code and if they have issue, they'll be in a better place to determine what is causing the issue and how to potentially fix the issue. If you program correctly, you shouldn't have issues :)
Hello sir. Thanks a lot for your videos! I am a programming beginner and I learned so much from you. Might I suggest a small recommendation to make it even easier for us: - Share your code & resources in the video descriptions - Create small chapters within the video, so the users can easily jump from one part to another. Something like 0:00 Intro, 2:36 Basics etc. It would make the first time watching and specially revisiting the videos much easier!
Thank you for your comments Mar. I do share code for when I think code it relevant and I do put in Chapter parkers for when I think it's relevant also. The Chapter markers feature was not available earlier and so my older videos don't have chapter markers. But I'm guessing you feel that some videos are missing chapter markers :). I'll try and provide those for videos moving forward.
Every instance of an object will be allocated in a similar fashion. It makes no difference for inherited objects. Descendants are "complete" that is to say, it's not like there is allocation for the ancestor and another allocation for the descendant. The descendant is a complete object in and of itself and thus when it is allocated it is allocation in its entirety
Thank you Tran! Appreciate you taking the time to comment. I can't say I know of any books that describe things at this detail level.. Be sure to watch Part 2 as well!
Great video ! but i don't think the GC does reference counting like python does but rather it will build the object graph and determine which objects are rooted to / reachable by the application in memory.
@23:07 Static variables and members of classes that are value types are allocated inline or `in sitto`. I could not get the last word there that he said. What does he mean when he says in-sitto?
Sorry, I don't understand your question. All methods get their own stack space when you call/execute a method. Method definitions are just definitions, they don't exist anywhere. However, just before being called, if they have not been JIT compiled before, then they will need to be compiled before they can be called and this compiled code also resides in memory (in the global heap and not stack)
You're correct. I'd say it slightly differently. Strings are reference types but behave somewhat like value types from the perspective of equality (value equality rather than reference equality). Additionally, when passing arguments to methods, once again they behave more like value types in that they behave like they are passed by value rather than by reference due to strings being immutable.
@Shiv Kumar I have a question. How under the hook the .Net Framework manage to know which value in the stack as value vs pointer? I might think it use a few bits to classify value type vs reference type? (20 vs 0x....) And how the MSIL really instruct to CPU to handle the value type vs reference type? Thanks,
All value types are derived implicitly from the System.ValueType. When a variable is declared using one of the basic, built-in data types or a user defined structure, it is a value type. An exception is the string data type, which is a reference type.
I explained this in the video. All static variable "hang off of" the static class, similar to how they are for objects. That is value types are in-line and reference types hold only the pointers in-line and they actual object is allocated elsewhere on the heap.
Ik this video is old, but the key concepts remain as relevant. Having to learn c# for my job and this video is a Godsent
If you find those topics interesting, feel free to watch my in-depth .NET GC Internals series at ruclips.net/p/PLpUkQYy-K8Y-wYcDgDXKhfs6OT8fFQtVm 👀
Thank you Konrad! You have great content indeed!
I generally don't comment on videos but your videos are really awesome.
Thank you Satyendra for taking the time to let me know! it is much appreciated. Sorry for the delayed reply, somehow tour comment slipped by
The amount of details in your explanation is impressive!
Thank you! I'm happy to hear that you appreciate the details.
you are giving this much of knowledge for no cost its incredible. thank you sir.
Thank you Dhanasekar! And you're welcome.
A clear visualisation of .net memory layout. Thanks Shiv for this nice video. Completed the 1st part, now going to complete the 2nd one.
So How was Part 2 Himanshu?
@@Matlus Amazing. Kudos to you for the way you explain everything in details.
@@himanshutyagi1205 Thank you! Appreciate the encouragement.
Great primer, I hope all of our teams have watched this or been versed in this. It would be great if you could cover string interning as well as this is a very important aspect of how strings are managed by the CLR, and changes how you describe string handling by the CLR by way of the intern pool.
The way you explain makes it so easy to understand this complex concept. If you ever write a book, I am going to be the first one to buy it!
Thank you for an inspiring and encouraging comment!
Easily the best video I've seen on this topic, bookmarked. Great work, thank you.
Thank you David! There is a Part 2 to this one that I'm sure you'll enjoy as well.
@@Matlus Good man, i'm on it- thanks again :D
I have been trying to understand properly on the memory allocation, finally I found one. Thanks for sharing this knowledge :)
Superb video. You have a knack for getting the pace and depth just right. I also look forward to video 2.0!
Hi Gareth, Part 2 is now available, please take a look
Thank you so much, I never know the difference between static object and allocation before.
You are just awesome , subscribed for life time
Thank you Dheeraj!
Fantastic video, part 2 pleeeeease!
@Nicholas Zhang, Part 2 is out. Have you watched it?
Excellent Presentation, Going to checkout part 2. 😀
Thank you Saurabh! I'm positive you'll like part 2 as well.
@@Matlus Honestly I tried watching it but it's too advance, and I understand very few things from it. Not your fault but I my self is not well enough to understand such beautiful talk.
@@saurabhchauhan232 Thank for the feedback Saurabh. Yes, Part 2 is quite a bit advanced and delves into the hardware, so it's not for everyone. Don't worry about it though. As you spend more time in software, you may start to understand (or have a need to understand) the hardware.
Amazing way of teaching with visualization. Please post about some latest tech like Micro-services.
Nice give and great presentation! I agree completely with your analogy of car driving. It amazes me how often, as an intermediate developer growing his skills, I come across senior devs and architects who haven't taken time out to understand the internals of their languages and tools.
I don't expect everyone to dig deep into internals but in code reviews it's irritating when my code is questioned or called wrong because the reviewer lacks the detailed understanding of components /design and assume I'm wrong because I'm less experienced. As new frameworks and tools keep coming out, devs are less and less encouraged to understand and instead choose the easier route of finding/adding another dependency yo abstract the details away.
This of course sometimes adds costs to the organization and in nearly all cases adds more complexity to the entire system and yet another possible point of failure.
Thank you Kevin! How true!! all of your observations match with mine as well. The dependencies, the not wanting to know how things actually work and not listening to a dev just because they have less experience than you etc.
It's a sad state of affairs really. But if we each do our part, maybe we can collectively affect a change :).
@@Matlus So true! Thanks for the kind words and the education. New sub now!!
@@kevinm8865 Thank you for subscribing Kevin!. By the way, there is a part 2 to this video I think you'll enjoy as well.
Hello Sir! Thank you sir! Your explanation is the best sir! You best of the best sir!
You are most welcome!
Wao...you make my day @shiv .... I had a lot of confusion regarding memory management but now i have basic understanding of how things works under the hood......and i am looking forward for part 2....Thanks...:)
There is a part 2 Rakesh. You can find it here. ruclips.net/video/Ge0tyJqdhxY/видео.html
"Hello Viewers" (bass, bass, all about that bass) - I was like woah...
Be sure to watch Part 2 of this one too Bobby
i had the same reaction :D
Actually too much bass is not good for speech ineligibility.
Bass under 500 Hz shall be tamed
Frequencies between 2 and 4 KHz shall be boosted.
Great video, Shiv, thanks! The part 2 you mentioned isn't available yet, is it? Thank you
Hi
Dušan, thank you! Yes, part 2 is now available.
Thank you for this video. Great in-depth explanation. Even before watching half of this video, I subscribed to your channel.
Thank you Krishna! And thank you for subscribing. There is a Part 2 of this video as well, be sure to watch that.
I think there is a mistake on the slide at 30:42.
Type Object Pointer belongs to the object (same as Sync Block Index).
Method table is part of Type object.
The Type object pointer is a pointer to the type instance. All objects will have a pointer and they'll all point to the same type instance. The slide could be wrong (I haven't seen it in a while) but as long as you get the concept :)
There are two method tables. One for instance methods and one for static methods. Of course this is just a mental model to have the actual implementation is subject to change at any time.
Superb information. From where did you get this information ? Is it published by microsoft? Does the latest .net core framework uses the same principle ?
Awsome video!! the way you explained is pretty good. Wowww you have a great cinematic voice, i felt like watching a movie while learning :)
Thank you Lalit, for your kind words.
Great session..Just wanted to clear one confusion..is array of integer value type..?
Thank you Arun!. An Array is a reference type. It doesn't matter what the Array is of.
Hi Shiv..i really like the way you explain things. Need your help if you can explain proper way to debug production memory leak issues in WCF (.NET)
Hi Techy. Thank you!
Looking for memory leaks is an VERY involved process, requiring an in-depth understanding of .NET and memory management. But you're go to have to start with getting a memory dump of the process while the problem is occurring. Visual Studio (new versions) do have the ability to open memory dumps, but you're going to have to learn to decipher what you're seeing as well. You can also use WinDbg and SonOfStrike.
Of course prevention is better than cure. A lot of my videos in the Programming With Intent playlist show you what I have learnt as best practices over my career. Following those practices will prevent such issues to a large extent.
Great Video!
Looking forward to part 2.
This is great. Can't wait for part 2.
Hi Brian, part 2 is now available
Very well explain Sir. for .net core GC working has been changed like workstation gc and server gc works different. When run .Net core app inside container with limited memory allocation then programmer need to choose which gc should be used based on hardware allocation. Could you make a video of how gc working and memory management has been changed in.net core.
Thanks
🙏
The choice of which GC to use is clear. Long running applications (like ASP.NET or Background services etc.) should use server GC. Desktop applications should use the workstation GC.
The changes are well documented so unless there is something you don't understand (and you tell me) I don't see any value on making a video on this.
This video explains what is going on under the hood so C# programmers can be smarter about how they write code and if they have issue, they'll be in a better place to determine what is causing the issue and how to potentially fix the issue.
If you program correctly, you shouldn't have issues :)
Hello sir. Thanks a lot for your videos!
I am a programming beginner and I learned so much from you.
Might I suggest a small recommendation to make it even easier for us:
- Share your code & resources in the video descriptions
- Create small chapters within the video, so the users can easily jump from one part to another.
Something like 0:00 Intro, 2:36 Basics etc.
It would make the first time watching and specially revisiting the videos much easier!
Thank you for your comments Mar. I do share code for when I think code it relevant and I do put in Chapter parkers for when I think it's relevant also. The Chapter markers feature was not available earlier and so my older videos don't have chapter markers. But I'm guessing you feel that some videos are missing chapter markers :). I'll try and provide those for videos moving forward.
Fantastic Video!!!!!
Thank you Humberto!
Humberto, be sure to watch Part 2
Hope you'll get more than 10k subs!
I hope so too. Very close now!
@@Matlus Congratz! Really lots of work done!
Awsome explanation. Really enjoyed.
Can you explained memory allocation in case of inheritance ? Some visuals will really help.
Every instance of an object will be allocated in a similar fashion. It makes no difference for inherited objects. Descendants are "complete" that is to say, it's not like there is allocation for the ancestor and another allocation for the descendant. The descendant is a complete object in and of itself and thus when it is allocated it is allocation in its entirety
@Shiv Kumar Hello! Absolutely fantastic video, learnt a lot! Do you still have plans to release the second part of the series?
@Benjamin B. Thank you! Yes I do plan to make Part 2. It's just taking longer than I expected due to work commitments.
Hi @Benjamin, part 2 is now available
What a wonderful video! Thanks a lot for all of your efforts doing this.
Would you recommend any books relates to this topic in Dotnet? Thanks
Thank you Tran! Appreciate you taking the time to comment. I can't say I know of any books that describe things at this detail level..
Be sure to watch Part 2 as well!
www.pdfdrive.com/writing-high-performance-net-code-e195138098.html
Great content and really informative! Can you share further references where we can go deep in this topic?
Great video ! but i don't think the GC does reference counting like python does but rather it will build the object graph and determine which objects are rooted to / reachable by the application in memory.
Yes, you're correct in terms of the actual implementation. I was giving folks a conceptual model.
Shiv Kumar, ok i agree but excellent video anyway.
Looking forward for Part 2! :)
Thank you for the video.
Thank you! And you're most welcome.
@23:07 Static variables and members of classes that are value types are allocated inline or `in sitto`. I could not get the last word there that he said. What does he mean when he says in-sitto?
In Situ. It means in situation or in place.
good content! Will part 2 be released?
Oh, Filinskaya? Is it you?
Hi
@Marina, Part 2 is now available
Thanks again. @Shiv Do you have programming to exception? :)
Not yet. @Trong, but I hope to do this one soon.
Programming to Exceptions is now available Trong.
Thanks for the explanation, but what about constants, aren't they allocated in memory? as they are interpreted at compile time.
Check out my video on So you Think you know C#? Constants
why we have defined the Main Method inside the Stack .. does it mean static method will store in stack ? just wanted to clarify on this
Sorry, I don't understand your question. All methods get their own stack space when you call/execute a method. Method definitions are just definitions, they don't exist anywhere. However, just before being called, if they have not been JIT compiled before, then they will need to be compiled before they can be called and this compiled code also resides in memory (in the global heap and not stack)
strings kind of behave like structs but they actually work as classes, that's important to know
You're correct. I'd say it slightly differently. Strings are reference types but behave somewhat like value types from the perspective of equality (value equality rather than reference equality). Additionally, when passing arguments to methods, once again they behave more like value types in that they behave like they are passed by value rather than by reference due to strings being immutable.
Great Voice
Thank you Pramod! Did you enjoy the video?
@@Matlus i enjoyed this tutorial .you explained surgeon level memory management
@@pramodkumarw Thank you! Happy to hear that.
@Shiv Kumar
I have a question.
How under the hook the .Net Framework manage to know which value in the stack as value vs pointer?
I might think it use a few bits to classify value type vs reference type? (20 vs 0x....)
And how the MSIL really instruct to CPU to handle the value type vs reference type?
Thanks,
All value types are derived implicitly from the System.ValueType. When a variable is declared using one of the basic, built-in data types or a user defined structure, it is a value type. An exception is the string data type, which is a reference type.
Thanks Shiv.
adamsitnik.com/Value-Types-vs-Reference-Types/ I found this article .... really good info
not to be pedantic but in the string immutability discussion, the string variable s1 was declared twice, that would be a compile error.
Dang it! PowerPoint has a bug :)
great content
Part two is here for everyone interested: ruclips.net/video/Ge0tyJqdhxY/видео.html
Hi... please provide for static class and variable memory management.
What do you mean by "variable memory management"?
Like if i have static class with static variable then How the memory is allocated internally and where? In depth C#
I explained this in the video. All static variable "hang off of" the static class, similar to how they are for objects. That is value types are in-line and reference types hold only the pointers in-line and they actual object is allocated elsewhere on the heap.
if you have online resource for the same then please refer i can study it.
thanks that was very informative
This was so helpful!