How Python Manages Memory (Overview)

Ever wonder how Python handles your data behind the scenes? How are your variables stored in memory? When do they get deleted?

In this course, we’re going to do a deep dive into the internals of Python to understand how it handles memory management.

By the end of this course, you’ll:

  • Learn more about low-level computing, specifically as relates to memory
  • Understand how Python abstracts lower-level operations
  • Learn about Python’s internal memory management algorithms

Course Slides (.pdf)

2.2 MB

00:00 Hi! I’m Austin Cepalia with, and welcome to Memory Management in Python. In this course, we’re going to take a journey deep into the inner workings of CPython.

00:12 CPython is the standard implementation of Python. It’s the program that takes your Python code and actually runs it, among other things. If you’ve coded in Python, then you’ve probably used it before.

00:25 As you might know, the programs you write deal with a lot of data. You store it in variables, you perform various operations on it, and you pass it as arguments to functions.

00:37 This data is all stored in computer memory, starting with RAM. Python is great because as the developer you don’t have to worry about how your data is stored in memory.

00:48 CPython handles all of that for us, and it does a lot that you might not even think about. For example, have you ever thought about where variables go once they’re no longer accessible in your program?

01:00 Those are the kinds of questions that this course will answer. In this course, you’ll learn about low-level computing, such as memory management and factors that can affect memory; CPython and how it handles reading and writing from memory; and CPython’s internal memory management algorithms, or how CPython allocates memory depending on what’s needed.

01:24 Understanding Python’s internals will give you better insight into some of Python’s behaviors. It might also give you a new appreciation for just how much Python does for you behind the scenes so you can focus on your code. Along the way, you’ll learn about a classic issue in computer science called a race condition and what kind of effect that can have on memory. Before we start, it should be noted that this course is built on Python 3.7 and it’s possible that things may change slightly with future versions.

01:55 Most of the core ideas, however, will not. Let’s start by learning about the basics of memory management.

Become a Member to join the conversation.