Python Memory Deep-Dive: A Performance Optimization Guide Course

Course Summary

Python memory management is often a black box to most developers. You probably know that Python uses reference counting, but how can you write code most efficiently to work with it? Did you know it also uses garbage collection? Do you know when that kicks it and its performance implications? With this course, you'll learn all of these concepts and more. You will explore them with concrete code examples, not just theories. And you will learn to optimize your code to both use less memory and to run faster.

What students are saying

I thought it a well done, interesting and enjoyable program that provides very usable tips and tricks for people to be better prepared to start developing applications in Python.
-- Jeff M.

Source code and course GitHub repository

github.com/talkpython/python-memory-management-course

What's this course about and how is it different?

This very unique course will teach not just how Python memory management works and how to create code that functions well within that world, it will provide many concrete techniques, tools, design patterns, and more to make your programs more memory efficient and computationally faster to boot.

If Python memory (allocations, clean up, and so on) has always felt like a weird black box that you have had to take for granted, join this course and open that box. There are many beautiful and interesting aspects of Python's runtime behavior making your code run. You should understand what's happening on your behalf.

What topics are covered

In this course, you will:

  • Learn how Python variables and data structures actually look in the CPython layer
  • See how the small object allocator treats most objects differently than your intuition
  • Understand Python's memory allocation primitives: blocks, pools, and arenas
  • Locate the elements on C code responsible for Python memory behavior
  • See reference counting in action with live code explorations
  • Discover why reference counting alone is not enough for memory cleanup
  • Work with Python's GC and see when it's needed, and when it's not
  • Compare different data structures to get a sense of their relative size
  • Use multiple clever but simple techniques to massively reduce memory during function calls
  • Lighten up your classes with properties
  • Leverage multiple memory profilers to investigate memory usage line by line and over time
  • And lots more

View the full course outline.

Who is this course for?

This course is for anyone who wants to understand how Python memory is managed and make their code more efficient and faster. If you're tired of Python memory being a black box hiding its behavior, turn on the light with this course.

The student requirements are quite light for this course. You'll need Basic Python language knowledge:

  • Classes
  • Functions
  • Properties
  • Variables
  • Loops
  • Iteration

Note: All software used during this course, including editors, Python language, etc., are 100% free and open source. You won't have to buy anything to take the course.

Concepts backed by concise visuals

While exploring a topic interactively with demos and live code is very engaging, it can mean losing the forest for the trees. That's why when we hit a new topic, we stop and discuss it with concise and clear visuals.

Here's an example of understanding the generational garbage collector and how objects migrate through the generations.

Example: Concepts backed by concise visuals

Get hands-on for almost every chapter

Things often get too abstract when talking about memory management and related concepts. In this course, we'll develop a lot of code demonstrating most of the features and functionality we discuss.

You'll have access to all the source code at github.com/talkpython/python-memory-management-course where you can explore and tweak things to see how they run and change behavior.

This course is delivered in very high resolution

Example of 1440p high res video

This course is delivered in 1440p (4x the pixels as 720p). When you're watching the videos for this course, it will feel like you're sitting next to the instructor looking at their screen.

Every little detail, menu item, and icon is clear and crisp. Watch the introductory video at the top of this page to see an example.

Follow along with subtitles and transcripts

Each course comes with subtitles and full transcripts. The transcripts are available as a separate searchable page for each lecture. They also are available in course-wide search results to help you find just the right lecture.

Each course has subtitles available in the video player.

Who am I? Why should you take my course?

Who is Michael Kennedy?

My name is Michael, nice to meet you. ;) There are a couple of reasons I'm especially qualified to teach you Python.

 1. I'm the host of the #1 podcast on Python called Talk Python To Me. Over there, I've interviewed many of the leaders and creators in the Python community. I bring that perspective to all the courses I create.

 2. I've been a professional software trainer for over 10 years. I have taught literally thousands of professional developers in hundreds of courses throughout the world.

 3. Students have loved my courses. Here are just a few quotes from past students of mine.

"Michael is super knowledgeable, loves his craft, and he conveys it all well. I would highly recommend his training class anytime." - Robert F.
"Michael is simply an outstanding instructor." - Kevin R.
"Michael was an encyclopedia for the deep inner workings of Python. Very impressive." - Neal L.

Free office hours keep you from getting stuck

