Every Developer Should Know This
Key Takeaways
The video discusses memory models in software, focusing on the stack and the heap, and explores their advantages and limitations, as well as memory management in various programming languages such as C++, Rust, and Python.
Full Transcript
as a developer it's important that you know a little bit about how computers work on a lower level today I'm going to use these ancient computer components that I recently acquired to illustrate the two most common ways that memory is organized in software as you can see in the past computers were made of wood w and to prove that I'm not making this up here's a real painting of BL Pascal with his wooden computer also I didn't know he was a Star Trek fan before I dive into the two memory models let's first talk about why we need to organize memory most of the tasks of the CPU are about changing memory in some way while the CPU task include reading from and writing to memory it's the operating system and the program that primarily handles reserving or allocating memory that the program requires and deallocating or freeing memory space that's no longer needed in order to do this effectively you need a way to organize memory so that your program runs efficiently but also that you as a developer don't have to think too much about such a low-level task the most common memory organization models are the stack and the Heap they each handle memory differently and they each have their advantages and limitations as you're going to see in this video programming languages have adopted different mechanisms for dealing with these limitations python in particular has an interesting way of doing this more about that later first let's see how these memory models actually work the stack is a basic model of memory that operates the name already says it as a stack you allocate memory by pushing something onto the stack and when you no longer need the memory you pop it from the stack again so assume this is a stack of a program that's currently running in that program let's create a variable and assign a value to it in order to do that you need to allocate memory first now let's say you call a function that also requires memory because you're going to need to store some metadata about the function such as the function name but also the arguments that are passed to the function and in essence that all together all that metadata creates the scope for the function now let's say that in the function you declare another variable the function does some work and then it completes now you can pop the allocated memory again from the stack and continue your program afterwards so that's the variable that we no longer need and these are the function metadata the function scope information that we no longer need either so this is great the stack is a really simple model and it seems to do a perfect job right well that's a small problem when you allocate memory on the stack you need to specify how much that way you can reserve that memory space and it will still be available even after something else is put on top of it but that's not always possible for example let's say you want to store the name of a user that the user provides in a text field you don't know how many characters your user is going to type so how much space do you actually need maybe these two blocks they're not enough now because the stack only has push and pop operations you can't change the size of things on the stack or move things around so you can't do this for example that's not allowed this is a problem you could just reserve a ton of space let's add even more right just in case the user parents decided to name the child after the chemical composition of Tian well first no one does that but also this wouldn't be very efficient you would require a lot of memory and you'd still have no guarantee that you wouldn't run into problems when a value needs more space than you thought or you could create a larger copy every time you need more memory so oh oops actually I need extra character so let's Reserve more memory on this stack and then we move everything to there this is also very inefficient and if you continue doing this then at some point you're going to get a stack Overflow to really solve this problem we're going to need a different memory model and that's called the Heap before diving into the Heap if you want to become better at making design decisions in general not just related to memory memory check out my free software design guide iron. codesign guide I've recently completely reworked this guide and added a lot more content to it and also spiced it up a bit visually it contains the seven most important things for you to consider when you're writing a new piece of software to get your free copy just click on the link below enter your email address and you get it in your inbox right away what is the Heap the Heap is a place where you can freely allocate memory let's create some space here where we can Define a he now of course there is still going to be some sort of limit because your computer is not going to have an infinite amount of memory but let's say we have a pretty large area here where we can allocate any memory that we want since allocating memory on the Heap happens separately for each thing that you need to store you don't have the space problem that you see on the stack so let's say we allocate some memory here for I don't know an integer and here's another place where we're going to allocate some memory for a string and then here we're going to allocate some memory for an even more complex object we can allocate some memory here and so on and so on and since you can basically place these memory blocks anywhere you want this gives you a lot of freedom if you don't have enough space you just allocate more memory and move things around as you please that's not the limitation that the stack has that you can only put things on top of each other with the stack it's easy to refer to memory because you know exactly where where's what and also it's really easy to deallocate memory because as soon as the stack frame is popped everything is cleaned up for the Heap this doesn't work because memory allocation is not linked to any scope like with the stack so let's first solve how you can refer to this memory on the Heap and this is done with a concept called a pointer I'm going to need a few extra things oops a pointer is nothing more than a variable that contains a memory address so let's say next to the Heap we also have the stack again which I'm going to put right here or actually let me put that here so you can more clearly see that so what we can do then is create a pointer and there's a variable that contains the memory address and let's say we declare that variable on the stack and then we let it point to let's see this one there we go very nice so since this is a variable the thing that I have right here you can store this thing on the stack furthermore you can also have multiple pointers that all point to the same piece of memory this happens for example if you pass an object which in most objectoriented languages is stored on the Heap and you pass that as an argument to a function or a method then a pointer to that object or reference to that object is going to be pass last along and that's going to be available in multiple places so let's say we now call a function and that adds this particular function scope and now that function gets as an argument a copy of this pointer right here another rope and we put this on top of the stack I hope this doesn't crash down I think I Now understand why we switched to computers that were not made of wood anymore and then this pointer will also point to this address to this piece of memory on the Heap there we go you can even store the pointer itself on the Heap or make it part of another object and then have a pointer to that that's stored on the stack or the Heap the world is your pointer you can access the memory that the pointer points to by De referencing it programming languages that support Heap allocated memory typically will have built-in support for dereferencing a pointer in their Syntax for example C has the arrow syntax and languages like C and Java simply use the dot syntax and automatically dreference the pointer for you python handles this differently but that comes at a price I'll talk about that in a minute so what happens if this stack frame here goes out of scope and is popped the pointer is simply removed but the memory on the stock is still allocated this shows why the Heap Can Be an Effective memory model especially for large blocks of data with the stack you would have to copy this data all the time but with the Heap you can just create and delete pointers on the stack without touching the actual data which is much more efficient but what happens if this pointer now also goes out of scope there's now no longer a variable that contains the memory address of this thing on the Heap in other words you no longer have a way to refer to this memory that doesn't have to be a problem if you no longer need the data in your program but it does need to be cleaned up otherwise your program is going to keep allocating more and more things on the Heap without cleaning it up until memory runs out and even before that your program is going to fill up all the memory so other programs can't use that memory anymore which will slow down your computer there are different ways to deal with this some languages like Java python C have an automatic garbage collector that every once in a while looks through memory and checks what memory can be freed up by looking at whether something is still referring to that memory in Old Computers wooden robots were used to do this job deallocate deallocate the problem is that even though this is the most convenient solution for developers it also removes some control because you don't know when this little robot here is going going to come and clean the house yes we don't like that an alternative is that you don't do garbage collection but you let the developer handle this explicitly when that's needed this is what C and C++ does problem is that developers then have to make sure to deallocate memory they no longer need however developers are forgetful for example recently I forgot my own age I literally had to compute it from my birth year and this will probably get worse from this point by the way about forgetting things don't forget to join my Discord Community via the link below there's ton of people there who love to talk about software and software development when memory is not deallocated and there's no way to refer to it anymore that's what we call a memory leak this is one of the most common bugs in software programming languages offer various kinds of solutions to this problem one way to solve it is to provide a programmatic layer around pointers and memory management that takes care of cleaning up the memory there are several of these smart pointer implementations in C++ that each have slightly different behavior in most cases you should use the unique pointer type but it's still something that you need to think about as a developer and if you deal with older C++ code it might have deprecated smart pointer types such as autop pointer in short this can become confusing another option is to handle memory management directly in the syntax of the programming language this is what rust does it has a built-in ownership model model to keep track of when to deallocate memory rust code that doesn't properly deal with this won't even compile this solution works really well but it does create quite a learning curve for new rust stations how does all of this work in Python well if you are a python nista memory management is really simple python doesn't want you to have to think about whether to use the stack or the Heap or having to clean up the memory none of that python is optimized for making things really simp for the developer because of that python just uses the Heap for everything including basic values like integers or floats and this is combined with an automatic garbage collector as a result in Python you don't have to think about managing memory at all this makes why am I still holding this robot this makes python really easy to get started with but it's also a significant reason for python being slow I think this is a reasonable tradeoff yes python is slow but the Simplicity is also what led to the widespread adoption of python in many domains and let's be honest here in most cases performance is not the deciding factor for picking a programming language instead most people look at EAS of use the EOS system libraries and other tooling that's available and whether the language has a cool looking icon so whenever someone tells you that python is a toy language and you should use a real language like C or rust tell them that you prefer quick development and iteration over code that may be performant but it's also much harder to write let's embrace our Collective laziness I hope you enjoyed this discussion of how memory models work we talked about the stack and the Heap I covered mechanisms for cleaning up memory manual cleanup smart pointers automatic garbage collection and rust ownership model personally I really enjoyed playing with wood today but now me and my little Ro what friend would like to hear from you what do you think of how different languages deal with memory do you have a preference for rust ownership model or smart pointers or Simply Having a garbage collection robot doing the work for you and what should I do with all this wood let me know in the comments I actually built this robot myself when I was a kid and it has quite a few interesting features for example you can open this up I I have no idea what this is for is it some sort of robot reproductive organ not quite sure also it has a really cool feature where you could like open this up like this ah you turn this and then this opens that's really cool right I was I remember I was pretty proud of that and there's a little plug here that says that we come from Mars and we come in peace I don't believe you also what I find kind of suspicious is that this is apparently a self-destruct button which doesn't make a whole lot of sense to me to put that like very easy to reach if I was a robot I would probably put this I don't know maybe here you know anyway as you can clearly see I've been fascinated by robots for a really long time so I'm just making my contribution towards the technological singularity where all humans are going to be eradicated and replaced by wooden robots I did mention Ros as an example of language does things quite differently from python if you want to learn more about that watch this video next thanks for watching and see you next time
Original Description
💡 Learn how to design great software in 7 steps: https://arjan.codes/designguide.
In this video, I’m exploring memory models in software, focusing on the stack and the heap. I'll use wooden blocks to visually explain these concepts and why memory management matters.
🎓 ArjanCodes Courses: https://www.arjancodes.com/courses/
🔖 Chapters:
0:00 Intro
0:40 Why do we need to organize memory?
1:38 What is the stack?
4:51 What is the heap?
12:12 How does all of this work in Python?
13:39 Final thoughts
15:26 Outro
#arjancodes #softwaredesign #python
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from ArjanCodes · ArjanCodes · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Full stack WEB DEVELOPMENT in 2021 - the ULTIMATE tech stack for FAST web app development
ArjanCodes
FROM PRODUCT IDEA TO SOFTWARE - turn your idea into reality in a few steps
ArjanCodes
Cohesion and Coupling: Write BETTER PYTHON CODE Part 1
ArjanCodes
Build a GLASSMORPHISM React Component - Typescript & Material-UI
ArjanCodes
Observer Pattern Tutorial: I NEVER Knew Events Were THIS Powerful 🚀
ArjanCodes
100% CODE COVERAGE - Think You're Done? Think AGAIN.☝
ArjanCodes
Two UNDERRATED Design Patterns 💡 Write BETTER PYTHON CODE Part 6
ArjanCodes
1000 Subscribers! 🚀 WHY I Started this Channel and WHAT'S NEXT
ArjanCodes
Channel Trailer ArjanCodes - March 2021
ArjanCodes
Exception Handling Tips in Python ⚠ Write Better Python Code Part 7
ArjanCodes
Monadic Error Handling in Python ⚠ Write Better Python Code Part 7B
ArjanCodes
GW BASIC Games I Wrote When I Was a Kid 🎮 Running 30 Year Old Code
ArjanCodes
Why You Should Think About SOFTWARE ARCHITECTURE in Python 💡
ArjanCodes
Uncle Bob’s SOLID Principles Made Easy 🍀 - In Python!
ArjanCodes
QUESTIONABLE Object Creation Patterns in Python 🤔
ArjanCodes
If You’re Not Using Python DATA CLASSES Yet, You Should 🚀
ArjanCodes
CODE ROAST: Yahtzee - New Python Code Refactoring Series!
ArjanCodes
7 UX Design Tips for Developers
ArjanCodes
Going All-in on Software Design in Python + an ANNOUNCEMENT 🎙
ArjanCodes
🎙 Interview with Sybren Stüvel, Developer @ Blender 3D
ArjanCodes
Do We Still Need Dataclasses? // PYDANTIC Tutorial
ArjanCodes
7 Python Mistakes That Instantly Expose Junior Developers
ArjanCodes
Answering Your Most Frequently Asked Python Questions // Q&A 07-2021
ArjanCodes
GitHub Copilot 🤖 The Future of Software Development?
ArjanCodes
More Python Code Smells: Avoid These 7 Smelly Snags
ArjanCodes
Test-Driven Development In Python // The Power of Red-Green-Refactor
ArjanCodes
5 Tips To Keep Technical Debt Under Control
ArjanCodes
Refactoring A Tower Defense Game In Python // CODE ROAST
ArjanCodes
The Factory Design Pattern is Obsolete in Python
ArjanCodes
Why the Plugin Architecture Gives You CRAZY Flexibility
ArjanCodes
Refactoring A Data Science Project Part 1 - Abstraction and Composition
ArjanCodes
Refactoring A Data Science Project Part 2 - The Information Expert
ArjanCodes
Refactoring A Data Science Project Part 3 - Configuration Cleanup
ArjanCodes
Purge These 7 Code Smells From Your Python Code
ArjanCodes
Running A Software Development YouTube Channel
ArjanCodes
Refactoring A PDF And Web Scraper Part 1 // CODE ROAST
ArjanCodes
Refactoring A PDF And Web Scraper Part 2 // CODE ROAST
ArjanCodes
How To Easily Do Asynchronous Programming With Asyncio In Python
ArjanCodes
The Software Designer Mindset
ArjanCodes
NEVER Worry About Data Science Projects Configs Again
ArjanCodes
Powerful VSCode Tips And Tricks For Python Development And Design
ArjanCodes
8 Python Coding Tips - From The Google Python Style Guide
ArjanCodes
What Is Encapsulation And Information Hiding?
ArjanCodes
8 Tips For Becoming A Senior Developer
ArjanCodes
Building A Custom Context Manager In Python: A Closer Look
ArjanCodes
GraphQL vs REST: What's The Difference And When To Use Which?
ArjanCodes
You Can Do Really Cool Things With Functions In Python
ArjanCodes
Announcing The Black VS Code Theme (Launching April 1st)
ArjanCodes
7 DevOps Best Practices For Launching A SaaS Platform
ArjanCodes
Refactoring a Rock Paper Scissors Lizard Spock Game // Code Roast Part 1
ArjanCodes
Refactoring a Rock Paper Scissors Lizard Spock Game // Part 2
ArjanCodes
Things Are Going To Change Around Here
ArjanCodes
Dependency Injection Explained In One Minute // Python Tips
ArjanCodes
How To Setup A MacBook Pro M1 For Software Development
ArjanCodes
A Simple & Effective Way To Improve Python Class Performance
ArjanCodes
How To Write Unit Tests For Existing Python Code // Part 1 of 2
ArjanCodes
How To Write Unit Tests For Existing Python Code // Part 2 of 2
ArjanCodes
Make Sure You Choose The Right Data Structure // Python Tips
ArjanCodes
5 Tips For Object-Oriented Programming Done Well - In Python
ArjanCodes
Next-Level Concurrent Programming In Python With Asyncio
ArjanCodes
More on: Systems Design Basics
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
Bloom Filters, Explained Properly
Dev.to · Daksh Gargas
Prefix Sums: The Preprocessing Trick That Makes Range Queries Instant
Medium · Programming
I Thought I Was Ready for the Interview — Then One Simple Math Question Destroyed Me
Medium · Programming
Week 2(Day 10): LeetCode Two Pointers(slow & fast): Remove Duplicates from Sorted Array (Brute…
Medium · Python
Chapters (7)
Intro
0:40
Why do we need to organize memory?
1:38
What is the stack?
4:51
What is the heap?
12:12
How does all of this work in Python?
13:39
Final thoughts
15:26
Outro
🎓
Tutor Explanation
DeepCamp AI