.Net Memory Allocations and Performance

Поделиться
HTML-код
  • Опубликовано: 16 июн 2024
  • In order to write efficient and performant code, one has to understand what costs us performance and how not to pay that cost. In this session, we dive into the .NET/C# memory model to gain a better/deeper understanding of what happens in memory when we instantiate various types of classes and structs, and how best to take advantage of this knowledge. We dive deeper still into the hardware, to better understand how modern CPUs and associated hardware such as CPU cache, DRAM, Prefetcher and Branch prediction etc. work together and how, the way we write code can have a huge impact on the performance of our applications.

Комментарии • 120

  • @jaysonknight
    @jaysonknight 5 лет назад +8

    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.

  • @JonatanMachado
    @JonatanMachado 5 лет назад

    Great Video!
    Looking forward to part 2.

  • @garethevans9789
    @garethevans9789 5 лет назад +3

    Superb video. You have a knack for getting the pace and depth just right. I also look forward to video 2.0!

    • @Matlus
      @Matlus  4 года назад +2

      Hi Gareth, Part 2 is now available, please take a look

  • @satyendrasingh3745
    @satyendrasingh3745 4 года назад +5

    I generally don't comment on videos but your videos are really awesome.

    • @Matlus
      @Matlus  4 года назад +2

      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

  • @charankumar5633
    @charankumar5633 Год назад

    I have been trying to understand properly on the memory allocation, finally I found one. Thanks for sharing this knowledge :)

  • @kurnaPunk
    @kurnaPunk 5 лет назад +12

    Fantastic video, part 2 pleeeeease!

    • @Matlus
      @Matlus  4 года назад +2

      @Nicholas Zhang, Part 2 is out. Have you watched it?

  • @dhanasekar4520
    @dhanasekar4520 3 года назад +3

    you are giving this much of knowledge for no cost its incredible. thank you sir.

    • @Matlus
      @Matlus  3 года назад

      Thank you Dhanasekar! And you're welcome.

  • @glaysonpatricio
    @glaysonpatricio 2 года назад

    The amount of details in your explanation is impressive!

    • @Matlus
      @Matlus  2 года назад

      Thank you! I'm happy to hear that you appreciate the details.

  • @davidpike5959
    @davidpike5959 4 года назад +1

    Easily the best video I've seen on this topic, bookmarked. Great work, thank you.

    • @Matlus
      @Matlus  4 года назад

      Thank you David! There is a Part 2 to this one that I'm sure you'll enjoy as well.

    • @davidpike5959
      @davidpike5959 4 года назад +1

      @@Matlus Good man, i'm on it- thanks again :D

  • @aaasd5526
    @aaasd5526 5 лет назад +2

    I'm waiting for the second part!
    Great stuff

    • @Matlus
      @Matlus  4 года назад +2

      Aasad, part 2 is now available. Please check it out

  • @krishnanigalye1173
    @krishnanigalye1173 3 года назад +1

    Thank you for this video. Great in-depth explanation. Even before watching half of this video, I subscribed to your channel.

    • @Matlus
      @Matlus  3 года назад +1

      Thank you Krishna! And thank you for subscribing. There is a Part 2 of this video as well, be sure to watch that.

  • @19gauravmittal91
    @19gauravmittal91 4 года назад +1

    Amazing way of teaching with visualization. Please post about some latest tech like Micro-services.

  • @DK-tr7ir
    @DK-tr7ir 6 лет назад

    Looking forward for Part 2! :)

  • @brianmikinski8984
    @brianmikinski8984 6 лет назад +1

    This is great. Can't wait for part 2.

    • @Matlus
      @Matlus  4 года назад

      Hi Brian, part 2 is now available

  • @DheerajKumarWebDetails
    @DheerajKumarWebDetails 4 года назад +2

    You are just awesome , subscribed for life time

    • @Matlus
      @Matlus  4 года назад +1

      Thank you Dheeraj!

  • @sarbanjeet
    @sarbanjeet 9 месяцев назад

    Thank you so much, I never know the difference between static object and allocation before.

  • @lalithswarna2097
    @lalithswarna2097 4 года назад +1

    Awsome video!! the way you explained is pretty good. Wowww you have a great cinematic voice, i felt like watching a movie while learning :)

    • @Matlus
      @Matlus  4 года назад

      Thank you Lalit, for your kind words.

  • @himanshutyagi1205
    @himanshutyagi1205 3 года назад +1

    A clear visualisation of .net memory layout. Thanks Shiv for this nice video. Completed the 1st part, now going to complete the 2nd one.

    • @Matlus
      @Matlus  3 года назад +1

      So How was Part 2 Himanshu?

    • @himanshutyagi1205
      @himanshutyagi1205 3 года назад +1

      @@Matlus Amazing. Kudos to you for the way you explain everything in details.

    • @Matlus
      @Matlus  3 года назад +1

      @@himanshutyagi1205 Thank you! Appreciate the encouragement.

  • @09FT9
    @09FT9 3 года назад +1

    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!

    • @Matlus
      @Matlus  3 года назад

      Thank you for an inspiring and encouraging comment!

  • @dusanrychnovsky3837
    @dusanrychnovsky3837 5 лет назад +2

    Great video, Shiv, thanks! The part 2 you mentioned isn't available yet, is it? Thank you

    • @Matlus
      @Matlus  4 года назад +1

      Hi
      Dušan, thank you! Yes, part 2 is now available.

  • @rakeshpathania829
    @rakeshpathania829 3 года назад +1

    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...:)

    • @Matlus
      @Matlus  3 года назад

      There is a part 2 Rakesh. You can find it here. ruclips.net/video/Ge0tyJqdhxY/видео.html

  • @woozy_deer
    @woozy_deer 5 лет назад

    This was so helpful!

  • @BobbyCannon
    @BobbyCannon 4 года назад +8

    "Hello Viewers" (bass, bass, all about that bass) - I was like woah...

    • @Matlus
      @Matlus  4 года назад +1

      Be sure to watch Part 2 of this one too Bobby

    • @LaurentLaborde
      @LaurentLaborde 4 года назад +2

      i had the same reaction :D

    • @EminoMeneko
      @EminoMeneko 3 года назад

      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.

  • @kevinm8865
    @kevinm8865 3 года назад +1

    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.

    • @Matlus
      @Matlus  3 года назад +2

      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 :).

    • @kevinm8865
      @kevinm8865 3 года назад +1

      @@Matlus So true! Thanks for the kind words and the education. New sub now!!

    • @Matlus
      @Matlus  3 года назад +1

      @@kevinm8865 Thank you for subscribing Kevin!. By the way, there is a part 2 to this video I think you'll enjoy as well.

  • @donchito6148
    @donchito6148 6 лет назад

    great content

  • @adilk.1090
    @adilk.1090 6 лет назад

    thanks that was very informative

  • @expertreviews1112
    @expertreviews1112 Год назад

    Great content and really informative! Can you share further references where we can go deep in this topic?

  • @conchimxanhxanh255
    @conchimxanhxanh255 4 года назад +2

    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

    • @Matlus
      @Matlus  4 года назад +1

      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!

    • @hydtechietalks3607
      @hydtechietalks3607 3 года назад

      www.pdfdrive.com/writing-high-performance-net-code-e195138098.html

  • @EminoMeneko
    @EminoMeneko 3 года назад +1

    Thank you for the video.

    • @Matlus
      @Matlus  3 года назад

      Thank you! And you're most welcome.

  • @saurabhchauhan232
    @saurabhchauhan232 3 года назад +1

    Excellent Presentation, Going to checkout part 2. 😀

    • @Matlus
      @Matlus  3 года назад +1

      Thank you Saurabh! I'm positive you'll like part 2 as well.

    • @saurabhchauhan232
      @saurabhchauhan232 3 года назад +1

      @@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.

    • @Matlus
      @Matlus  3 года назад

      @@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.

  • @HumbertoPereira1994
    @HumbertoPereira1994 4 года назад +1

    Fantastic Video!!!!!

    • @Matlus
      @Matlus  4 года назад +1

      Thank you Humberto!

    • @Matlus
      @Matlus  4 года назад +1

      Humberto, be sure to watch Part 2

  • @LilPozzer
    @LilPozzer 2 года назад +1

    Hope you'll get more than 10k subs!

    • @Matlus
      @Matlus  2 года назад

      I hope so too. Very close now!

    • @LilPozzer
      @LilPozzer 2 года назад

      @@Matlus Congratz! Really lots of work done!

  • @benjaminb.4043
    @benjaminb.4043 6 лет назад +1

    @Shiv Kumar Hello! Absolutely fantastic video, learnt a lot! Do you still have plans to release the second part of the series?

    • @Matlus
      @Matlus  6 лет назад +1

      @Benjamin B. Thank you! Yes I do plan to make Part 2. It's just taking longer than I expected due to work commitments.

    • @Matlus
      @Matlus  4 года назад

      Hi @Benjamin, part 2 is now available

  • @skarpetky8590
    @skarpetky8590 2 года назад

    Hello Sir! Thank you sir! Your explanation is the best sir! You best of the best sir!

    • @Matlus
      @Matlus  2 года назад

      You are most welcome!

  • @Trhnetesiuz
    @Trhnetesiuz 3 года назад +3

    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!

    • @Matlus
      @Matlus  3 года назад +1

      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.

  • @techypd559
    @techypd559 3 года назад +1

    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)

    • @Matlus
      @Matlus  3 года назад +2

      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.

  • @asutoshrao8316
    @asutoshrao8316 3 года назад

    Awsome explanation. Really enjoyed.
    Can you explained memory allocation in case of inheritance ? Some visuals will really help.

    • @Matlus
      @Matlus  3 года назад +1

      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

  • @trongphan6197
    @trongphan6197 5 лет назад +1

    Thanks again. @Shiv Do you have programming to exception? :)

    • @Matlus
      @Matlus  5 лет назад

      Not yet. @Trong, but I hope to do this one soon.

    • @Matlus
      @Matlus  4 года назад

      Programming to Exceptions is now available Trong.

  • @ArunKumar-ll9lo
    @ArunKumar-ll9lo 3 года назад +1

    Great session..Just wanted to clear one confusion..is array of integer value type..?

    • @Matlus
      @Matlus  3 года назад

      Thank you Arun!. An Array is a reference type. It doesn't matter what the Array is of.

  • @filinskaya
    @filinskaya 5 лет назад +1

    good content! Will part 2 be released?

    • @andreian4303
      @andreian4303 5 лет назад

      Oh, Filinskaya? Is it you?

    • @Matlus
      @Matlus  4 года назад

      Hi
      @Marina, Part 2 is now available

  • @Pablo0145
    @Pablo0145 2 года назад

    Thanks for the explanation, but what about constants, aren't they allocated in memory? as they are interpreted at compile time.

    • @Matlus
      @Matlus  2 года назад

      Check out my video on So you Think you know C#? Constants

  • @DevNerdKonradKokosa
    @DevNerdKonradKokosa 2 года назад +1

    If you find those topics interesting, feel free to watch my in-depth .NET GC Internals series at ruclips.net/p/PLpUkQYy-K8Y-wYcDgDXKhfs6OT8fFQtVm 👀

    • @Matlus
      @Matlus  2 года назад

      Thank you Konrad! You have great content indeed!

  • @trongphan6197
    @trongphan6197 6 лет назад

    @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,

    • @Matlus
      @Matlus  6 лет назад

      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.

    • @trongphan6197
      @trongphan6197 6 лет назад

      Thanks Shiv.

    • @trongphan6197
      @trongphan6197 5 лет назад

      adamsitnik.com/Value-Types-vs-Reference-Types/ I found this article .... really good info

  • @Zapanath
    @Zapanath 4 года назад

    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.

    • @Matlus
      @Matlus  4 года назад +1

      Yes, you're correct in terms of the actual implementation. I was giving folks a conceptual model.

    • @Zapanath
      @Zapanath 4 года назад

      Shiv Kumar, ok i agree but excellent video anyway.

  • @sachinucer
    @sachinucer 2 года назад

    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

    • @Matlus
      @Matlus  2 года назад

      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)

  • @BhanuKer
    @BhanuKer 2 года назад

    @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?

    • @Matlus
      @Matlus  2 года назад

      In Situ. It means in situation or in place.

  • @tileq
    @tileq 2 года назад

    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.

    • @Matlus
      @Matlus  2 года назад

      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.

  • @Sushilkumar-yq7iy
    @Sushilkumar-yq7iy 3 года назад +1

    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
    🙏

    • @Matlus
      @Matlus  3 года назад +2

      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 :)

  • @pramodkumarw
    @pramodkumarw 3 года назад +1

    Great Voice

    • @Matlus
      @Matlus  3 года назад +1

      Thank you Pramod! Did you enjoy the video?

    • @pramodkumarw
      @pramodkumarw 3 года назад +1

      @@Matlus i enjoyed this tutorial .you explained surgeon level memory management

    • @Matlus
      @Matlus  3 года назад +1

      @@pramodkumarw Thank you! Happy to hear that.

  • @JuankQuinteroMejia
    @JuankQuinteroMejia 4 года назад +1

    strings kind of behave like structs but they actually work as classes, that's important to know

    • @Matlus
      @Matlus  4 года назад

      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.

  • @PranilKadamp
    @PranilKadamp 6 лет назад

    Hi... please provide for static class and variable memory management.

    • @Matlus
      @Matlus  6 лет назад

      What do you mean by "variable memory management"?

    • @PranilKadamp
      @PranilKadamp 6 лет назад

      Like if i have static class with static variable then How the memory is allocated internally and where? In depth C#

    • @Matlus
      @Matlus  6 лет назад +1

      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.

    • @PranilKadamp
      @PranilKadamp 6 лет назад

      if you have online resource for the same then please refer i can study it.

  • @Zapanath
    @Zapanath 4 года назад

    not to be pedantic but in the string immutability discussion, the string variable s1 was declared twice, that would be a compile error.

    • @Matlus
      @Matlus  4 года назад +1

      Dang it! PowerPoint has a bug :)

  • @TheInsaneBrother
    @TheInsaneBrother 4 года назад

    Part two is here for everyone interested: ruclips.net/video/Ge0tyJqdhxY/видео.html