Garbage Collection (Mark & Sweep) - Computerphile

235,825
0
Published 2023-01-20
How does memory management work? In C you had to manage things yourself, but modern languages take care of a lot of it for you - Laurence Tratt of Kings College London explains.

More about Laurie: bit.ly/C_LaurenceTratt

Laurence recommends the book 'The Garbage Collection
Handbook: The Art of Automatic Memory Management' (2nd ed.) for those
interested in exploring this subject in more detail.

www.facebook.com/computerphile
twitter.com/computer_phile

This video was filmed and edited by Sean Riley.

Computer Science at the University of Nottingham: bit.ly/nottscomputer

Computerphile is a sister project to Brady Haran's Numberphile. More at www.bradyharan.com/

All Comments (21)
  • @pfefferle74
    As a C# programmer you often don't spend much time thinking about garbage collection because it just works silently in the background - until you start interfacing with native unmanaged code, and then you go though a hard school of troubleshooting, having to learn how the runtime actually manages your objects in memory and why it constantly keeps moving them around.
  • @exaaltare1170
    This Gent has ability of explaining complex topics, in extremely simplified way, which is phenomenal. Please, 🙏 do more videos with him, diving in all possible topics.Respect
  • @njdarda
    i love the clarity of Laurence's examples and explanations
  • @circuit10
    You didn't mention what people normally say is the main disadvantage of garbage collection which is that it pauses the program every now and then (but there are some clever ones that can run on a different thread)
  • @marksilverman
    Most modern computers maintain a virtual address space for each process. That means that pointers aren't really a direct reference to a "physical part of the chip". Inside the CPU, the address translation hardware converts the virtual address to a physical one. Pages of memory are swapped in and out as needed, so a process can use more memory than is physically present on the machine. That makes programming vastly easier in some ways and vastly more difficult when things go wrong.
  • @scbtripwire
    15:56 Note: Turning off and on again is the time tested workaround for memory leaks, but definitely not a fix in itself, that's up to the developer. (I know this is what he meant, I'm just clarifying for others who might not know better.)
  • Love that laptop, I've got one as well (It's called a Framework and is designed to be customizable and repairable)
  • @sanderbos4243
    Laurence is probably my favorite person on the channel, he's able to simplify and explain things so incredibly well with a lot of humor
  • Really good account! Underneath there’s another story that’s also interesting - how should the operating system allocate memory - this was a subject of debate in the early days of OS design when memory was much more limited. Donald Knuth analysed three options: first fit (take the first free chunk that’s greater than the size needed), best fit (take the chunk that’s nearest in size to the desired amount) or a binary chop method called the buddy system. Somewhat non-intuitively the first fit works better than best fit for long term stability!
  • @MladenMijatov
    So nice seeing framework laptop in the wild. Good job man! Excellent choice.
  • @jvcmarc
    this was really fun, i always wondered how garbage collectors work but never searched for it

    could you guys maybe do a video on different memory management strategies? like Rust's borrow checker?
  • @paxrsi
    There are also some downsides to the garbage collection. In many cases, esp. smaller programs the user will not notice them. I am maintaining an enterprise application written in Java which needs to run on a machine with 1 TB of RAM (yes, terabyte) and there I did experience some problems. For example, the GC did sometimes run for more than an hour (yes, more than 60 minutes) and all connections to the outside world including the database ran into timeouts, because of the "stop the world" nature of the GC. That's the reason why the GC has all these fuzzy parameters to get control of those situations but as an user, you are just buying an application and you want to use it and not get an expert in Java GC to run it. It gets even more complicated with all these different JVMs available. The IBM JVM runs different to the Oracle JVM and the GCs have different parameters. So if someone is able to solve this in Oracle JVM with some cool parameter settings you may still struggle with the problem because these parameters simply do not exist in IBM JVM. Someone might argue to choose hard- and software from different vendors than IBM, but then Java is not write once run everywhere. In my opinion the GC is a tool to make the life of the programmer easier but in some circumstances the price is it will make the life of the user harder. However the user is the last one who should deal with memory management of the application.
    There are also other problems e.g. you cannot take advantage of CPU memory access optimization like prefetch.
  • @navalenigma
    32768 is 32k, 32678 isn't. I'm a right pedantic arse for pointing that out on this excellent video. A typo under pressure of typing on a live video. Oh I feel grubby pointing this out. I'll go write some code using GOTO as punishment.
  • I literally love your channel , the way you create videos, the instructors and the topics you make video about are just awesome . thanks for your serve to the community
  • @superjimnz
    Never is a strong word; there are many programs where you do know exactly what you'll need and never perform dynamic allocation, or at least only on the stack... no heap allocation. This is especially true in embedded programming.
  • I immediately noticed the framework laptop. Nice to see other people using them, especially in more popular videos like this one!
  • @Luredreier
    Sounds like a excellent time to address Rusts memory management.
  • @tan.nicolas
    Computerphile is such a gem! cheers from Chile and thanks for all the top content you share!
  • @BiggFanDDD
    Great video! I've always wondered how garbage collection worked. Thanks!
  • @Waschlack
    Really interesting topic and awesome explanation!
    I personally only knew about the ref counter and didn't imagine there to be such an interesting and better option out there