What is Boxing in C# and how it affects memory and speed

Поделиться
HTML-код
  • Опубликовано: 16 май 2021
  • Become a Patreon and get source code access: / nickchapsas
    Check out my courses: dometrain.com
    Hello everybody I'm Nick and in this video I am going to explain what is Boxing and Unboxing in C# and how it is affecting your memory allocations and your application's speed. This is nothing you should worry about but it is something you should be aware of. Understanding it can greatly help you make better code flow and data structure choices.
    Don't forget to comment, like and subscribe :)
    Social Media:
    Follow me on GitHub: bit.ly/ChapsasGitHub
    Follow me on Twitter: bit.ly/ChapsasTwitter
    Connect on LinkedIn: bit.ly/ChapsasLinkedIn
    Keep coding merch: keepcoding.shop
    #csharp #boxing

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

  • @2D_SVD
    @2D_SVD 3 года назад +84

    I'm starting to suspect that this 420 is not REALLY a random number

  • @andrewbennett5733
    @andrewbennett5733 Месяц назад

    I learned software with C#/.NET, then immediately ended up in a Java/Spring job for two years. I'm now getting ready to start a new job that uses C#/.NET in a few weeks and your videos are really helping me remember all the things I've forgotten as well as REALLY learning the things I skipped over the first time! Thanks for taking the time to make such great educational content!

  • @filcondrat
    @filcondrat 3 года назад +28

    code like a butterfly, debug like a bee

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

    Even the topics I think I know well, like this one, I still watch your videos and learn a lot. Didn't know that non generic data structures did box the underlying values. It makes sense. Thanks a lot!

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

    nice short and steady explanation, like it :)

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

    Love the content, thanks for sharing!

  • @mosth8ed
    @mosth8ed 3 года назад +12

    More videos like this about performance best practices and ways to overcome both common and overlooked situations! The more it relates to Unity/games the better, but the enterprise / desktop application examples you have been using should work just as well.

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

    good and clear explanation.

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

    good! this reminds me the days when there were no generics in C# and I used to inherit from simple collection class for all my classes.

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

    Great selection of random numbers 👌 👍

  • @mihaimyh
    @mihaimyh 3 года назад +20

    Can you explain in a future video the differences between stack an heap allocation?

    • @berylliosis5250
      @berylliosis5250 3 года назад +20

      Stack allocation: Temporary, cheap storage for local variables. Stack allocations are almost completely free - about as expensive as a single assignment. However, in C#, you can only store primitive types, structs, and the reference itself on the stack.
      Heap allocation: Expensive, garbage-collected storage for any kind of variable. Heap allocations are very expensive (as he said in the video, can be 20 times as expensive as a single assignment), _and_ accessing things that are allocated on the heap is slower than accessing things from the stack (locality of reference, etc etc advanced performance stuff). In (safe) C#, _all_ reference types are heap allocated, as as are the boxed primitive types shown in the video.
      Heap allocation is necessary because stack allocation size must be fixed at compile time; however, it must be explicitly freed - that's the job of the GC, although some languages let you free it manually for better, more consistent performance. The GC and automatic heap allocation get rid of some of the problems that can happen from having real pointers and direct access to the heap, but they also cause significant performance drops (this is part of why C is so much faster than C#).

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

    For performance intensive software like Game Servers, this is a very important thing to keep in mind.

  • @StockportJambo
    @StockportJambo 3 года назад +10

    I find that if I try and mix 69 and 420 with coding, I don't code as well.

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

    Hi Nick, could you please explain what is stack indeed? What is heap is clear, but what is stack? Where physically this type of memory is?

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

    Great content man. Also, by the numbers you use, I see you are a man of culture 🚬

  • @ibrahimfikry6624
    @ibrahimfikry6624 5 месяцев назад

    Perfect ❤

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

    Thanks

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

    Hey Nick thanks for the great content but I was wondering what is that Ide or plugins you're using for IL and memory allocation view?

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

      That's a native feature of my IDE called JetBrains Rider.

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

    What I don't understand is the boxing/unboxing warnings. They are stated like I have a choice. I don't think most people are doing this just because. When it comes up you usually can't avoid it.

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

      Some times you can, but it might worth it due to potential readability issues. It's a tricky one and it really depends on how important memory allocations are to your application's flow.

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

    How to organize entity, business logic, dto in enterprise level rest apis? Which patterns should we follow?

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

    Great! but what about boxing and unboxing reference types? Is there any performance/memory allocations concerns?

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

      Reference types will not be boxed, only value types. When passing a reference type as an object, no conversion will take place, and no memory will be allocated as every reference type is by definition of type object. When you want to cast an object to its original (reference) type you will incur a small performance penalty, but no memory will be allocated.

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

    Please make a video about struct via interface boxing. IEnumerable, IEnumerator - foreach boxing cases. Lambda an local methods memory allocation.

  • @msafari1964
    @msafari1964 8 месяцев назад

    🎉❤ very good

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

    Wow just noticed the Ryzen 5950X 👍. Another set of learning.

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

    You didn’t explain what’s the definition of boxing and why it is used for or how does it compare to Nullable, and history about it which would give a lot to the context.

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

      I never explain things with the textbook definition because it’s hard to understand but I did explain what boxing is. The nullable point is a good one, I should have mentioned that

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

      @@nickchapsas Thank you for agreeing on that one. I’m just savvy fan and wanted to share my thoughts because I know this is common question during interviews. Thanks for you channel, I love it!

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

    @nick Which editor(IDE) you are using? IF you are using Visual studio then how you configure with so beautifully?

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

      This is not Visual Studio. It's JetBrains Rider, a completely different IDE.

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

      @@nickchapsas thanks for quick response. I will be looking forward to improve my skills by watching your video.

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

      @Nick Can you create some videos on linq using cosmos dB . How can we write effect query. And what we should keep in mind while writing linq with cosmos dB.

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

    If I'm correct boxing means pointers in C++ which is very easy and not confusing as here

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

    @nick Chapsas Can you or someone please help me with the extensions for IL viewer and Memory window. IL not working and could not find Memory window.

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

    Hello, what extension are you using to see the code generated in the dll? Thanks!

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

      There is no extension. It's just built into my IDE, JetBrains Rider

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

    Heh, 420 is the number he picks. must be planning something special after the video.

  • @aapris1986
    @aapris1986 5 месяцев назад

    How to find this memory window at VS2019?

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

    wow

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

    But why does the int32 take 24 bytes? an int take only 4 bytes..

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

      "All value types implicitly inherit from the class System.ValueType, which, in turn, inherits from class object."
      Int32 is actually a struct. It's still an object so that you can use inherited methods like .Equals() .ToString() etc but when you assign one int to another it changes the value when reference types change the reference to the different object. The other object still exists until the GC cleans it up.
      Anyway so it uses a bit more memory to do this.

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

    👍

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

    Amazing content. But your are speaking so fast))

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

    Uuh second video with 420 and 69 as "random ints".. i smell smooth brained diamond handed $GME ape 😅
    Thanks for your work Nick !

  • @user-wv8xz3vi3l
    @user-wv8xz3vi3l 3 года назад

    shortly - use generic list! use strong types!

  • @aaronkoller8811
    @aaronkoller8811 3 года назад +9

    69 to 420 is not THAT random. :)

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

    69 and 420 good example numbers 😂

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

    420 random number

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

    69 and 420? lol bro?!

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

    Please be slower on your explanation!

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

    20 times more expensive than a normal assignment, yikes. Heap allocation is slow.