Chúng tôi không thể tìm thấy kết nối internet
Đang cố gắng kết nối lại
Có lỗi xảy ra!
Hãy kiên nhẫn trong khi chúng tôi khắc phục sự cố
Bên trong C#: Stack & Heap, Value Types, Boxing, stackalloc + More
C#, as a language, is all about flexibility. Sure, the ability to rapidly prototype and deliver on code quickly, without worrying about safe memory management or performance, is one of its greatest strengths. But when the moment comes that you need ultra-efficient, C++-comparable code for a task, the tools are all there for you to pull out and leverage, and this video is the first step in doing just that!
I feel there's a bit of a gap in those who understand the memory layout and implications of their C# code and those who don't, and wanted to try to fill that gap a little.
SUMMARY:
The video starts by looking at the stack and the heap, the two core regions of memory in .NET. It glosses over the LoaderAllocator and Native Heaps too as options.
For the stack, the characteristics are first explained; the fact that you can push and pop to it like a regular stack, but get/set middle items. Then I go through how this actually maps to methods and locals, with calling and returning from functions. I cover some of the special features of the stack, such as being used for return addresses and parameters (I probably should've mentioned the rbx register here, but you live and learn). For the heap, I explain how closely the heap's implementation is tied to the GC (Garbage Collector), the reason and function of the garbage collector, and how classes and other reference types go onto there. I use GCHeap::Alloc as an example of the GC being tied to the heap, which you can find in this file: https://github.com/dotnet/runtime/blob/main/src/coreclr/gc/gc.cpp
Now I move onto value types and reference types, the very tools that allow you to select your memory layout. I start by explaining reference types' function, as they're the most common and strangest one of the two, and putting value types after allows me to contrast the difference between the two greater. With reference types, the behaviour of placing 4-8 byte reference at the position they're passed and allocating contents to new locations on the heap is brought up, as well as the noticeable impacts this whole reference ideas has on code. After this, I talk about the object headers that come on reference types and their impact. With value types, the behaviour of storing inline and not containing a header is my main focus, as well as the lack of inheritance on them.
Next up is an analysis of the performance impact value types can hold compared to reference types. I discuss the importance of using BenchmarkDotNet for benchmarks, then proceed to do so for an array of a million struct and class items. After showing this, I explain the impact of allocations, garbage collection, dereferencing (and the indirections thereof), and object headers/method tables.
Once showing the massive performance flaws reference types have, I explain why reference types are used at all. This is done by highlighting the flaws in doing everything with value types. Some flaws I highlight are the constant data copying at every pass from variable to variable, the lack of heap access with value types alone, as well as the complete lack of support for inheritance or threading on value types.
Along the way, I also get a chance to talk about null. The fact that reference types support null because the meaning of the value is a reference of 0, and the fact that value types can be opted into null using Nullable T (to attach a boolean to the struct to allow the state to be represented). I also take the chance to mention not to worry about nullable reference types for memory changes, and only see them as a cosmetic feature.
Finally, the video is closed out with an explanation of boxing (to explain the ability to place structs into interface, object or ValueType types of variables) and the stackalloc keyword to create arrays on the stack.
CHAPTERS:
00:00 Memory Regions
03:00 The Stack
08:15 The Heap
10:25 Data Types
12:00 Reference Types
18:10 Value Types
19:55 Inheritance In Memory
21:05 Performance
27:52 Why Reference Types?
33:05 Nullability In Memory
34:30 Common Uses for Value Types
36:50 Boxing
39:55 stackalloc
CREDITS:
Script, Recording & Editing: Alex Burrows
Desktop Background: @ndumiphotos (Instagram)
GC Internals Images: Pulled from dotnetos https://goodies.dotnetos.org/
MORE INFO:
Thanks for watching my video, before are some general ABMedia links if you are interested:
Website ► https://abco.live/
Discord ► https://abco.live/discord
A huge thank you goes to @ndumiphotos (on Instagram) for the graphics used in the intro.
#csharp #dotnet #span
Dịch Vào Lúc: 2025-03-03T17:41:09Z