One of the challenges of self-paced online learning is getting stuck. It can be hard to get the help you need to get unstuck.

That's why at Talk Python Training, we offer live, online office hours. You drop in and join a group of fellow students to chat about your course progress and see solutions via screen sharing.

Just visit your account page to see the upcoming office hour schedule.

The time to act is now

No matter what you're building with Python, you should understand how it works when it runs your code. This course will give you that next-level insight with concrete examples and references back to the CPython source code.

Dive into Python memory management and level up your understanding. Join today! You've got nothing to lose. Every one of our courses comes with a 2-week money-back guarantee.

Course Outline: Chapters and Lectures

Welcome to the course
6:52
Welcome
1:04
Why care about memory?
0:57
Smaller and faster
0:46
Topics covered
2:57
Student expectations
0:42
Meet your instructor
0:26
Setup and tools
5:55
You'll need Python 3
2:51
Our code editor
2:05
Git the source
0:59
Python variables and memory
40:00
Python roughly equals CPython
1:40
Let's talk pointers
2:57
Passing values in C
1:33
Does Python have pointers?
3:33
Pass by value
1:19
Red pill / blue pill
4:05
CPython long source
1:57
The id() function
1:29
Loading the sample code
3:15
The size of objects
7:47
The *real* size of objects
3:46
Concept: Flyweight design pattern
1:07
Flyweight numbers in CPython
4:47
CPython source book
0:45
Allocating memory in Python
19:53
Allocation introduction
1:02
Allocation in action
4:00
Big objects may actually be many small ones
1:52
Small object allocation introduction
2:13
Allocation blocks
2:17
Allocation pools
1:05
Pools in the CPython source
1:19
Allocation arenas
0:54
Allocator stats
5:11
Recovering memory in Python
42:27
Ref counting in source
3:00
Reference counting
9:03
When reference counting breaks
3:36
GC in action
5:21
GC without containers?
3:56
Pythons generational garbage collector
2:42
When does the GC run?
4:43
Do you need the GC?
6:26
Ref-counting and the GIL
3:40
Efficient data structures
47:29
Data struct chapter intro
1:39
What we arent covering
0:53
Disabling the gc revisited
1:12
Data with cycles
6:06
Cycle busting part 1
2:17
Cycle busting with friend map
6:09
checking friends in the friend map
7:38
Different container types
1:28
Container sizes, starter data
4:47
Container sizes, lists
1:55
Demo: Container sizes, classes
3:47
Container sizes, arrays
3:08
Container sizes, Pandas
2:29
Container sizes, NumPy
2:01
Monitoring memory usages for current process
2:00
Memory and functions
41:20
Function and memory
0:38
Functions clinging to memory
2:51
Implementing the pipeline functions
2:54
Tracking memory usage
4:09
Dropping intermediate data
5:19
Concept: Dropping intermediate data
1:32
Converting the pipeline to generators
10:31
Concept: Generators
1:48
Useful closures
5:57
Counting with closures
4:30
Concept: Closure state
1:11
Memory and classes
36:30
Classes and memory introduction
0:49
Plain ol' fields
6:50
Is it a crowd?
1:46
Testing crowd sizes
2:05
Delayed fields with properties
4:04
Concept: Properties, a memory-oriented perspective
1:02
Where do classes store memory?
7:53
Concept: Class dictionaries
1:28
People with slots
5:27
Concept: Slots
1:31
Slots are faster, not just smaller
3:35
Investigating memory usage
17:11
Profiling introduction
1:22
Profiling in PyCharm
2:56
A memory profiler
1:33
Line level memory profiling
2:58
Concept: Line by line memory with memory_profiler
0:34
Graphing memory usage over time
3:05
Concept: Graphing memory usage over time
0:29
A data science-focused profiler
1:27
Profiling with Fil
2:47
Course conclusion and review
12:56
You crossed the finish line
0:54
Take the red pill (AKA pointers)
1:06
Allocating memory
1:31
Reference counting
1:17
Garbage collection
1:19
Container types
0:45
Memory and functions
1:45
Classes
2:40
Profiling
1:13
Bye and thanks
0:26
Buy for $49 + tax Bundle and save 85% Team Gift

Questions? Send us an email: contact@talkpython.fm

Talk Python's Mastodon Michael Kennedy's Mastodon