Stack, Heap, and Static Memory

Ғылым және технология

This is the first video in a series of videos about memory management. In this first video I will be discussing memory regions we must know about as Rust developers. Specifically the Stack, Heap, and Static memory.
📝 Get your FREE Rust cheat sheet: www.letsgetrusty.com/cheatsheet
Chapters:
0:00​ Intro
1:48 Computer Resources
2:16 Persistent vs Volatile Memory
4:07 Stack, Heap, and Static Memory
9:04 Outro

Пікірлер: 91

  • @letsgetrusty
    @letsgetrusty2 жыл бұрын

    📝 Get your *FREE Rust cheat sheet* : www.letsgetrusty.com/cheatsheet

  • @toolb0x_

    @toolb0x_

    2 жыл бұрын

    does not work.. :-/

  • @xy9439

    @xy9439

    2 жыл бұрын

    Can confirm, not working

  • @letsgetrusty

    @letsgetrusty

    2 жыл бұрын

    @@toolb0x_ It's working for me. Might take a minute for the email to arrive. It might also be in your spam folder. If you can't find the email please reach out to me at bogdan@letsgetrusty.com

  • @toolb0x_

    @toolb0x_

    2 жыл бұрын

    @@letsgetrusty i have send you an email. looks like others have the same problem..

  • @toolb0x_

    @toolb0x_

    2 жыл бұрын

    now it is working! Thanks Bogdan :-)

  • @lukejagodzinski
    @lukejagodzinski2 жыл бұрын

    Great video! Can't wait for the next one in the series. I used to program in C++ when I was kid. I read several books back then but none explained this topic. You just sit down and start programming. I think it should be the first thing that a programmer learns. Even if you don't manage memory manually, like in JS, it's always better to understand how your program works. Recently I read that in embedded systems there is no heap and there is Rust version for such systems. Would be nice if you get even deeper into this subject :). But anyway, keep doing great work! :)

  • @arjentix
    @arjentix2 жыл бұрын

    As an embedded software developer I have one another thing to mention about heap memory. It's fragmentation. If you are developing software for aircraft or nuclear power plant you should care about it and probably use only stack and static memory

  • @dmitriidemenev5258

    @dmitriidemenev5258

    2 жыл бұрын

    It's not exactly correct. To be precise, you should prefer stack and static memory by default. One of the huge problems with stack and static memory is that it must be provided by the system (assuming it's being executed in some OS) and it's not always possible to reserve sufficiently large chunk of memory for plenty of reasons. Because of that, sometimes allocation is inevitable but even so indeed it should happen only once. On Linux, the stack size limit is at 8/10Mb depending on the distribution. You can increase the limits but it's a slippery path and a serious question whether you truly need.

  • @mlenox

    @mlenox

    2 жыл бұрын

    Memory management can handle fragmentation. Still good to know the concept for use cases where it might be an issue. No need to refrain from using heap though.

  • @PrivateUsername

    @PrivateUsername

    2 ай бұрын

    As another embedded developer, I agree. In fact, for custom HW, dynamic memory is absolutely forbidden. If you don't know the size of something, go back to the HW devs and ask the size of the storage/block/register/packet/etc.

  • @brandonhunter3036
    @brandonhunter3036 Жыл бұрын

    A picture is worth a thousand words.. I've seen various memory diagrams for Rust but this is by far the most clear and meaningful I've seen thus far. Thanks much!!

  • @ImranSheikh-kg4qd
    @ImranSheikh-kg4qd2 жыл бұрын

    Ok, Another great rust video, Its time to like it. Oh wait, its already liked 🤔 . Ah I liked it subconsciously 😂

  • @GolangDojo
    @GolangDojo2 жыл бұрын

    Congrats on 20K boi :)

  • @MikeAnSean
    @MikeAnSean2 жыл бұрын

    ur a legend bogdon, I wanted more info on this & u read my mind

  • @lsoldano93
    @lsoldano932 жыл бұрын

    I wish my Operating Systems professors in college had explained this as clearly, thanks for the refresher.

  • @deez_gainz
    @deez_gainz2 жыл бұрын

    Awesome idea, there are so many videos about stack and heap but not so many quality content about memory management strategies, hope you nail it!

  • @jorgeosorio1613
    @jorgeosorio1613 Жыл бұрын

    Thank you so much, this video was very helpful.

  • @bjugdbjk
    @bjugdbjk2 жыл бұрын

    Awesome !! I guess rust is really different when u coming from high level programming background and this series really going to help !! Thanks a ton brother !! Cheers from INDIA ... by the way ur rust book playlist is amazing !! Anyone who wants to step into that playlist s the Best .

  • @jakubstiburek8316
    @jakubstiburek83162 жыл бұрын

    Привіт. Thanks for amazing videos. I'm also a web dev so I really appreciate mainly these videos to get familiar with the low level programming concepts.

  • @LNMLucasMasiero
    @LNMLucasMasiero2 жыл бұрын

    Thanks you so much ♥♥♥. Your videos are really helpful and this one I've found it interesting. Didn't know that Heap memory was shared between the all threads. Its like static is the first level, then stack goes up with a limited size of memory that can consume but doesn't mean itll always consume that amount, like a dynamic range. And the heap as the unlimited. So interesting.

  • @seguidor777
    @seguidor7772 жыл бұрын

    Thanks for posting these series, they are simple and clear, very easy to understand. Keep up the good work

  • @joelimbergamo639
    @joelimbergamo6392 жыл бұрын

    Yasss! Thanks I was waiting for a video like this! It would be awesome if you did a video explaining how are diferent variable types stored in memory for example struts with traits and vectors.

  • @richb0199
    @richb01992 жыл бұрын

    This is very important information for systems development. When I was working with Assembly and C/C++ it was imperative to understand memory and memory management. With languages like Java and C#, the garbage collector makes it less important to know. Probably why Rust is perceived to have a steep learning curve. Excellent video

  • @davidmcbride5357
    @davidmcbride53572 жыл бұрын

    Thanks for playlists ❤️

  • @MateusCavalcanteFonseca
    @MateusCavalcanteFonseca2 жыл бұрын

    so great content bro, love from Brazil

  • @farzadmf
    @farzadmf2 жыл бұрын

    Nice video, looking forward to other videos in the series

  • @naughtrussel5787
    @naughtrussel57872 жыл бұрын

    Helpful, useful video, great content, really brings understanding of the important concepts of program engineering. Thank you very much for producing high-quality content.

  • @MrSerozka
    @MrSerozka2 жыл бұрын

    Awesome topic, keep it up!)

  • @kamurashev
    @kamurashev2 жыл бұрын

    👍 Great! Looking forward for the next one!

  • @tomazbracic6568
    @tomazbracic65682 жыл бұрын

    Great explanation!!

  • @diegoromero-lovo5112
    @diegoromero-lovo51122 жыл бұрын

    Would be great a video of Async, Futures and Poll!

  • @lee45283
    @lee452832 жыл бұрын

    Keep um coming!!

  • @isurusathsara-tu6ui
    @isurusathsara-tu6ui Жыл бұрын

    very straightforward ✌

  • @wentaochi7462
    @wentaochi74622 жыл бұрын

    love this series.

  • @willkoger6475
    @willkoger64752 жыл бұрын

    Great Video, this is exactly what I was looking for. Your content is the best I've seen when trying to learn Rust. As a byproduct I think its making me a better programmer as well learning these concepts that I didn't "need" to learn in python or JavaScript.

  • @ghziad94
    @ghziad942 жыл бұрын

    Awesome, can't wait for the next in the series!

  • @ringo.gg.
    @ringo.gg.2 жыл бұрын

    Nice video, thanks! Would be awesome to see more concurrent & async rust in your channel :D

  • @dimitrobest5293
    @dimitrobest52932 жыл бұрын

    good lesson, static memory was new to me. waiting for more about this. (watching from Ukraine)

  • @ninjarogue
    @ninjarogue2 жыл бұрын

    Awesome great explanation

  • @sillytechy
    @sillytechy Жыл бұрын

    Awesome series

  • @0nyr
    @0nyr2 жыл бұрын

    This is truly a brilliant series 🌟

  • @theopantamis9184
    @theopantamis91842 жыл бұрын

    Really helpful, thanks !

  • @jcbritobr
    @jcbritobr2 жыл бұрын

    Very nice stuff.

  • @gareth2021
    @gareth20212 жыл бұрын

    simple and informative thanks

  • @diegomaza2674
    @diegomaza26742 жыл бұрын

    Excelente video, muchas gracias 👌

  • @joaojoca100
    @joaojoca1002 жыл бұрын

    Good video! Thank you for the information! Perhaps more diagrams and pictures would help the understanding and visualization of the concepts in general :) I like Rust bc since I've been studying more in-depth fundamental concepts of CS the things and vocabulary used in rust lang started making more sense: -> The concept of why ownership was implemented makes more sense once one understands memory attacks (buffer overflow, format strings, data leaks....); -> The idea of lifetimes annotation is something often used when the compiler parses the code before compiling it, so it helps a lot the optimization as the code is transformed to lower levels of the architecture; -> Reference Counting is another idea already present in CS literacy before its use in Rust, in distributed systems, when facing naming resolutions, reference counting was a solution to keep track of objects references in the used network. Rust borrowed this solution to memory management security and fast process execution again; Many other advanced ideas were used when the rust started being developed! It's indeed a great language to learn!

  • @scheimong
    @scheimong2 жыл бұрын

    This series is going to be excellent, I can feel it. It's going to benefit developers outside of the Rust community too, which is even better. That being said I do have a very strong objection to the way educators like to graphically represent memory. In my opinion, the classical "stack grows down, heap grows up" diagram is unnecessarily confusing and could potentially be very misleading, so it should no longer be used. First of all, in any modern OS memory allocation is delegated several layers, each with their own management strategies and optimisations, so the diagram is not a physically accurate representation of the actual memory layout (e.g. there's no reason why the stack can't grow "upwards" instead of "downwards", whatever that means; nor is there a reason why stack, heap, and static regions are in that particular order). My past self as a CS student used to struggle to remember where each category of memory belongs and how they grow and shrink, while in fact it was a complete waste of time because it's just an arbitrarily-chosen representation. That being said it would be a useful teaching tool if it abstracts away complexity and aids in understanding. The problem is that it really doesn't. Unless you are a developer of extremely low-level memory management software, all you need is to see stack, heap, and static as three distinct and separate regions. The stack is well, a stack; a heap is basically a do-whatever-you-want sandbox; and static is determined at compile time. Where exactly they each are and what their physical layouts are is frankly none of your business. And I mean that in a good way, because the allocator has taken care of it for you. So why is it that we continue to draw our memory diagrams like that? Maybe because it highlights how memory is finite? But duh, everyone and their dog knows that; plus most programs are never going to run into "out of memory physically" situations these days. So what I propose is simple - draw these memory regions as separate things. The stack - sure it's a stack alright, we can keep the rectangular box. But don't tack on the heap after it - they are not actually "connected" anyway, and drawing them as such serves no purpose but to confuse. So just draw the heap separately, as a circle or something. Then draw the static region as a circle (or whatever shape you prefer) and we're done. Tells the same story but with much less confusion.

  • @letsgetrusty

    @letsgetrusty

    2 жыл бұрын

    Thank you for the feedback Chameleon. I will take this into account!

  • @mlenox

    @mlenox

    2 жыл бұрын

    It is important to know that the stack does have a direction. Call it what you will, up, down, etc. Stacks are always (AFAIK) last-in-first-out buffers.

  • @kostiantynsalnykov995
    @kostiantynsalnykov9952 жыл бұрын

    Thanks for the videos ☺️

  • @saaddahmani1870
    @saaddahmani18702 жыл бұрын

    Great ... waiting what next .. thanks.

  • @proudmoroccan8164
    @proudmoroccan81642 жыл бұрын

    Good content, thank you.

  • @geoproces
    @geoproces3 ай бұрын

    Super !!Thx Bogdan

  • @jorgeyarasca9871
    @jorgeyarasca9871 Жыл бұрын

    Great video, ty

  • @zuggrr
    @zuggrr2 жыл бұрын

    Thank you so much !

  • @user-bj6lk4wh7y
    @user-bj6lk4wh7y2 жыл бұрын

    Thanks for the good video!

  • @ashtangacenterodessa
    @ashtangacenterodessa2 жыл бұрын

    Great video, thank you! @Ivan Bulyk

  • @davidsulaberidze5978
    @davidsulaberidze59782 жыл бұрын

    Thank you very much

  • @costinEEST
    @costinEEST10 ай бұрын

    Thanks for the explanation. Is the table from minute 04:07 available online? :D

  • @alanaxotla9112
    @alanaxotla91122 жыл бұрын

    Just excellent!

  • @corpsecoder5216
    @corpsecoder52162 жыл бұрын

    Great video

  • @adilmabrouk6098
    @adilmabrouk60985 ай бұрын

    Thanks a lot

  • @pictureus
    @pictureus2 жыл бұрын

    Nice :) We like

  • @dungam9402
    @dungam9402 Жыл бұрын

    thanks

  • @Jonathansclone
    @Jonathansclone2 жыл бұрын

    Good stuff good stuff

  • @santiago4773
    @santiago47732 жыл бұрын

    You're the best!

  • @PHANTOMWORLD
    @PHANTOMWORLD11 ай бұрын

    This video was fucking amazing

  • @umut8244
    @umut82442 жыл бұрын

    Thanks.

  • @davidross5525
    @davidross55252 жыл бұрын

    Great video thank you. Quick question, using your example of getting a name from the user. You said we don't know how long their name is, so the amount of memory needed to allocate to the variable is unknown at compile time => we have to use heap memory for this. But memory on the stack also has dynamic size, so why can't we just allocate some stack memory instead?

  • @gvcallen

    @gvcallen

    2 жыл бұрын

    You technically can (although I'm not sure if you can in Rust) but it's not considered the best practice. What you're talking about is "Variable Lenght Arrays" or just in general dynamically-sized stack allocation. In C, you can use the function "alloca" to do this. There are a few reasons it is recommended against e.g. it creates a higher chance of stack overflow errors since you could allocate extremely large buffers. This in turn means you should probably have an upper limit on the size of the buffer, in which case it often makes more sense to just allocate that upper limit on the stack statically in the first place. Hope that helps!

  • @NoahNobody
    @NoahNobody2 жыл бұрын

    Interesting. I guess heap memory allocation is more for more specialised use cases.

  • @lew9068
    @lew90682 ай бұрын

    I’m still learning so found this a little difficult to understand but I do have a question. When you mentioned the heap not storing a fixed string size due to the program not knowing the size of the user input… Does this mean even with a restriction in place to prevent the users string input size, it will still be stored on the heap? I hope this makes sense. Thanks

  • @inx1819
    @inx18192 жыл бұрын

    wow your camera is higher quality than my sight bro

  • @arubaga
    @arubaga2 жыл бұрын

    Is Zig a better companion language to Rust compared to C? Zig’s arena allocator seems to be well matched to a foreign function call from Rust.

  • @articatsvao7317
    @articatsvao7317 Жыл бұрын

    Cool, but isn't the Heap itself cleared at the end when the program exits (thanks to ownership)? Or did you mean it?

  • @michaelschnell5633
    @michaelschnell5633 Жыл бұрын

    Hmmm,... As an embedded and system Programmer - rather new to Rust - lots of "errors" popped up to me in this video. I do appreciate that these simplification makes a lot of sense when its gets to highlighting the memory management paradigm of Rust vs the "traditional" ways of C++ or Java. But I suppose Rust can be used without a heap for small embedded projects, as I often do with C++ . Or does that not make sense as Rust guarantees that the heap is safe ?

  • @dwaynebrock8979
    @dwaynebrock8979 Жыл бұрын

    :ᚨᛏᛟᛗᛁᚲ:ᛒᚨᛞᚷᛖᚱ: awesome video introducing memory management.

  • @mauriciomatias6261
    @mauriciomatias62612 жыл бұрын

    Nice

  • @yiyoascen
    @yiyoascen2 жыл бұрын

    How does one get your first rust job?

  • @filipemecenas
    @filipemecenas Жыл бұрын

    ⚡⚡⚡⚡

  • @shushens
    @shushens2 жыл бұрын

    I am wondering.. What does "Coming from a web developer background" mean? How can a web developer not know how memory works? Because he doesn't need any?

  • @letsgetrusty

    @letsgetrusty

    2 жыл бұрын

    More specifically a JavaScript background. JS is garbage collected so you don't need to know the details of memory management.

  • @codychan4992
    @codychan49922 жыл бұрын

    Hi there, would you like to make new topics such as introducing some useful crates for writing some projects, or how to read Rust projects from the real world project level. Since I watched all your Rust videos and videos from other channels, I finished the Rust By Examples, and Rustlings (I didn't read the Rust Book and Cargo Book, I know, I know, I should read them). After I read some real project source code I learned a lot in Cargo.toml such as features, and so many useful crates used in the projects.

  • @letsgetrusty

    @letsgetrusty

    2 жыл бұрын

    Thanks for the suggestion. I'll add that idea to the list.

  • @CorpoWolf
    @CorpoWolf4 ай бұрын

    I just call Volatile memory -> “Memory”; Then I call Persistent Memory -> “Storage”; I think it simplifies it.

  • @PrivateUsername
    @PrivateUsername2 ай бұрын

    It is absolutely terrifying that "programmers" don't know about stack and heap as part of CS101.

  • @emjaqua
    @emjaqua Жыл бұрын

    Blows my mind that modern programmers don't know mem management. I blame Java

  • @Thisguyrocks518
    @Thisguyrocks518 Жыл бұрын

    Dude

  • @AbhinandanMishra1
    @AbhinandanMishra12 жыл бұрын

    I'm a competitive programmer and I have to solve some coding questions and that's why I am learning rust but it's very hard for me to get inputs. Like integer Input, create 2d array and take input. How to make graph and all. Kindly help me for programming in rust rather than development only. Please make a good Input video in rust. Thanks

  • @NotGovernor
    @NotGovernor6 күн бұрын

    I need to you experience watching your video on a decent size modern quality HDR monitor. Stare into the sun of your white background for 10 minutes. Please... Stop abusing us. :D #DarkMode

Келесі