Where are types allocated in .NET and why people get it so wrong

preview_player
Показать описание

Hello everybody I'm Nick and in this video I am going to break down where reference and value types get allocated in .C#. This is a very common question that pops up on interviews as well and peopl get is so wrong. There are some very simple rules that if you follow you are able to infer where something will be allocated and in this video I will show you how you can learn them as well.

Don't forget to comment, like and subscribe :)

Social Media:

#csharp #memory #clr
Рекомендации по теме
Комментарии
Автор

👍🏽 “completely random number” picked. Lol

clearlyunwell
Автор

04:30 Perhaps another explanation to the same thing: A value type is always on the "local memory frame" of its parent scope. In the application object type, it's the "local memory" of the object on the heap. In the method it's the "local memory" of the process, which is in this case the stack frame.

emmepombar
Автор

Please keep up the videos! I swear you put out videos, on specific topics I am currently learning, every single time. You have helped me push my skill set to the next level in many areas. Thank you very much.

ericm
Автор

Nick Chapsas is a lowkey master of comedy

liquidpebbles
Автор

Nice video. 0:25 The part of newly created reference type is the reference itself, which will be allocated on stack. So I think it is right to say that reference types will allocate memory on heap and stack.

norbertn
Автор

Did anyone think that “value types are allocated on the stack” meant value types inside heap-allocated reference types? You only have to think about how objects are represented in memory to realize it can’t work that way.

davidwright
Автор

The video contains good expertise on how the memory allocations is working in .NET tech. Btw, people aren't wrong about it. They "understand it" that any primitive type incapsulated into an object is a part of that object (like a package or frame) and is stored into heap. Thanks for interpolation investigation. I had not time to investigate its boxing behaviour.

This is why I like the modern C#. Being a very high level coding language, it has many tools to control the memory in different scenarios.

oldclient
Автор

Dude, I had exactly the same argument/discussion about where value types are allocated, exactly on the same day you uploaded your video :). If I had known I would have sent your link and save a lot of time explaining :).

nerrierr
Автор

That's interesting, I wouldn't have thought this distinction would need to be made. I've probably said the same thing before, but to me it's natural that the primitive components of reference types are stored on the heap (otherwise, what would "reference types are stored on the heap" even mean..?)
I'm not incredibly knowledgeable about .NET internals, but I think local primitive types can also be register allocated as an optimization? It would make no sense if they couldn't. I suppose the only examples where you showed that they were on the stack required it because of pointers, so they probably can be.

berylliosis
Автор

I would add one more bullet.
6. Value type declared as a collection item (array, list, etc.) => Heap.

This might be considered a special case of "member of class", but still.

sergeybenzenko
Автор

Outstanding of all other reference types, an Array instance can be allocated on the stack by 'stackalloc' keyword and can be used by a pointer to its first element

КириллМурзич
Автор

Hmm, I've heard somewhere, that string interpolation is lowered to concatenation wherever it's possible, and string.format if it can't be simply concatenated in newer versions of dotnet

kamikadze
Автор

First of all, congratulations on all your videos, always interesting and very well explained.
It would be nice if you can do some video on serverless architecture based on microservices, especially how to build a web API using Azure function and c#, integration with CosmosDb, strong authentication, best practices, tests, etc.
It's just an idea, but I think it would be a very interesting series of videos.

simoneurbani
Автор

Duuude! After almost every video I open IDE and try all that myself! This is awesome! Thanks for the HxD by the way

TedFanat
Автор

Excellently presented and articulated presentation of allocations on Stack vs Heap. Really cleared up my understanding of it. Keep up the excellent work.

PedrosBanioles
Автор

Thank you for explaining... This highlighted a number of subtleties I didn't have a clear picture of. 👍

millchk
Автор

Very deep and clear explanation, extra thanks for conclusions in the end of the video

ЕгорФедоренко-сщ
Автор

Good explanation, but I kind of think that using it to say that the common phrase is 'wrong' is just a tiny bit pedantic. When people say 'reference types are allocated on the heap, value types on the stack' they still mean the behavior you describe, or at least they should.
Otherwise, they would actually think that *everything* is allocated on the stack since all reference types are just compositions of value types (eg. primitives and pointers/references).

filiecs
Автор

That was very useful. especially the wrap-up section at the end with the text. Thank you very much Nick

gembel
Автор

Very nice explanation and covers the nuances. Any plans to make a similar video on garbage collector?

markf