I was not ready for this video 1 year ago! Came back today and I think I understand my games poorer performance issues better...I likely still need to watch this video like 5 more times XD
One rule I usually follow is to use structs to store data that is short lived and either read-only or independent of other copies. Some good examples are event arguments, their main function is to return data to multiple places and be deallocated right after. If you are making a game with many events firing every frame, classes will just overwork the GC, but with a struct there won't be any GC and the stack allocation is minimal.
Awesome video man! I've always been a little unsure of the differences between the two and when to use structs vs classes but you explained it in a way that made complete sense. The animated diagrams you showed of the Stack and Heap were so useful!
Wow you put a lot of effort into this video. And I understood it. Ngl I watched thr first 2 mins and I was lost, but as I continued watching it all made sense! Thanks
Nice to have finally found a channel that gets down to the nitty griddy of coding! Most of these tutorial channels are honestly just pointing out fairly obvious things u can find out on your own... And the big problem with unity related forum searches is, that most of the users are entry level devs and don't understand longevity, or even the questions being asked. Hopefully your content will provide some much needed substance to this void of information! (Ok, that sounded pretty harsh on the other channels, but appreciate the 'deep dive' aspect!)
Fantastic explanation. I learned a lot. Sounds like if your initializing a variable with new it's going on the heap. I never understood why some needed this and some did not now its perfectly clear!
This answered a few questions I didn't even know I had, thanks Taro! Definitely going to have to watch this a couple times over to really absorb all the info, lol.
Great video. There is a lot of advice out there regarding the dangers of heap allocation and garbage collection, so it's good to see one that covers the "other side" of the balance.
Great video! Understanding the difference between values and references was the topic that took me the longest to learn when I was learning how to program
Super, super, super interesting video. Great work! I´ve been following you for a while and with every new video I´ve improved as a dev. Thanks, keep up the good work!
Very nice video and really beautifully explained. For people interested, I want to mention if you want to make sure that a struct does not escape to the heap via collections, boxing or whatever ... you can use the "ref" keyword and create a struct like "ref struct StructName {}", this will make sure you are not doing anything on the heap by accident. ref structs can also contain Span fields, which can be useful.
What a gorgeous way to explain the differences between Value and Reference Types, while still being able to explain the Stack and Heap. Thank you so so much for this!
Thanks for elevating us man! I've learned so much from you and I love being able to pop back in for a refresher when necessary. You mention the 1MB Stack limit, I'm guessing there are few situations where we need to be weary of this, is there a heap limit too? (is that our available RAM?)
That's right, but it's quite limited on 32 bit systems. The program is allocated a certain amount and can continue to request more as required. Glad you enjoyed the video :)
Do you have any sort of FAQ talking about the tools you use to edit and produce these videos? Because as the kids say; this fire be bussin', knaw mean, no buggin'.
Perhaps this was mentioned, but since Classes aren't copied the way structs are, note that one can copy a class by passing the object into the class constructor. var copy = new MyClass(original);
In the example it's only more useful to use class when there's two or more properties IF they are not in scope correct? If they were all inside the function it would be better to use struct right?
Good to know. I am working on my own networking stuff. I was really focused on doing automated serialization, rpcs, networkvariable etc. This helps to understand and to be careful. But even I have over 10 years C#.. I never thought about it the values in side Coords. I really couldn't say if both coordswill have same values if you change it. Or I had blackout :D You should also mention ref keyword. I think it is also very important to know
Great tutorial, thank you very much! Question: what happens if an element buried deep in the stack is ready to be deallocated? Like at 4:56, what if 'one' should be deallocated before 'two' and 'three'?
Kinda off topic but it always annoyed me that there is no mutable string version in C#. Game UI is very dynamic, so even if you want to do a simple thing like a tooltip for a status effect with a duration timer you have to allocate a whole new string after the timer ticks (so for example every second) to update the text. Sure there are things like StringBuilder etc but in the end the ToString call will still allocate a new string instance. The only way to avoid this is to use unsafe code and preallocate a large "buffer" string to modify the char values with pointers, but this can lead to other issues.
I remember I had an issue a while ago where I wrote something like obj1 = obj2, and I couldn't figure out why modifying obj2 would also affect obj1. I eventually realized it was because of references, which I didn't know that's how classes worked in C#. It tripped me up because I was used to C++'s use of the & symbol to denote reference. Also didn't know that structs are value types, that should come in handy. Does any other language do that where structs are value and classes are reference? As far as I can tell, C++ and Java just differentiate on the default visibility.
Is there a way to use the address of a struct to pass it by reference, to avoid the 1000x stack memory problem in your recursive example? 90% of my coding experience is in C where everything is a value type, basic numeric types or structs thereof, so I must admit that I’m more comfortable using structs over classes wherever possible.
"Imagine a world where we couldn't compare strings with the equality operator (s1 == s2). Chaos!" LMAO! Are you taking a jab at Java? 🤭🤭 #tarodevvsjava. Awesome video man!
So on Unity New vector3 always causes ja heap allocation? Should we avoid using it in Update and Coroutines? Instead use a member variable and modify it recursively?
I'm sorry but even what you say about stack and heap generally correct, for csharp it's wrong. All class variables(class field w/e) are stored in the heap not in the stack. In C/C++ you can create a variable without a class so you can keep a variable in the stack but in csharp everything is created in class (could be changed in new implementations Idk). The value of a variable of reference type is a reference to heap memory. So basically , if a variable is not a local variable (created in a function) it's always in the heap. Again this is for csharp not for general.
Damn, I miss spoke a few times in this recording so there are a few annotations! Don't shoot me guys
Also, explode this thread
represent
misbrained
👍
Boom!
waku waku
Finally, I understood the principle of Stack and Heap, You Are A Legend
Your illustrations of the stack and heap allocations where didactically perfect!
You're great at making complicated concepts easy to understand, thank you!
You're welcome!
I was not ready for this video 1 year ago! Came back today and I think I understand my games poorer performance issues better...I likely still need to watch this video like 5 more times
XD
So good coming back and realizing you've grown as a developer. Well done!
Now I know what a stack overflow is. I feel like a computer engineer now !!
One rule I usually follow is to use structs to store data that is short lived and either read-only or independent of other copies. Some good examples are event arguments, their main function is to return data to multiple places and be deallocated right after. If you are making a game with many events firing every frame, classes will just overwork the GC, but with a struct there won't be any GC and the stack allocation is minimal.
this is super useful. i've been using c# for over a year and never knew the difference. thanks a lot!
This tied some of my disparate knowledge bits together, making me a better man
Awesome video man! I've always been a little unsure of the differences between the two and when to use structs vs classes but you explained it in a way that made complete sense. The animated diagrams you showed of the Stack and Heap were so useful!
Finally now i know what does the stack overflow error mean.
It all makes sense 😊
Just a few days ago i found your videos and love your work ..... Plz don't stop
The visuals looked like they took a lot of work to create, but they really helped me understand what's happening under the hood. Thank you!
That makes it worth the effort 😊
I finally get stack and heap, thank you soooo much.
Been coding in unity for like 5 years now. THis is still not just beyond the scope of what my brain can access, but im afraid it will forever be.
Wow you put a lot of effort into this video. And I understood it. Ngl I watched thr first 2 mins and I was lost, but as I continued watching it all made sense! Thanks
Nice to have finally found a channel that gets down to the nitty griddy of coding! Most of these tutorial channels are honestly just pointing out fairly obvious things u can find out on your own... And the big problem with unity related forum searches is, that most of the users are entry level devs and don't understand longevity, or even the questions being asked. Hopefully your content will provide some much needed substance to this void of information! (Ok, that sounded pretty harsh on the other channels, but appreciate the 'deep dive' aspect!)
Glad I could provide some lower level content for you :)
thanks I watched the whole thing but I want to hammer these topics into my head so ima come rewatch later on
Man, you are a truly a savior to my C# Unity knowledge !!!
Happy to help 🙏
Fantastic explanation. I learned a lot. Sounds like if your initializing a variable with new it's going on the heap. I never understood why some needed this and some did not now its perfectly clear!
Very cool I was looking for this the other day. Glad you made a video about this topic.
What’s more impressive? The c# or the after effects work making the c# understandable. Both are! Thanks brother.
Took me ages my man. I need to skill up on my editing
Amazing content man, please never stop, if I wasn't currently unemployed you would be my debut patreon subscription in a heartbeat.
This answered a few questions I didn't even know I had, thanks Taro!
Definitely going to have to watch this a couple times over to really absorb all the info, lol.
Watched this 2 days ago and today I finally fixed a bug caused by interpreting this stuff wrong last week lol
That was the best explanation I didn't know I needed THANK YOU.
You're welcome soso :)
Covered so much stuff I needed to learn… awesome!
VERY new dev here. Don’t understand shit but still very well made videos 👍
Excellent video, somehow this all makes more sense when someone tells me about it instead of just reading about it.
Great video. There is a lot of advice out there regarding the dangers of heap allocation and garbage collection, so it's good to see one that covers the "other side" of the balance.
This is extremely helpful thanks man. I never understood the difference before.
Thank you for the helpful informations!
Really good rundown! Love the stack/heap graphics. One day maybe I’ll understand it as well
Great video! Understanding the difference between values and references was the topic that took me the longest to learn when I was learning how to program
Awesome lecture!
Awesome video, Need more of this content !!!
Super, super, super interesting video. Great work! I´ve been following you for a while and with every new video I´ve improved as a dev. Thanks, keep up the good work!
Great video 😊 I've totally enjoyed watching it
Very nice video and really beautifully explained.
For people interested, I want to mention if you want to make sure that a struct does not escape to the heap via collections, boxing or whatever ... you can use the "ref" keyword and create a struct like "ref struct StructName {}", this will make sure you are not doing anything on the heap by accident. ref structs can also contain Span fields, which can be useful.
Quaternions!
Soooooooon
Good entry video. The next one should go deeper and explore passing structs by ref for even better perfs.
"Imagine a world where we couldn't compare strings with the equality operator" - welcome to java! 😂
this...was REALLY well explained. Thanks so much for making all these videos!
Great lesson, comment for algorithm
Much appreciate for the video! I wonder if there are good written detailed resources on these topics
ROFL "better him than me, " i saw the little "fu" in there haha
Thank you
Super helpful thank you 💯💯💯
What a gorgeous way to explain the differences between Value and Reference Types, while still being able to explain the Stack and Heap.
Thank you so so much for this!
Great explanation and demo
Thanks for elevating us man! I've learned so much from you and I love being able to pop back in for a refresher when necessary. You mention the 1MB Stack limit, I'm guessing there are few situations where we need to be weary of this, is there a heap limit too? (is that our available RAM?)
That's right, but it's quite limited on 32 bit systems. The program is allocated a certain amount and can continue to request more as required.
Glad you enjoyed the video :)
Do you have any sort of FAQ talking about the tools you use to edit and produce these videos? Because as the kids say; this fire be bussin', knaw mean, no buggin'.
I use premiere and after effects my man. I'm glad the visuals added to the experience as I remember them taking quite a long time.
Welcome back nerds... The what did you just call me!
My boy you clicked on a video called "Value & Reference types in C#. Write Better Code!" you have no defense!
Neither do I!
@@pj-wille You nerds
@@pj-wille Dammit, fair enough.
Great video dude, subbed, even though you called me a nerd at the beginning.
Thanks nerd
Perhaps this was mentioned, but since Classes aren't copied the way structs are, note that one can copy a class by passing the object into the class constructor.
var copy = new MyClass(original);
No? Not unless the class specifically has support for that.
@@MulleDK19 Meaning any class with a constructor. There is no extra code.
Holy fuck this was an epiphany ... For so long I was wondering why certain variables became references, while others became copies. >.
64 bit comment - nice video! :)
Talking about multi threading....do you have any future plans for Unity DOTS tutorial....?
In the example it's only more useful to use class when there's two or more properties IF they are not in scope correct? If they were all inside the function it would be better to use struct right?
Good to know. I am working on my own networking stuff. I was really focused on doing automated serialization, rpcs, networkvariable etc. This helps to understand and to be careful.
But even I have over 10 years C#.. I never thought about it the values in side Coords. I really couldn't say if both coordswill have same values if you change it. Or I had blackout :D
You should also mention ref keyword. I think it is also very important to know
Great tutorial, thank you very much! Question: what happens if an element buried deep in the stack is ready to be deallocated? Like at 4:56, what if 'one' should be deallocated before 'two' and 'three'?
i like your voice
Always hitting me with the compliments
What happens to prefab, suppose we have a prefab list, they all go into memory? , I assume they are only on the list, not instansiet on scene.
Kinda off topic but it always annoyed me that there is no mutable string version in C#. Game UI is very dynamic, so even if you want to do a simple thing like a tooltip for a status effect with a duration timer you have to allocate a whole new string after the timer ticks (so for example every second) to update the text. Sure there are things like StringBuilder etc but in the end the ToString call will still allocate a new string instance. The only way to avoid this is to use unsafe code and preallocate a large "buffer" string to modify the char values with pointers, but this can lead to other issues.
I remember I had an issue a while ago where I wrote something like obj1 = obj2, and I couldn't figure out why modifying obj2 would also affect obj1. I eventually realized it was because of references, which I didn't know that's how classes worked in C#. It tripped me up because I was used to C++'s use of the & symbol to denote reference.
Also didn't know that structs are value types, that should come in handy.
Does any other language do that where structs are value and classes are reference? As far as I can tell, C++ and Java just differentiate on the default visibility.
if anyone can answer what does he mean by the pointer for the struct at 10:16
🤓
Is there a way to use the address of a struct to pass it by reference, to avoid the 1000x stack memory problem in your recursive example? 90% of my coding experience is in C where everything is a value type, basic numeric types or structs thereof, so I must admit that I’m more comfortable using structs over classes wherever possible.
Love your content and style, but you need better lighting. (coming from another developer youtuber)
Really informative!
Thanks
And didn't know about these strings :o
Tricky little buggers
Походу видео о кватернионах это аналог гайда на Брюмастера
So you are telling me stackoverflow isn't just a random name picked for a website.................?!?!?!?!?!
Crazy but true 😜
"Welcome back nerds" *clicks like*
"Imagine a world where we couldn't compare strings with the equality operator (s1 == s2). Chaos!" LMAO! Are you taking a jab at Java? 🤭🤭 #tarodevvsjava. Awesome video man!
So on Unity New vector3 always causes ja heap allocation? Should we avoid using it in Update and Coroutines? Instead use a member variable and modify it recursively?
Unitys Vector3 is also a struct, so no heap allocation there. It's perfectly fine to use in update.
I can feel the struct excitement coursing through my veins
funny looking python code.
Kinda hard code to undetstand
Need some exmpls from the typical game dev solutions
int one = 12;
I think this line breaks the immersion
It is a bit silly
Still waiting for the garbage collector video
Stillwaitingskeleton.png
Yo... you cut your hair. I thought you would go wild man. A bit disappointing. Video is quality tho 😄
I didn't! Don't worry, it'll grow faaaaaaast
@@Tarodev oh good! and yeah I learned a tonne here so tyvm
Data is stored in the balls.
lol
I thought struct is faster than class for entire time.
I'm sorry but even what you say about stack and heap generally correct, for csharp it's wrong. All class variables(class field w/e) are stored in the heap not in the stack. In C/C++ you can create a variable without a class so you can keep a variable in the stack but in csharp everything is created in class (could be changed in new implementations Idk). The value of a variable of reference type is a reference to heap memory. So basically , if a variable is not a local variable (created in a function) it's always in the heap. Again this is for csharp not for general.
That's exactly what I said though? 🤔
@@Tarodev You did create some variables outside of a function and and said these are in stack? Sorry m
aybe I misunderstood?