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
I'm starting to suspect that this 420 is not REALLY a random number
Well he uses 69 too, to increase randomness.
Maybe 420 is a source of entropy
The seed of this random number generator is green
🤣
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!
code like a butterfly, debug like a bee
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!
nice short and steady explanation, like it :)
Love the content, thanks for sharing!
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.
good and clear explanation.
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.
Great selection of random numbers 👌 👍
Can you explain in a future video the differences between stack an heap allocation?
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#).
For performance intensive software like Game Servers, this is a very important thing to keep in mind.
I find that if I try and mix 69 and 420 with coding, I don't code as well.
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?
Great content man. Also, by the numbers you use, I see you are a man of culture 🚬
Perfect ❤
Thanks
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?
That's a native feature of my IDE called JetBrains Rider.
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.
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.
How to organize entity, business logic, dto in enterprise level rest apis? Which patterns should we follow?
Great! but what about boxing and unboxing reference types? Is there any performance/memory allocations concerns?
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.
Please make a video about struct via interface boxing. IEnumerable, IEnumerator - foreach boxing cases. Lambda an local methods memory allocation.
🎉❤ very good
Wow just noticed the Ryzen 5950X 👍. Another set of learning.
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.
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
@@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!
@nick Which editor(IDE) you are using? IF you are using Visual studio then how you configure with so beautifully?
This is not Visual Studio. It's JetBrains Rider, a completely different IDE.
@@nickchapsas thanks for quick response. I will be looking forward to improve my skills by watching your video.
@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.
If I'm correct boxing means pointers in C++ which is very easy and not confusing as here
@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.
Hello, what extension are you using to see the code generated in the dll? Thanks!
There is no extension. It's just built into my IDE, JetBrains Rider
Heh, 420 is the number he picks. must be planning something special after the video.
How to find this memory window at VS2019?
wow
But why does the int32 take 24 bytes? an int take only 4 bytes..
"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.
👍
Amazing content. But your are speaking so fast))
Uuh second video with 420 and 69 as "random ints".. i smell smooth brained diamond handed $GME ape 😅
Thanks for your work Nick !
shortly - use generic list! use strong types!
69 to 420 is not THAT random. :)
69 and 420 good example numbers 😂
420 random number
69 and 420? lol bro?!
Please be slower on your explanation!
20 times more expensive than a normal assignment, yikes. Heap allocation is slow.