Latest / Talk Python To Me / #425: Memray: The endgame Python memory profiler
Transcript
- 0:00Understanding how your Python application is using memory can be tough.
- 0:03First, Python has its own layer of reused memory, arenas, pools, and blocks, to help it be more efficient.
- 0:10And many important Python packages are built in native compiled languages like C and Rust,
- 0:15oftentimes making that section of your memory usage opaque.
- 0:19But with memory, you can get way deeper insight into your memory usage.
- 0:23We have Pablo Galindo Salgado and Matt Wazinski back on the show to dive into memory.
- 0:29The sister project to their PyStack one we recently covered.
- 0:32This is Talk Python To Me, episode 425, recorded June 20th, 2023.
- 0:38Welcome to Talk Python To Me, a weekly podcast on Python.
- 0:54This is your host, Michael Kennedy.
- 0:56Follow me on Mastodon, where I'm @mkennedy,
- 0:59and follow the podcast using @talkpython, both on Bostodon.org.
- 1:03Be careful with impersonating accounts on other instances.
- 1:06There are many.
- 1:07Keep up with the show and listen to over seven years of past episodes at talkpython.fm.
- 1:13We've started streaming most of our episodes live on YouTube.
- 1:16Subscribe to our YouTube channel over at talkpython.fm/youtube to get notified about upcoming shows and be part of that episode.
- 1:24This episode is brought to you by JetBrains, who encourage you to get work done with PyCharm.
- 1:30Download your free trial of PyCharm Professional at talkpython.fm/done dash with dash PyCharm.
- 1:38And it's brought to you by InfluxDB.
- 1:39InfluxDB is the database purpose built for handling time series data at a massive scale for real-time analytics.
- 1:46Try them for free at talkpython.fm/InfluxDB.
- 1:51Hey, all.
- 1:52Before we dive into the interview, I want to take just a moment and tell you about our latest course over at Talk Python,
- 1:56MongoDB with Async Python.
- 1:59This course is a comprehensive and modernized approach to MongoDB for Python developers.
- 2:05We use Beanie, Pydantic, Async and Await, as well as FastAPI to explore how you write apps for MongoDB and even test them with Locus for load testing.
- 2:14And just today, yes, exactly today, the last of these frameworks were upgraded to use the newer, much faster Pydantic 2.0.
- 2:23I think it's a great course that you'll enjoy.
- 2:25So visit talkpython.fm/Async dash MongoDB to learn more.
- 2:29And if you have a recent full course bundle, this one's already available in your library of courses.
- 2:34Thanks for supporting this podcast by taking and recommending our courses.
- 2:39Hey, guys.
- 2:41Hey, Pablo.
- 2:42Matt.
- 2:43Welcome back to Talk Python To Me.
- 2:44It hasn't been that long since you've been here last time, has it?
- 2:47With the magic of the issue, maybe even minutes since the person listening to us listens to the previous one.
- 2:53Exactly.
- 2:53We don't know.
- 2:54We don't know when they're going to listen.
- 2:55And they don't know when we recorded it necessarily.
- 2:57It could be magic.
- 2:59It's a little bit apart.
- 3:00But we got together to talk about all these cool programs that give insight into how your app runs in Python.
- 3:07So we talked about PyStack previously about figuring out what your app is doing, if it's locked up or at any given moment, if it crashes, grab a core dump.
- 3:14And maybe we thought about combining that with memory and just talking through those.
- 3:19But they're both such great projects that, in the end, we decided, nope, they each get their own attention.
- 3:25They each get their own episode.
- 3:26So we're back together to talk about memory and memory profiling in Python.
- 3:32An incredible, incredible profiler we're going to talk about in a minute.
- 3:37Pablo, you were just talking about how you were releasing some of the new versions of Python, some of the point updates and some of the betas.
- 3:43You want to give us just a quick update on that before we jump into talking about memory?
- 3:48Yeah, absolutely.
- 3:49I mean, just to clarify also, like the ones I released myself are 3.10 and 3.11, which are the best versions of Python you will ever find.
- 3:57But the ones we are releasing right now is 3.12.
- 4:01We got the beta 3 today.
- 4:03You should absolutely test beta 3.
- 4:05Maybe they are not as exciting as 3.11, but there is a bunch of interesting things there.
- 4:10And, you know, there is the work of the FasterCPython team.
- 4:13And we have a huge change in the parser, but technically tokenizer, because we are getting f-strings even better.
- 4:21And that has a huge amount of changes everywhere, even if you don't think a lot about that.
- 4:26But having this tested is quite important, as you can think.
- 4:30So far, we really, really want everyone to test this release.
- 4:33Everyone that is listening to the live version of the podcast can go to python.org.
- 4:38A lot of the latest pre-release, that is Python 3.12 beta 3.
- 4:43And tell us what's broken.
- 4:45Hopefully it's not my fault.
- 4:46But yeah.
- 4:47No, that's excellent.
- 4:48Thanks for keeping those coming.
- 4:49Do you know how many betas are planned?
- 4:52We're on three now.
- 4:54This is going to be a bit embarrassing because I should know being a release manager, I think there is two more.
- 4:57It's a bit tricky though, because I think we released beta two a week after beta one, because we shift the schedule.
- 5:03It's a bit difficult to know, but there is a PEP that we can certainly find.
- 5:07You just search for Python releases schedule, Python 3.12.
- 5:11They will tell you exactly how many betas there are.
- 5:13I think there is two more betas.
- 5:14Then we will have one release candidate, if I recall correctly, if we do things the way I did them.
- 5:20And then the final version in October.
- 5:21I'm looking forward to it.
- 5:23And, you know, the release of 3.12, actually, it's going to have some relevance to our conversation.
- 5:28Yes, indeed.
- 5:29Yeah.
- 5:30I assume people probably listened to the last episode, but maybe just, you know, a real quick introduction to yourself.
- 5:35You know, Pablo, you go first, just so people know who you are.
- 5:37Yeah, absolutely.
- 5:38So I'm Pablo Galindo, I have many things in the Python community.
- 5:41I have been practicing to save them very fast, so I don't take a lot of time.
- 5:44So I'm a CPython card developer, Python release manager, a string council, and I work at Bloomberg and the Python infrastructure team doing a lot of cool tools.
- 5:54I think I don't, I'm not forgetting about anything, but yeah, I'm around.
- 5:58I break things so you can, I like to break tools, like with my new changes in CPython.
- 6:04That's what I do.
- 6:04Excellent.
- 6:05Sorry, Wukesh.
- 6:07And I am Matt Wisniewski.
- 6:09I am Pablo's co-worker on the Python infrastructure team at Bloomberg.
- 6:12I am the co-maintainer of Memray and PyStack.
- 6:16I'm also a moderator on Python Discord, and that is the extent of my very short list of community involvement compared to Pablo's.
- 6:24Excellent.
- 6:25Well, yeah, you both are doing really cool stuff, as we're going to see.
- 6:28Let's start this conversation off about profilers at a little bit higher level, and then we'll work our way into what is Memray and how does it work and where does it work, all those things.
- 6:38So let's just talk about what comes with Python, right?
- 6:43We have, interestingly, we have two options inside the standard library.
- 6:47We have cprofile and profile.
- 6:49Do I use cprofile to profile cpython and profile for other things, or what's going on here, guys?
- 6:55That's already...
- 6:56You use cprofile whenever you can, is the answer.
- 6:59Yes, indeed.
- 7:00There is a lot of caveats here.
- 7:01I already see, like, two podcast episodes yet with this question, so let's keep this short.
- 7:07The cprofile and profile are the profilers that come with the standard library.
- 7:11I'm looking at the...
- 7:13I mean, probably you are not looking at this in the podcast version, but here we are looking at the Python documentation and the profile, cprofile version.
- 7:21And there is this lovely sentence that says cprofile and profile provide deterministic profiling of Python programs.
- 7:27And I'm going to say, wow, that's an interesting take on them.
- 7:30I wouldn't say deterministic, although I think the modern terminology is tracing.
- 7:35And this is quite important because, like, it's not really deterministic in the sense that if you executed the profile 10 times, you're going to get the same results.
- 7:44You probably are not because programs in general are not deterministic due to several things.
- 7:48We can go into detail.
- 7:49Why not?
- 7:50Even what else is running on your computer, right?
- 7:52Like...
- 7:52Exactly.
- 7:52What this is referring to is actually a very important thing for everyone to understand because everyone gets this wrong.
- 7:58And, like, you know, there is so many discussions around this fact and comparing apples to oranges that is just very annoying.
- 8:04So what this is referring to is a cprofile is what is called a tracing profiler.
- 8:08The other kind of profiler that we will talk about is a sampling profiler, also called a statistical profiler.
- 8:15So this one, so a sampling profiler basically is a profiler that every time, assuming that it's a performance one, because cprofile checks time.
- 8:23So how much time does your functions take or why your code is slow, in other words.
- 8:28So this profiler basically checks every single Python call that happens, all of them.
- 8:32So it sees all of the calls that are made.
- 8:34And every time a function returns, it goes and sees them.
- 8:37Unfortunately, this has a disadvantage that is very slow.
- 8:41So running the profiler will make your code slower.
- 8:44So your code takes one hour to run.
- 8:46It's not unsurprising that running it under cprofile makes it two hours.
- 8:50And then you will say, well, how can I profile anything?
- 8:53Well, because it's going to report a percentage.
- 8:55So hopefully it's the same percentage, right?
- 8:57Not just that it makes it slow.
- 8:59The other problem with it is that it makes it slow by different amounts, depending on the type of code that's running.
- 9:05If what's executing is IO and you're waiting for a network service or something like that to respond, cprofile isn't making that any slower.
- 9:12It takes the amount of time that it takes and it's able to accurately report when that call finishes.
- 9:16But if what's running is CPU bound code, where you're doing a bunch of enters and exits into Python functions and executing a bunch of Python bytecode, the tracing profiler is tracing all of that and it's got overhead added to that.
- 9:28So the fact that it isn't adding overhead to network calls or to disk IO or things like that, but is adding overhead to CPU bound stuff means that it can be tough to get a full picture of where your program is spending its time.
- 9:40It's very good at telling you where it's spending its CPU, but not as good at telling you where it's spending its time.
- 9:46Right, right.
- 9:46Because it has this, it's one of these Heisenberg quantum mechanics sort of things.
- 9:51By observing it, you make a change.
- 9:53And it's really, Matt, that's a great point.
- 9:55I think also you could throw into there specifically in the Python world that it's really common that we're working with computation involving C or Rust, right?
- 10:06And so if I call a function where the algorithm is written in Python, every little step of that through the loops of that algorithm are being modified and slowed down by the profiler.
- 10:16Whereas once it hits a C or a Rust layer, it just says, well, we're just going to wait till that comes back.
- 10:22And so it doesn't interfere, right?
- 10:23And so it, even across things like where you're using something like say Pandas or NumPy, potentially, it could misrepresent how much time you're spending there.
- 10:33On the other hand, it was not going to interfere with the RAS or C, but it's also not going to report inside that.
- 10:38So you are going to see a very high level view of what's going on.
- 10:43So it's going to tell you algorithm running, but like you're not going to see what's going on, right?
- 10:48Well, the advantage here is that it comes with the standard library, which is, and it's a very simple profiler.
- 10:52So you know what you're doing, which is maybe a lot to ask.
- 10:56Because, you know, it's not, no, I mean it like in the sense that it's not that you are a professional.
- 11:00It's that sometimes it's very hard to know when it's a good choice of a tool.
- 11:05Because as Matt was saying, you know, you have a lot of CPU bound code and you don't have a lot of IOD and you're safe.
- 11:11But like sometimes it's very difficult to know that that's true or like how much do you have.
- 11:15So you have a very simple situation like a script maybe or a simple algorithm.
- 11:18It may work and you don't need to reach for something more sophisticated, right?
- 11:21Yeah.
- 11:21Knowing what type of problem you're falling into and whether this is the right tool already requires you to know something about where your program is spending most of its time.
- 11:29If you are using this tool to find out where your program is spending its time, you might not even be able to accurately judge if this is the right tool to use.
- 11:37That's true.
- 11:37But also it can give you some good information, right?
- 11:40It's not completely, but it certainly, as long as you are aware of those limitations that you laid out, Matt, you could look at it and say, okay, I understand that these things that seem to be equal time, they might not be equal.
- 11:52But it still gives you a sense of like within my Python code, I'm doing more of this.
- 11:56Right.
- 11:56Or here's how much time I'm waiting.
- 11:58Also another thing to mention here, which is going to become relevant when we talk about memory as well, is an advantage of this is that because it's in the standard library.
- 12:06What this tool produces is a file with the information of the profile run.
- 12:10And because it's in the standard library and it's so popular, there is a lot of tools that can consume the file and show you the information in different ways.
- 12:17So you have a lot of ways to choose how you want to look at the information.
- 12:21Some people, for instance, like to look at the information into kind of a graph that will tell you the percentage of the calls and things like that.
- 12:29Some other people like to see it in a graphical view.
- 12:32So there's this box with boxes inside that will tell you the percentage and things like that.
- 12:37And some people like to see it in Terminal or in the GUI or in PyCharm or whatever it is.
- 12:43So there is a lot of ways to consume it, which is very good because, you know, different people have different ways to consume the information.
- 12:49And that is a fact.
- 12:50Depends on who you are and how, whether you're looking at some visualizations may be better than others.
- 12:55And there is a lot to choose here.
- 12:57And that is an advantage compared to something that, you know, just offers you one and that's all.
- 13:00Indeed.
- 13:00So I mentioned that 3.12 might have some interesting things coming around this profiling story.
- 13:08We have PEP669, low impact monitoring for CPython.
- 13:14This is part of the Faster CPython initiative, I'm guessing, because Mark Shannon is the author of it.
- 13:19It's kind of related.
- 13:20I don't think it's immediately there.
- 13:22I mean, it's related to the fact that it's trying to make profiling itself better.
- 13:26I know he has to spend time from the Faster CPython project into implementing this.
- 13:29I need to double check if this is in 3.12.
- 13:32I think it is.
- 13:33But it may be accepted for 3.12 by going to 3.13.
- 13:37We should double check.
- 13:39So the 100% say that it's in 3.12 because I don't know if he had the time to fully implement it.
- 13:45Yeah, I don't know if it's in there, but from a PEP perspective, it says accepted and for Python 3.12.
- 13:49For Python 3.12, yes.
- 13:50I do believe it's in there.
- 13:51I'm pretty sure that I've been talking with Ned Batchelder a bit about coverage, and I'm pretty sure he said he's been testing with this in the coverage, testing coverage against this with 3.12 betas.
- 14:02So the idea here is to add additional events to the execution in Python, I'm guessing.
- 14:08It says it's going to have the following events, pystart, pyresume, pythrow, pyyield, pyunwind, call.
- 14:15How much either of you guys know about this?
- 14:17Yeah, I'm quite.
- 14:18I mean, I was involved in judging this, so I know quite a lot since I was accepting this.
- 14:25But the idea here is that just as a high-level view, because if we go into detail, we can again make two more podcast episodes, and maybe we should invite Max Shannon in that case.
- 14:35But the idea here is that the tools that the interpreter exposes for the profiler and debugging, because debugging is also involved here, they impose quite a lot of overhead over the program.
- 14:45What this means is that running the program under a debugger or a profiler will make it slow.
- 14:51We are talking about tracing profilers, yes, because the other kind of profilers, sampling profilers, they work differently, and they will not use these APIs.
- 15:00They trade accuracy for a lower impact, yeah.
- 15:02Yes.
- 15:03I mean, just to be clear, because I don't think we are going to talk that much about them, but just to be clear what is the difference.
- 15:08The difference here is that a sampling profiler, instead of tracing the program, I see the runs and sees everything that the program does, it just takes photos of the program at regular intervals.
- 15:18So it's like, you know, imagine that you're working on a project, and then I enter your room every five minutes and tell you, what file are you working on?
- 15:26And then you tell me, oh, it's a program.cpp, right?
- 15:29And then I enter again, it's a program.cpp.
- 15:31And then I enter again, it's like a other thing.cpp.
- 15:34So if I enter 100 times and 99 of them you were in this particular file, then I can tell you that that file is quite important, right?
- 15:41So that's the idea.
- 15:43But maybe when I was not there, you were doing something completely different, and I miss it.
- 15:46It just happens that every five minutes you were checking the file because you really like how it's written, but you were not doing anything there.
- 15:52So, you know, like there is a lot of cases when that can misrepresent what actually happened.
- 15:57An advantage here is that nobody is annoying you while I'm not entering the room, right?
- 16:03So you can do actual work at actual speed, right?
- 16:06And therefore, these profiles are faster, but as you say, they trade kind of like accuracy for speed.
- 16:12But this PEP tries to make tracing profiles faster, so that the other ones.
- 16:16And the idea here is that the kind of APIs that Cpython offers are quite as low because they are super generic in the sense that what they give you is that every time a function call,
- 16:26in the case of the profiler APIs, is made or returns, it will hold you, but it will basically pre-compute a huge amount of, well, not a huge amount,
- 16:36but it's quite a lot of amount of information for you, so you can use it.
- 16:39Most of the time you don't care about that information, but it's just there, and it was just pre-computed for you, so it's very annoying.
- 16:45And in the case of the tracing, the Cs.setTrace, so this is for debuggers and, for instance, coverage use that as well, which is the same idea, but instead of every function call, it's every bytecode instruction.
- 16:56So every time the bytecode execution loop executes the instruction, it calls you, or you can have different events, like every time it changes lines, something like that.
- 17:05But the idea is that the overhead is even bigger.
- 17:07And again, you may not care a lot about all these things.
- 17:11So the idea here is that instead of, like, calling you every single time, you could maybe do something, you can tell the interpreter what things are you interested in.
- 17:19So you say, well, look, I'm a profiler, and I am just interested on, you know, when a function starts and when a function ends, I don't care about the rest.
- 17:27So please don't pre-compute line numbers, don't give me any of these other things, just call me.
- 17:31Just don't do anything.
- 17:32So the idea is that then you only pay for these particular cases, and the idea is that it's as fast as possible.
- 17:40Because also the fact that this is event-based makes the implementation a bit easier in the sense that it doesn't need to slow down the normal execution loop by a lot.
- 17:47Only you register a lot of events, then it will be quite slow.
- 17:50But as you can see here for the list of events, there is a bunch of things that you may not care about, like, for instance, race exceptions or change lines and things like that.
- 17:58But the idea here is that, you know, because it's event-based, then if you are not interested in many of these things, then you don't register the events for that.
- 18:05So you are never called for them and you don't pay the cost, which in theory will make some cases faster.
- 18:10Some others not.
- 18:11Sure.
- 18:12It depends on how many of these events the profiler subscribes to, right?
- 18:15This portion of Talk Python To Me is brought to you by JetBrains and PyCharm.
- 18:22Are you a data scientist or a web developer looking to take your projects to the next level?
- 18:27Well, I have the perfect tool for you, PyCharm.
- 18:30PyCharm is a powerful integrated development environment that empowers developers and data scientists like us to write clean and efficient code with ease.
- 18:39Whether you're analyzing complex data sets or building dynamic web applications, PyCharm has got you covered.
- 18:45With its intuitive interface and robust features, you can boost your productivity and bring your ideas to life faster than ever before.
- 18:52For data scientists, PyCharm offers seamless integration with popular libraries like NumPy, Pandas, and Matplotlib.
- 18:59You can explore, visualize, and manipulate data effortlessly, unlocking valuable insights with just a few lines of code.
- 19:06And for us web developers, PyCharm provides a rich set of tools to streamline your workflow.
- 19:10From intelligent code completion to advanced debugging capabilities, PyCharm helps you write clean, scalable code that powers stunning web applications.
- 19:19Plus, PyCharm's support for popular frameworks like Django, FastAPI, and React make it a breeze to build and deploy your web projects.
- 19:28It's time to say goodbye to tedious configuration and hello to rapid development.
- 19:33But wait, there's more.
- 19:35With PyCharm, you get even more advanced features like remote development, database integration, and version control, ensuring your projects stay organized and secure.
- 19:43So whether you're diving into data science or shaping the future of the web, PyCharm is your go-to tool.
- 19:48Join me and try PyCharm today.
- 19:50Just visit talkpython.fm/done-with-pycharm, links in your show notes, and experience the power of PyCharm firsthand for three months free.
- 20:01PyCharm.
- 20:02It's how I get work done.
- 20:04For example, so one of the events is PyUnwind.
- 20:10So exit from a program function during an exception unwinding.
- 20:14You probably don't really care about recording that and showing that to somebody in a report.
- 20:20But the line event, like an instruction is about to be executed that has a different line number from the preceding instruction.
- 20:26There we go.
- 20:27All right.
- 20:27Something like that.
- 20:28This is an interesting one.
- 20:29Sorry, Matt, do you want to mention something?
- 20:31I think you do need to care about unwind.
- 20:33Actually, you need to know what function is being executed.
- 20:36And in order to keep track of what function is being executed at any given point in time, you have to know when a function has exited.
- 20:42There's two different ways of knowing when the function has exited, either a return or an unwind, depending on whether it returned due to a return statement or due to falling off the end of the function or because an exception was thrown and not caught.
- 20:54Okay.
- 20:54Give us an example of one that you might not care about from a memory-style perspective.
- 21:00Instruction is one that we wouldn't care about.
- 21:02In fact, even line is one that we wouldn't care about.
- 21:04Memory cares about profilers in general, for the most part, will care not about what particular instruction is being executed in a program.
- 21:12They care about what function is being executed in a program because that's what's going to show up in all the reports they give you rather than line-oriented stuff.
- 21:19So maybe coverage and then Batch Elder might care about line.
- 21:23Yeah, yeah, yeah.
- 21:24But you guys would.
- 21:25He very much cares about line.
- 21:26Yeah, I can imagine.
- 21:27And that's the slow one.
- 21:28That's the slow one.
- 21:29And it's important to understand why it's slow.
- 21:31It's slow because the program doesn't really understand what a line of code is, right?
- 21:36A line of code is a construct that only makes sense for you, the programmer.
- 21:41The parser doesn't even care about the line because it sees coding in a different way.
- 21:45It's a stream of bytes.
- 21:46And lines don't have semantic meaning for most of the program compilation and execution.
- 21:52The fact that you want to do something when a line changes, then it forces the interpreter to not only keep around that information, which mostly is somehow there, compressed, but also reconstructed.
- 22:02So basically every single time, I mean, it's made in a obviously better way.
- 22:06But the idea is that every single time it executes the instruction, it needs to check, oh, did I change the line?
- 22:11And then if the answer is this, then it calls you.
- 22:14That is basically the old way, sort of.
- 22:16Because instead of doing that, it has kind of a way to know when that happens, so it's not constantly checking.
- 22:21But this is very expensive because it needs to reconstruct that information.
- 22:24That slowness is going to happen every single time you're asking for something that doesn't have kind of meaning in the execution of the program.
- 22:31And an exception has it.
- 22:32Like the interpreter needs to know when an exception is raised and what that means because it needs to do something special.
- 22:37But the interpreter doesn't care about what a line is.
- 22:39So that is very expensive.
- 22:41Right.
- 22:41You could go and write statement one, semicolon statement two, semicolon statement three, and that would generate a bunch of bytecodes, but it still would just be one line.
- 22:48Sure.
- 22:49Hey, Pablo, sidebar, it sounds like there's some clipping or some popping from your mic, so maybe just check the settings just a little bit.
- 22:56Oh, absolutely.
- 22:57Yeah, hopefully we can clean that up just a bit.
- 22:59But it's not terrible either way.
- 23:00All right.
- 23:01So you think this is going to make a difference?
- 23:03This seems like it's going to be a positive impact here?
- 23:06One particular way that it'll make a difference is that for the coverage case that we just talked about, coverage needs to know when a line is hit or when a branch is hit, but it only needs to know that once.
- 23:15And once it has found that out, it can stop tracking that.
- 23:18So the advantage that this new API gives is the ability for coverage to uninstall itself from watching for line instructions or watching for function call instructions from a particular frame.
- 23:31Once it knows that it's already seen everything that there is to see there, then it can speed up the program as it goes by just disabling what it's watching for as the program executes.
- 23:40Okay.
- 23:40That's an interesting idea.
- 23:41It's like, it's decided it's observed that section enough in detail, and it can just kind of step back a little bit higher.
- 23:47Yep.
- 23:47All right.
- 23:48Okay.
- 23:48Excellent.
- 23:49So this is coming, I guess, in October.
- 23:52Pablo will release it to the world.
- 23:54Thanks, Pablo.
- 23:54No, this time is Thomas Wooders, which is the release manager of 312 is Thomas.
- 24:00Oh, this is Thomas.
- 24:01Oh, this is 312.
- 24:02That's right.
- 24:02That's right.
- 24:03So you want to blame someone, don't blame me for this one.
- 24:06Exactly.
- 24:08Exactly.
- 24:08All right.
- 24:09So that brings us to your project, Memray, which is actually a little bit of a different focus than at least Cprofile, right?
- 24:17And many of the profilers, I'll go and say most of the profilers answer the question of where am I spending time, not where am I spending memory, right?
- 24:26I would agree that that's true.
- 24:27There are definitely other memory profilers.
- 24:29We're not the only one, but the majority of profilers are looking at where time is spent.
- 24:32And yet understanding memory in Python is super important.
- 24:36I find Python to be interesting from the whole memory, understanding the memory allocation algorithms, and there's a GC, but it only does stuff some of the time.
- 24:47Like, how does all this work, right?
- 24:49And we as a community, maybe not Pablo as a core developer, but as a general rule, I don't find people spend a ton of time obsessing about memory like maybe they do in C++ where they're super concerned about memory leaks or some of the garbage collected languages where they're always obsessed with.
- 25:05You know, is the GC running and how's it affecting real time or near real time stuff?
- 25:10It's a bit of a black box, maybe how Python memory works.
- 25:15Would you say for a lot of people out there?
- 25:17Oh, yeah, absolutely.
- 25:18Yeah, I think that's definitely true.
- 25:20I think it is as well.
- 25:21And even these days, with all the machine learning and data science and the higher the abstraction goes, the easier it is to just allocate three gigabytes without you knowing.
- 25:30Like, you do something and then suddenly you have half of the RAM filled by something that you don't know what it is.
- 25:35Yeah.
- 25:36Because, you know, you are so high level that you didn't allocate any of these memories as the library.
- 25:40Yeah. Profiling for where time is being spent is something that pretty much every developer wants to do at some point.
- 25:45From the very first programs you're writing, you're thinking to yourself, well, I wish this was faster and how can I make this faster?
- 25:50I think looking at where your program is spending memory is more of a special case that only comes up in either when you have a program that's using too much memory and you need to figure out how to pair it back.
- 26:01Or if you are trying to optimize an entire suite of applications running on one set of boxes and you need to figure out how to make better use of a limited set of machine resources across applications.
- 26:14So that comes up more at the enterprise level.
- 26:17Yeah, sure.
- 26:18We heard Instagram give a talk about what did they entitled it?
- 26:21Something like dismissing the GC or something like that where they talked about actually.
- 26:26It's very funny because they made that talk and then they make a following up saying like the previous idea was actually bad.
- 26:33So now we have a refined version of that.
- 26:34I know.
- 26:35I remember they did all that.
- 26:36We have a refined version of the idea.
- 26:37But yeah.
- 26:38This was the one where they were disabling GC in their worker processes.
- 26:43Yeah.
- 26:44For, I think they're Jenga workers.
- 26:45Yeah.
- 26:45Yes.
- 26:46They have a forecast check.
- 26:47Quite interesting use case because it's quite common.
- 26:49But I want to ask to what Matt said that memory has this funny thing compared with time,
- 26:54which is that when people think about the time my program is spending on something,
- 26:59they don't really know what they are talking about.
- 27:01Right.
- 27:02They know what you want.
- 27:02Memory is funny because most of the time they actually don't.
- 27:06And you will say, how is that possible?
- 27:07Like the problem is that with memory is that you understand the problem.
- 27:10Like I have this thing called memory on my computer and it's like a number,
- 27:15like 12 gigabytes or six gigabytes or whatever it is.
- 27:18And it's half full.
- 27:19And I understand that concept, but the problem is that why is half full or like,
- 27:23what is even like memory in my program, which is different from that value.
- 27:28Now there's a huge disconnect.
- 27:30Right.
- 27:30And this is so much, so interesting.
- 27:33Like, I don't know if like, this is going to be a lot, like a super interesting to talk
- 27:36about, but like, I want to just highlight this because when, when I, imagine that I ask
- 27:41you, what is allocating memory for you?
- 27:43Like, what is that?
- 27:44It's calling malloc.
- 27:45It's creating a Python object.
- 27:46It like, because when you, this is, this is very interesting.
- 27:49And in Python, because we are so high level, who knows?
- 27:52Because when you create a Python object, well, it may or may not require memory.
- 27:57But when you call malloc, it may or may not actually allocate memory.
- 28:00Right.
- 28:01And if you really go and say, okay, so, so just tell me when I really go to that, you
- 28:07know, physical memory, and I really spend some of that physical memory in my problem.
- 28:11If you want just that, then you are not going to get information about your program because
- 28:16you are above so many abstractions that if I just told you when that happens, you're going
- 28:21to miss so much because you're going to find that the Python and the runtime C or C++ and
- 28:28the OS really likes to batch this operation.
- 28:31The same way you don't want to, you know, you're going to read a big file.
- 28:35When you call read, you're not going to read one byte at a time because that will be very
- 28:39expensive.
- 28:39The OS is going to kind of read a big chunk.
- 28:42And every time you call read, it's going to give you the pre-chunk that it already fetched.
- 28:47Right.
- 28:47And here it will happen the same.
- 28:49It's going to basically, even if you ask for like a tiny amount, like let's say you want
- 28:53just a 5k bytes, right?
- 28:55It's going to record like grab a big chunk and then it's going to give you from the chunk until
- 28:59it gets rid of.
- 29:00So what's going to happen is that you may be very unlucky and you're going to ask for
- 29:03a tiny, tiny object.
- 29:04And if you only care when I really go to the physical memory, you're going to get like maybe
- 29:09a 4k allocation from that very, very tiny object that you ask.
- 29:13And then you're going to, that doesn't make any sense because I just wanted space for this
- 29:16tiny object.
- 29:17And then you located four kilobytes of memory or even more.
- 29:20It's super not obvious, isn't it?
- 29:22Yeah.
- 29:22On Linux, the smallest amount you could possibly allocate from the system is always a multiple
- 29:27of four kilobytes.
- 29:28Well, that's by default.
- 29:29You can actually change that.
- 29:30The page size.
- 29:32The page size can be changed.
- 29:33Yes.
- 29:33Can it be lowered?
- 29:34I don't think it can be lower, but certainly it can be made higher.
- 29:37And when you make higher, there is this big page optimization.
- 29:41When it's super ridiculous.
- 29:43Actually, Windows, you can do the same, if I recall, because Windows has something called
- 29:46huge pages.
- 29:47There's something called huge pages.
- 29:49And it's very funny because it affects some important stuff, like the speed of hard drives
- 29:53and things like that.
- 29:54This portion of Talk Python To Me is brought to you by Influx Data, the makers of InfluxDB.
- 30:02InfluxDB is a database purpose built for handling time series data at a massive scale for real-time
- 30:10analytics.
- 30:10Developers can ingest, store, and analyze all types of time series data, metrics, events, and
- 30:16traces in a single platform.
- 30:18So, dear listener, let me ask you a question.
- 30:20How would boundless cardinality and lightning-fast SQL queries impact the way that you develop
- 30:25real-time applications?
- 30:26InfluxDB processes large time series data sets and provides low-latency SQL queries, making
- 30:32it the go-to choice for developers building real-time applications and seeking crucial insights.
- 30:38For developer efficiency, InfluxDB helps you create IoT analytics and cloud applications using
- 30:44timestamped data rapidly and at scale.
- 30:47It's designed to ingest billions of data points in real-time with unlimited cardinality.
- 30:52InfluxDB streamlines building once and deploying across various products and environments from
- 30:58the edge, on-premise, and to the cloud.
- 31:00Try it for free at talkpython.fm/influxDB.
- 31:05The link is in your podcast player show notes.
- 31:08Thanks to InfluxData for supporting the show.
- 31:13Maybe one of you two can give us a quick rundown on the algorithm for all the listeners.
- 31:18But the short version is if Python went to the operating system for every single byte of memory
- 31:25that it needed.
- 31:26So if I create the letter A, it goes, oh, well, I need, you know, what is that?
- 31:3030, 40 bytes.
- 31:31Turns out.
- 31:32Hopefully less.
- 31:33Hopefully less.
- 31:34But yeah, it's not eight.
- 31:37Yeah, it's not just the size, actually, of like you would have in C.
- 31:39There's like the reference count and some other stuff.
- 31:42Whatever.
- 31:43Like it's, let's say, 30, 20 bytes.
- 31:45It's not going to go to the operating system and go, I need 20 more.
- 31:4820 more bytes.
- 31:4920 more bytes.
- 31:50It has a whole algorithm of getting certain like blocks of memory, kind of like 4K blocks
- 31:55of page size.
- 31:56And then internally say, well, here's where I can put stuff until I run out of room to store,
- 32:02you know, new.
- 32:02Right.
- 32:0320 byte size pieces.
- 32:05And then I'll go ask for more.
- 32:07So you need something that understands Python to tell you what allocation looks like, not just
- 32:13something that looks at how the process talks to the OS, right?
- 32:16Yeah, I think that's definitely the case.
- 32:18There's one pattern that you'll notice with large applications is that there tend to be
- 32:22caches all the way down.
- 32:23And you can think of this as the C library fetching, allocating memory from the system and then
- 32:28caching it for later reuse once it's no longer in use.
- 32:32And above that, you've got the Python allocator doing the same thing.
- 32:36It's fetching memory from the system allocator and it's caching it itself for later reuse and
- 32:42not freeing it back to the system immediately, necessarily.
- 32:46Yeah.
- 32:46The key here, which is a conversation that I have with some people that are surprised, like,
- 32:51like, okay.
- 32:52So when they ask like, what is this Python allocator business?
- 32:54And when you explain it, they say, well, it's doing the same thing as malloc in the sense that
- 32:59when you call malloc, it doesn't really go to the system every single time.
- 33:02It does the same thing in a different way with a different algorithm.
- 33:06I mean, that the Python allocator does.
- 33:08So what's the point if they are doing the same thing?
- 33:10The key here is that is the focus.
- 33:12Like the algorithm that malloc follows is generic.
- 33:15Like it doesn't know what you're going to do.
- 33:17It's trying to be fast, as fast as possible.
- 33:20But for the, because it doesn't know how you're going to use it, it's going to be, try to make
- 33:24it as fast as possible for all possible cases.
- 33:26But the Python allocator knows something which is very important, which is that most Python
- 33:31objects are quite small.
- 33:33And the object itself, not the memory that it holds to, right?
- 33:36Because like the list object by itself is small.
- 33:39It may contain a lot of other objects, but that's a big array, but the object itself is very small.
- 33:44And the other thing is that there tend to be sort of leave.
- 33:46This means that there is a huge amount of objects that are being created and destroyed very fast.
- 33:49And that is a very specific pattern of uses.
- 33:52And it turns out that you can customize the algorithm doing the same basic thing with Matt
- 33:57mentioned, this caching of memory.
- 33:58You can customize the algorithm to make that particular pattern faster.
- 34:02And that's why we have a Python allocator in Python.
- 34:05And we have also malloc.
- 34:06Right.
- 34:07So there's people can go check out the source code.
- 34:09There's a thing called PyMalloc that has three data structures that are not just bytes, but
- 34:15it has arenas, chunks of memory that PyMalloc directly requests.
- 34:20It has pools, which contain fixed sizes of blocks of memory.
- 34:25And then these blocks are basically the places where the variables are actually stored, right?
- 34:32Like I needed 20 bytes, so that goes into a particular block.
- 34:35Often the block is dedicated to a certain size of object, if possible, right?
- 34:41And these tend to be quite small.
- 34:42Because the other important thing is that this is only used if your object is smallish.
- 34:46I think it's 512 kilobytes or something like that.
- 34:50There's a limit.
- 34:51It doesn't matter.
- 34:51The important thing is that if the object is medium size or big, it goes directly to malloc.
- 34:57So it doesn't even bother with any of these arenas or blocks.
- 35:01So this is just for the small ones.
- 35:02And I guess that's because it's already different from the normal allocation pattern that we see
- 35:07for Python objects, that they tend to be small.
- 35:09At the point where you're getting bigger ones, we might not have as good of information about
- 35:12what's going on with that allocation.
- 35:14And it might make sense to just let the system malloc handle it.
- 35:18Okay.
- 35:18So there's that side.
- 35:19We have reference counting, which does most of the stuff.
- 35:22And then we have GCs that catches the cycle.
- 35:24Not really worth going in, but primarily reference counting should be people's mental model,
- 35:28I would imagine, right?
- 35:29For the lifetime, you mean?
- 35:31For the lifetime of objects, yeah.
- 35:32Yeah.
- 35:32Yeah.
- 35:33Yeah.
- 35:33That's why it was at least conceivable that Instagram could turn off the GC and
- 35:37instantly run out of memory, right?
- 35:39Right.
- 35:39Right.
- 35:40I mean, when they turn off, this is just the pedantic compiler engineer mindset turning on
- 35:45here.
- 35:46But technically, reference count is a GC model.
- 35:48So technically, there is two GCs in Python, right?
- 35:51But yeah.
- 35:52But normally, when people say that you see...
- 35:54How about not the mark and sweep GC?
- 35:57Right.
- 35:57When people say that you see, they say the cycle GC.
- 36:00Yeah.
- 36:01Right.
- 36:01Yeah.
- 36:01Cool.
- 36:02Python doesn't actually have a mark and sweep GC.
- 36:04The way the cycle collecting GC works is not mark and sweep.
- 36:08It's actually implemented in terms of the reference counts.
- 36:10It was something that surprised me a lot when I learned it.
- 36:13Yeah.
- 36:13There is an interesting page in the dev guide written by a crazy Spanish person that goes
- 36:19into detail over how it is done.
- 36:20Yeah.
- 36:21I wonder who wrote that.
- 36:21Okay.
- 36:22We talked a bit about profilers.
- 36:23We, I think, probably dove enough into the memory.
- 36:26Again, that could be a whole podcast.
- 36:28Just like, how does Python memory work?
- 36:29But let's focus on not how does it work, but just measuring it for our apps.
- 36:34And you touched on this earlier, you guys, when you talked about there's memory and there's
- 36:39performance, but there's also a relationship between memory and performance, right?
- 36:43Like, for example, you might have an algorithm that allocates a bunch of stuff that's thrown
- 36:47away really quickly.
- 36:48And allocation and deallocation has a cost, right?
- 36:51You might have more things in memory that mean cache misses on the CPU, which might make
- 36:57it run slower, right?
- 36:58There's a lot of effects that kind of tie together with performance in memory.
- 37:02So I think it's not just about memory.
- 37:04It's what I'm trying to say, that you want to know what it's up to.
- 37:07So tell us about memory.
- 37:08It's such a cool project.
- 37:10So yeah, memory is our memory profiler as a lot of fairly interesting features.
- 37:16It does.
- 37:17One of them is that it supports a live mode where you can see where your application is
- 37:23spending memory as it's running, as like a nice little automatically updating grid that
- 37:28has that information in it that you can watch as the program runs.
- 37:31It also has the ability to attach to an already running program and tell you some stuff about
- 37:35it.
- 37:35But sort of the main way of running it is just capturing capture file as the program runs in
- 37:41the same way as cprofile would capture its capture file.
- 37:44Check out the report.
- 37:45Yeah.
- 37:46Yeah.
- 37:46Doing some reporting based on that capture file after the fact.
- 37:49So just for people listening, because I know they can't see this, the live version is awesome.
- 37:54If you've ever run glances or htop or something like that, where you can kind of see a two-y type
- 38:01of semi-graphical live updating dashboard, it's that, but for memory.
- 38:07This is really nice.
- 38:09Yeah.
- 38:09Yeah.
- 38:09And the other really cool feature that it's got is the ability to see into a C or Ruster's C++ extension
- 38:16modules.
- 38:16So you can see what's happening under the hood inside of things that are being used from your Python code.
- 38:23So if you're calling a library that's implemented partly in C, like NumPy, you can see how NumPy is doing its allocations under the hood.
- 38:30Right.
- 38:30Yeah.
- 38:30Pablo, you were touching on this a little bit, like how the native layer is kind of a black box that you don't really see into, you don't see into a CPython.
- 38:38Sorry, with C profile, but also with some of the other memory profilers.
- 38:41Right.
- 38:42And this, this looks at it across the board.
- 38:44C, C++, Rust.
- 38:45Right.
- 38:46So this is kind of important because as we discussed before, what is memory?
- 38:51Not only is complicated, but also depends on what you want.
- 38:53Like the thing is that, and this is quite a big, important part is that you really need to know what you're looking for.
- 38:59So for instance, we memory kind of highlights two important parts, which is that it sees all possible allocations.
- 39:06So not only the ones made by Python, because like Python has a way to tell you when an object is going to be created, but it doesn't really, it's not going to tell you is you are going to kind of like use memory for it or not.
- 39:17Among other things, because for instance, there is the Python even caches of entire objects.
- 39:21There is this concept of free lists.
- 39:23So object creation doesn't really mean memory allocation.
- 39:26It also tells you when you are going to allocate memory, when you normally run Python, you may use PyMalloc and PyMalloc caches that memory.
- 39:34So you don't really, you may not go to the actual system.
- 39:37So by default, memory checks all allocations onto the system allocator.
- 39:42So malloc basically.
- 39:43So every time you call malloc or mem map or one of these, we see it.
- 39:46And apart from seeing it and recording it, we also can tell you who made the location from C++ and Python.
- 39:54On top of that, if you really want to know when you create objects, well, not objects, but like when, when Python says I need memory, we can also tell you that if you want.
- 40:02So if you really want to know, well, I don't really care if PyMalloc caches and whatnot.
- 40:07Every single time Python does, it requires memory.
- 40:11Just tell me, even if you reuse it, maybe I just want to know because that kind of will show you a bit of like when you require object creation.
- 40:17Again, not 100%, but mostly, mostly doing that.
- 40:22And the idea here is that you can, you can really customize what you want to track and you don't pay for what you don't want.
- 40:28So for instance, most of the time you don't want the, to know when Python requires memory because most of the time it's not going to actually impact your memory usage.
- 40:37Right.
- 40:37Because as you mentioned, PyMalloc is going to use one of these arenas and you're going to see the actual malloc.
- 40:43But sometimes you want, so memory allows you to know, decide when you want to track one.
- 40:48And by default, it's going to use the faster method, which is mostly the, is the most similar to when you execute your program.
- 40:55And, or an interesting feature that as of this time only memory has is that it can tell you the location, like who actually made the location.
- 41:03So who call who, et cetera.
- 41:05Right.
- 41:05So you're going to tell you this Python function called this C function that in turn call this Python function.
- 41:10And this one actually made a call to malloc or, or created a Python list or something like that.
- 41:15I think that was really a fantastic feature that it's easy to kind of miss the significance of that.
- 41:20But if you get a memory profiler, it just says, look, you allocated a thousand lists and they used a good chunk of your memory.
- 41:27You're like, well, okay, well let's go through and find where lists are coming from.
- 41:31Right.
- 41:32Like, like converting that information back of how many of these types of objects and how many of those objects you allocated back to like, where can I look at my code and possibly make a change about that?
- 41:42That can be really, really tricky.
- 41:44And so the fact that you can see this function is allocating this much stuff is super helpful.
- 41:49One of the important things here to highlight, which I think is interesting.
- 41:53Maybe Matt can also cover it more in detail, but is that memory, most memory profilers are actually sampling profilers.
- 42:00Reason is that the same way tracing profilers for function calls need to trace every single function call.
- 42:06A memory profiler is that tracing memory profile needs to trace every single allocation.
- 42:11But there are allotions happen much more often than function calls.
- 42:15So you made the calculation based on normal programs, when it can be anything that you want, just open Python even, or even any C or C++, you're going to see that actually you allocate a huge amount of, so doing something per allocation is super expensive.
- 42:28It's extremely expensive.
- 42:30And most profilers, what they do is that they do sampling.
- 42:32It's a different kind of sampling.
- 42:33So it's not this photo kind of thing.
- 42:35They use a different statistic based on bytes.
- 42:37So they basically see these memories, a stream of bytes, and they decide to sample some of them.
- 42:42So they are inaccurate, but normally they try to be, use statistics to tell you some information.
- 42:47So memory on the other hand.
- 42:48To give an example, instead of sampling every 10 milliseconds and seeing what the process is doing right now, it's sampling every 10 bytes.
- 42:55So every time a multiple of 10 bytes is allocated from the system, it checks what was allocating that.
- 43:01Although it'll use a bigger number than 10 in order to ask for this to actually be effective, since most allocations will get at least 10 bytes.
- 43:07But something like that.
- 43:08Yeah.
- 43:09Right.
- 43:10So memory is tracing, which means that it sees every single allocation.
- 43:13This is quite an interesting kind of decision here because like, you know, it's very, very hard to make a tracing profiler that is not extremely slow.
- 43:20So, you know, memory tries to be very fast, but obviously it's going to be a bit slower than sampling profilers.
- 43:25But the advantage of this, what makes memory quite unique is that because it captures every single allocation into the file, which has a huge amount of technical challenges.
- 43:34For instance, these files can be genormals.
- 43:36Like we are taking gigabytes and gigabytes, and we put a ridiculous amount of effort into making them as small as possible.
- 43:42So it has double compression and things like that.
- 43:44So you're not using XML to store that?
- 43:46No, I certainly not.
- 43:47You know, the first version almost, I think every, if you look at our release notes from one version to the next, every version, we're like, and the capture files are now 90% smaller.
- 43:56Again, we've continued to find more and more ways to shrink.
- 43:59Sure.
- 44:00Right.
- 44:00At the cost of the now reasoning about what is in the file is just bananas because, you know, we, we kind of do a first manual compression based on the information we know is there, but then we run it C4 on that.
- 44:12So, so it's like double compression already.
- 44:14And there is even like a mode when, when we pre kind of massage the data into the only one that you care.
- 44:20So it's even smaller.
- 44:21So it is out of effort.
- 44:23But the advantage of having that much information is that now we can produce a huge amount of reports.
- 44:28So for instance, not only we can show you the classic flame graph, like this, this visualization over hook or what, I'm like, you know, instead of where you're spending your time, where did you locate your memory, but we can do some cooler things.
- 44:40So for instance, we can, you mentioned that there is this relationship between like running time and memory.
- 44:46So one of the things that we can show you in the latest versions of memory is that for instance, in my end that you have like a Python list, or if you're in C++ a vector, right.
- 44:55And then you have a huge amount of data you want to put into the vector and you start adding, showing Python will be append.
- 45:01So you start calling append.
- 45:02And then at some point the list has a pre-allocated size and you're going to fill it.
- 45:07And then there is no more size, no more room for the data.
- 45:10So it's going to say, well, I need more memory.
- 45:12So you're going to require a bigger chunk of memory.
- 45:14You're going to copy all the previous elements into the new chunk.
- 45:17And then it's going to keep adding elements and it's going to happen again and again and again and again.
- 45:22So if you want to introduce millions of elements into your list, because it doesn't know how many you need.
- 45:28I mean, you could tell it, but in Python is a bit more tricky than in C++.
- 45:31C++ has a call reserve when you can say, I'm going to need this many.
- 45:36So just, just make one call to the allocator and then let me fill it.
- 45:39But in Python, there is a way to do it, but not a lot.
- 45:42So the idea here is that it's going to go through these cycles of getting bigger and bigger.
- 45:45And obviously it's going to be as low because every time you require memory, you pay time.
- 45:49And memory can detect this pattern because we have the information.
- 45:52So memory can tell you when you are doing this pattern of like creating a bigger chunk, copying, creating a bigger chunk, copying.
- 45:58And it's going to tell you, hey, these areas of your code, you could pre-reserve a bigger chunk in Python.
- 46:05There is idioms depending on what you're doing, but it's going to tell you, maybe you want to tell whatever you're creating to just allocate once.
- 46:11So for instance, in Python, you can multiply a list of none by 10 million.
- 46:15And it's going to create a list of 10 million nones.
- 46:17And instead of calling append, you set the element using .
- 46:21Oh, interesting.
- 46:22Yeah, kind of keep track of yourself of where it is instead of just using len of.
- 46:27Exactly.
- 46:28But in C++, for instance, with memory also Cs, as long as it's called from Python, so it's going to tell you, well, you should use reserve.
- 46:35So tell the vector how many elements you need.
- 46:38Therefore, you're not going to go into this.
- 46:40There's not a way to do that in Python lists though, is there?
- 46:43To actually set like a capacity level when you allocate it?
- 46:46With this trick.
- 46:47With this trick.
- 46:48Yeah, yeah.
- 46:49Then you can't use len on it anymore, right?
- 46:50There's not a something in the initialization.
- 46:53Yeah, okay.
- 46:54I didn't think so either, but I could have missed it and it would be important.
- 46:57No, no, no.
- 46:58There are ways that I don't want to reveal because the list has a, it works the same as a vector.
- 47:05It's just that the reserve call is not exposed, but there are ways to trick the list into thinking that it needs a lot of memory.
- 47:12But I know how to reveal it so people don't rely on them.
- 47:15Those ways are implementation details that can change from one Python version to the next.
- 47:18Yeah, exactly.
- 47:19For instance, one example.
- 47:20Let me give you one example.
- 47:21Imagine that you have a tuple of 10 million elements and then you call list on the tuple.
- 47:26So you want a list of those 2 million elements because Python knows that it's a tuple and it knows the size.
- 47:31It knows how many elements it needs.
- 47:33So it's going to just require the million elements array.
- 47:35And then you're going to just copy them in one go.
- 47:36So it's not going to go through this.
- 47:38I see.
- 47:39You can pass a, some kind of iterable to a list to allocate it.
- 47:44But if it is a specific type where Python knows about it and says, oh, I actually know how big that is instead of doing the growing algorithm, it'll just initialize.
- 47:53Okay.
- 47:53I think it's an implementation detail of CPython in the sense that this only works in CPython.
- 47:57I don't really remember, but there is this magic method you can implement on your classes called len hint.
- 48:02So this is underscore, underscore, len, underscore, hint, underscore, underscore, that is not the len, but it's a hint to the, to Python.
- 48:10And it's going to say, well, this is not the real len, but it's kind of an idea.
- 48:14And this is useful for instance, for generators or iterators.
- 48:17So, so you may not know how many elements there are because it's a generator, but you may know, like, at least this many.
- 48:23So Python uses this information sometimes to pre-allocate, but I don't think this is like in the language.
- 48:28I think this is just a CPython.
- 48:30Sure.
- 48:31Okay.
- 48:32Excellent.
- 48:33So let's talk about maybe some of the different reporters you've got.
- 48:37So you talked about the flame graph.
- 48:39You've got a TQDM style report.
- 48:43You can put it just out on, you know, nice colors and emoji out onto the terminal.
- 48:47Like, give us some sense of like how we can look at this data.
- 48:50Yeah.
- 48:51That one is showing you kind of just aggregate statistics about the run.
- 48:53So it tells you a histogram of how large your allocations tended to be.
- 48:58It gives you some statistics about the locations that did the most allocating and the locations that did the largest number of allocations.
- 49:07So the most by number of bytes and the most by count, as well as just what your total amount of memory allocated was.
- 49:13It's interesting because this one looks across the entire runtime of the process.
- 49:18A lot of our other reports will like the other major one that we need to talk about is the flame graph reporter.
- 49:24That's probably the most useful way for people in general to look at what the memory usage of their program is.
- 49:31But the flame graph.
- 49:32So what a flame graph is, let's start there.
- 49:34A flame graph is shows you memory broken out by call tree.
- 49:39So rather than showing any time dimension at all, the flame graph shows you this function called that function called that function called that function.
- 49:48And at any given depth of the call tree, the width of one of the function nodes in the graph shows you what percentage of the memory usage of the process was can be allocated to that call or one of the children below it.
- 50:04That can be a really useful way, a really intuitive way of viewing how time or memory is being spent across a process.
- 50:11But the downside to it is that it does not have a time dimension.
- 50:15So with a memory flame graph like this, it's showing you a snapshot at a single moment in time of how the memory usage at that time existed.
- 50:26There's two different points in time that you can select for our flame graph reports.
- 50:29So you can either pick time right before tracking started or sorry, right before tracking stopped, which is sort of the point at which you would expect everything to have been freed.
- 50:37And you can use that point to analyze whether anything was leaked.
- 50:41Something was allocated and not deallocated.
- 50:43And you want to pay attention to that.
- 50:44The other place where you can ask it to focus in on is the point at which the process used the most memory.
- 50:52So the point during tracking when the highest amount of memory was used, it'll by default focus on that point.
- 50:58And it will tell you at that point how much memory could be allocated to each unique call stack.
- 51:03Yeah, these flame graphs are great.
- 51:05You have nice search.
- 51:06You got really good tool tips, obviously, because some of these little slices can be incredibly small tool tips there.
- 51:12But you can click on them.
- 51:13If you click on one of them, it will zoom.
- 51:15Oh yeah.
- 51:16Okay.
- 51:17And then it, yeah, if you click on one, then it'll like expand down and just focus on.
- 51:21For instance, the example that you're looking at for the people like here in the podcast, they were not going to see it, but here there is one of these flame graphs and one of the kind of like paths in the flame graph.
- 51:31One of the notes in the, in the tree is about imports.
- 51:34So here I'm looking at a line that says from something import core.
- 51:37So that's obviously memory that was allocated during importing.
- 51:41So obviously you're kind of get rid of that, but hopefully unless you're implanted in the library.
- 51:45So you may not care about that one.
- 51:47You may care about the rest.
- 51:48So you could click in the other path and then you don't care about you.
- 51:52You are going to see only the memory that was not allocated during imports.
- 51:56Right.
- 51:57Or you could be surprised.
- 51:58You could go, wait, why is half my memory being used during an import?
- 52:01And I only sometimes even use that library.
- 52:03You could push that down.
- 52:05Well, it's like additionally imported or something.
- 52:07Right.
- 52:08Like here, as you can see, you go up in this example.
- 52:10I think this example uses non-py.
- 52:12Yes.
- 52:13So you hover over this line that says import non-py as MP.
- 52:16Yeah.
- 52:17You may be surprised that importing non-py is 63 megabytes.
- 52:21Megabyte.
- 52:22And 44,000 allocations as well.
- 52:25Yeah.
- 52:26Just by importing.
- 52:27So here you go.
- 52:28Surprise.
- 52:29So yes, that's...
- 52:30And if someone wants to be extremely surprised, just try to import answer flow and see what happens.
- 52:36Okay.
- 52:37I can tell you that it's not a nice surprise.
- 52:39But here you can kind of focus on different parts if you want.
- 52:43Also, we have these nice like check boxes in the top that automatically hide the imports.
- 52:48So you don't care about the imports one.
- 52:51It just hides them.
- 52:52So you can just focus on the part that is just not imports, which is a very common pattern because, again, you may not be able to optimize non-py yourself.
- 53:00Right?
- 53:01If you decide you have to use it, then you have to use it.
- 53:04So it allows you to clean a bit because these ones can get quite complicated.
- 53:08Mm-hmm.
- 53:09So another thing that stands out here is I could see that it says the Python allocator is PyMalloc.
- 53:14This is the one that we've been talking about with arenas, pools, and blocks, and pre-allocating, and all of those things.
- 53:20That's not what's interesting.
- 53:21What's interesting is you must be showing us this because there might be another one.
- 53:26That's right.
- 53:27Okay.
- 53:27Well, not another one.
- 53:28Python only ships with...
- 53:29Well, Python does ship with two, kind of.
- 53:31It's also got a debug one that you wouldn't normally use.
- 53:34But the reason we're showing this to you is because it makes it very hard to find where memory leaks happen if you're using the PyMalloc allocator.
- 53:42So if you're using PyMalloc as your allocator, you can wind up with memory that has been freed back to Python but not yet freed back to the system.
- 53:51And we won't necessarily know what objects were responsible for that.
- 53:57And if you're looking at memory leaks, we won't be able to tell you whether every object has been destroyed because we won't see that the memory has gone back to the system.
- 54:05And that's what we're looking for at the leaks level.
- 54:07Now, as Python...
- 54:08Sorry.
- 54:09As Pablo said earlier, there's an option of tracing the Python allocators as well.
- 54:12So in memory leaks mode, you either want to trace the Python allocators as well so that we can see when Python objects are freed and we know not to report them as having been leaked as long as they were ever freed.
- 54:24Or you can run with a different allocator, just malloc.
- 54:28You can tell Python to disable the PyMalloc allocator entirely and just whenever it needs any memory to always just call the system malloc.
- 54:36And in that case...
- 54:37Oh, interesting.
- 54:38Okay.
- 54:39In that case, I'm not saying...
- 54:40Yeah, there is an environment variable called Python malloc.
- 54:43So all uppercase, all together, Python malloc.
- 54:45And then you can set it to malloc, the word malloc, and that will deactivate by malloc.
- 54:50You can set it to py malloc, which will do nothing because by default you get that.
- 54:54But you can also set it to py malloc debug or something like that.
- 54:57I don't recall exactly that one.
- 54:59I think it's py malloc plus debug.
- 55:01Right.
- 55:02And that will set the debug version of py malloc, which will tell you like if you use it wrong or things like that.
- 55:06The important thing also, apart from what Matt said, is that using py malloc can be slightly surprising sometimes.
- 55:12But the important thing to highlight here is that this is what really happens.
- 55:16So normally you want to run with this on because that is good to tell you what happened.
- 55:20It's just that what happened may be a bit surprised.
- 55:23Imagine, for instance, the case that we mentioned before, imagine that you allocate a big list.
- 55:28Not a huge one, but quite a big one.
- 55:30And then it turns out that that didn't allocate any memory because, you know, it was already there available in the arenas.
- 55:36Right.
- 55:36And then you allocated like the letter A.
- 55:39Well, maybe not the letter A, but the letter E from the Spanish alphabet, right?
- 55:43Yeah.
- 55:44Which is, it's especially not cache because like, where are you going to cache that?
- 55:48If you allocate the letter E, then suddenly there is no more memory.
- 55:52So py malloc says, well, I don't have any more memory.
- 55:55So let me allocate four kilobytes.
- 55:57And then when you look at your fling graph, you're going to, the flinger is going to tell you your letter E took four kilobytes.
- 56:04And you're going to say, what?
- 56:06How is that possible?
- 56:07And then you're going to go onto Reddit and rage about how.
- 56:10Yeah, Python is stupid.
- 56:11How bad you like.
- 56:12Exactly.
- 56:13And you are going to say, how is this even possible?
- 56:15Well, the two important facts here is that, yes, it's possible because you're not, it's not that the letter E itself needed four kilobytes.
- 56:23But when you, when you wanted that, then this happens, which is what the fling graph is telling you.
- 56:29You may say, oh, but that's not what I want to know.
- 56:31I want to know how much the letter E took.
- 56:33Then you need to deactivate py malloc or set Python trace allocation, which you can.
- 56:37It's just that normally the actual thing that you want, which is very intuitive if you think about it, is what happened when I requested this object.
- 56:45Because that's when your program run is going to happen.
- 56:47Because like, imagine that normally you reach for one of these memory profilers, not by, not for looking at your program.
- 56:53Like, oh, let me look at my beautiful program.
- 56:56How is this in memory?
- 56:57How is this in memory?
- 56:58You're rich because you have a problem.
- 56:59The problem normally is that I don't have an old memory and my program is using too much.
- 57:03Why is that?
- 57:04And to answer that question, you normally want to know what happens when you run your program.
- 57:09You don't want to know what happens if I deactivate this thing and yada, yada, right?
- 57:13And you want to absolutely take care of like, okay, there is this thing that is caching memory.
- 57:17Because like, if you run it without PyMalloc, it may report a higher peak, right?
- 57:22Because like, it's going to simulate that every single object that you want to request, require memory when it really didn't happen, right?
- 57:29Because maybe actually it was cached before.
- 57:32Or in other words, the actual peak that your program is going to reach may be in a different point as well.
- 57:38Because if you deactivate this caching, then the actual peak is going to happen at a different point, right?
- 57:45Or under different conditions.
- 57:46So you really want that engine to report 4K most of the time, except with leaks.
- 57:51Because in leaks, it's a very specific case.
- 57:53In leaks, you want to know, did I forget to deallocate an object?
- 57:57And for that, you need to know really like, you know, the relationship between every single allocation and deallocation.
- 58:02And you don't want caching.
- 58:03Right.
- 58:04So you've got to be exactly always traced and always removed.
- 58:08We saw a big red warning if you run with leaks and PyMalloc saying like, this is very likely not what you want.
- 58:15But who knows?
- 58:16Maybe someone wants that, right?
- 58:18Maybe.
- 58:19You might still detect it, but you might not.
- 58:21Well, yeah.
- 58:22Like I have used that in CPython itself, for instance, because we have used successfully, like the spam.
- 58:28We have used successfully memory in several cases in CPython to find memory leaks.
- 58:34And to greater success because the fact that we can see SQL is just fantastic for CPython because it literally tells you where you forgot to put PyInRef or PyDef or something like that, which is fantastic.
- 58:46We have found bugs that were there for almost 15 years just because we couldn't.
- 58:52It was so complicated to locate those bugs that until we have something like this.
- 58:56Nobody saw it.
- 58:57Right, exactly.
- 58:58But I have required sometimes to know the leaks with PyMalloc enabled just to understand what was, how PyMalloc was holding onto memory, which for us is important, but maybe not for the user.
- 59:09All right.
- 59:10Two more things.
- 59:11We don't have a lot of time left.
- 59:13Let's talk about temporary allocations real quick.
- 59:16I think that that's an interesting aspect that can affect your memory usage, but also can affect, you know, just straight performance, both from caching and yours also spending time allocating things.
- 59:25Maybe you don't have to.
- 59:27Who wants to take this one?
- 59:28Yeah, I think we talked about this for a while when Pablo was talking about how lists allocate memory.
- 59:33One thing that memory has that most memory profilers don't have is an exact record of what allocations happened when and in what order relative to other allocations.
- 59:45And based on that, we can build a new reporting mode that most memory profilers could not do, where we can tell you if something was allocated and then immediately thrown away after being allocated and then something new is allocated and then immediately thrown away.
- 59:59We can detect that sort of thrashing pattern where you keep allocating something and then throwing it away very quickly, which lets you figure out if there's places where you should be reserving a bigger list or pre-allocating a vector or something like that.
- 1:00:12So that's based on just this rich temporal data that we're able to collect that most other memory profilers can't.
- 1:00:18Yeah, that's excellent.
- 1:00:19And you can customize what it means to be temporary.
- 1:00:22So that by default is what Matt mentioned, this allocate, the allocate, allocate, the allocate, allocate, the allocate, but you can decide for whatever reason that any allocation that is followed by a bunch of things.
- 1:00:33And then it's the allocation.
- 1:00:34And then a bunch of things is two, three, four, four, five, six allocations.
- 1:00:37Then it's considered temporary because you have, I don't know, some weird data structure that just happens to work like that.
- 1:00:44So you can select that, that end, let's say.
- 1:00:46Excellent.
- 1:00:47Yeah.
- 1:00:48And you've got some nice examples of that list.append story you were talking about.
- 1:00:52Yeah.
- 1:00:53And this absolutely matters because allocating memory is pretty slow.
- 1:00:57So when you're doing this, it really transforms something that is quadratic, like O n square into something that is constant.
- 1:01:05So you absolutely want that.
- 1:01:07You do want that.
- 1:01:08That's right.
- 1:01:08Yeah.
- 1:01:09When I was thinking of temporary variables, I was thinking of math and like, as you multiply some things, maybe you could change, change the orders or do other operations along those lines.
- 1:01:19But yeah, the growing list is huge because it's not just, oh, there's, there's one object that was created.
- 1:01:26You're making 16 and then you're making 32 and copying the 16 over.
- 1:01:30Then you're making 64 and copying the 32 over.
- 1:01:33It's, it's massive, right?
- 1:01:34And then you're making a lot of decisions.
- 1:01:35And then you're making a lot of decisions.
- 1:01:36And then you're making a lot of decisions.
- 1:01:37And then you're making a lot of decisions.
- 1:01:38And then you're making a lot of decisions.
- 1:01:39And then you're making a lot of decisions.
- 1:01:40And then you're making a lot of decisions.
- 1:01:41And then you're making a lot of decisions.
- 1:01:42And then you're making a lot of decisions.
- 1:01:43And then you're making a lot of decisions.
- 1:01:44And then you're making a lot of decisions.
- 1:01:45And then you're making a lot of decisions.
- 1:01:46And then you're making a lot of decisions.
- 1:01:47And then you're making a lot of decisions.
- 1:01:48And then you're making a lot of decisions.
- 1:01:49And then you're making a lot of decisions.
- 1:01:50And then you're making a lot of decisions.
- 1:01:51And then you're making a lot of decisions.
- 1:01:52And then you're making a lot of decisions.
- 1:01:53And then you're making a lot of decisions.
- 1:01:54And then you're making a lot of decisions.
- 1:01:55And then you're making a lot of decisions.
- 1:01:56And then you're making a lot of decisions.
- 1:01:57And then you're making a lot of decisions.
- 1:01:58And then you're making a lot of decisions.
- 1:01:59And then you're making a lot of decisions.
- 1:02:00And then you're making a lot of decisions.
- 1:02:01And then you're making a lot of decisions.
- 1:02:02And then you're making a lot of decisions.
- 1:02:03And then you're making a lot of decisions.
- 1:02:04And then you're making a lot of decisions.
- 1:02:05And then you're making a lot of decisions.
- 1:02:06And then you're making a lot of decisions.
- 1:02:07And then you're making a lot of decisions.
- 1:02:08And then you're making a lot of decisions.
- 1:02:09And then you're making a lot of decisions.
- 1:02:10And then you're making a lot of decisions.
- 1:02:11And then you're making a lot of decisions.
- 1:02:12And then you're making a lot of decisions.
- 1:02:13And then you're making a lot of decisions.
- 1:02:14And then you're making a lot of decisions.
- 1:02:15And then you're making a lot of decisions.
- 1:02:16And then you're making a lot of decisions.
- 1:02:17And then you're making a lot of decisions.
- 1:02:18And then you're making a lot of decisions.
- 1:02:19And then you're making a lot of decisions.
- 1:02:20And then you're making a lot of decisions.
- 1:02:21And then you're making a lot of decisions.
- 1:02:22And then you're making a lot of decisions.
- 1:02:23And then you're making a lot of decisions.
- 1:02:24And then you're making a lot of decisions.
- 1:02:25And then you're making a lot of decisions.
- 1:02:26And then you're making a lot of decisions.
- 1:02:27And then you're making a lot of decisions.
- 1:02:28And then you're making a lot of decisions.
- 1:02:29And then you're making a lot of decisions.
- 1:02:30And then you're making a lot of decisions.
- 1:02:31And then you're making a lot of decisions.
- 1:02:32And then you're making a lot of decisions.
- 1:02:33And then you're making a lot of decisions.
- 1:02:34And then you're making a lot of decisions.
- 1:02:35And then you're making a lot of decisions.
- 1:02:36And then you're making a lot of decisions.
- 1:02:37And then you're making a lot of decisions.
- 1:02:38And then you're making a lot of decisions.
- 1:02:39And then you're making a lot of decisions.
- 1:02:40And then you're making a lot of decisions.
- 1:02:41And then you're making a lot of decisions.
- 1:02:42And then you're making a lot of decisions.
- 1:02:43And then you're making a lot of decisions.
- 1:02:44And then you're making a lot of decisions.
- 1:02:45And then you're making a lot of decisions.
- 1:02:46And then you're making a lot of decisions.
- 1:02:47And then you're making a lot of decisions.
- 1:02:48And then you're making a lot of decisions.
- 1:02:49And then you're making a lot of decisions.
- 1:02:50And then you're making a lot of decisions.
- 1:02:51And then you're making a lot of decisions.
- 1:02:52And then you're making a lot of decisions.
- 1:02:53And then you're making a lot of decisions.
- 1:02:54And then you're making a lot of decisions.
- 1:02:55And then you're making a lot of decisions.
- 1:02:56And then you're making a lot of decisions.
- 1:02:57And then you're making a lot of decisions.
- 1:02:58And then you're making a lot of decisions.
- 1:02:59And then you're making a lot of decisions.
- 1:03:00And then you're making a lot of decisions.
- 1:03:01And then you're making a lot of decisions.
- 1:03:02And then you're making a lot of decisions.
- 1:03:03And then you're making a lot of decisions.
- 1:03:04And then you're making a lot of decisions.
- 1:03:05And then you're making a lot of decisions.
- 1:03:06And then you're making a lot of decisions.
- 1:03:07And then you're making a lot of decisions.
- 1:03:08And then you're making a lot of decisions.
- 1:03:09And then you're making a lot of decisions.
- 1:03:10And then you're making a lot of decisions.
- 1:03:11And then you're making a lot of decisions.
- 1:03:12And so we have actually used this thing to speed up C-Python.
- 1:03:14Oh, that's amazing.
- 1:03:15And like I said, this is a feature that we're able to do exactly because we're a tracing profiler.
- 1:03:19And we do see every single allocation.
- 1:03:21We built a new feature that was just released literally last week where we have a new type of flame graph we can generate.
- 1:03:28That is a temporal flame graph that gives you sliders on it where you can adjust the range of time that you are interested in.
- 1:03:35So instead of only being limited to looking at that high watermark point or only being limited to looking at the point right before tracking stop to see what's going on.
- 1:03:42What was allocated and not deallocated.
- 1:03:44You can tell the flame graph to focus in on this spot or on that spot to see what was happening at a particular point in time.
- 1:03:51And that's again a pretty unique feature that requires tracing profiling in order to be able to do because you need to know allocations that existed at any given point in time from one moment to the next.
- 1:04:02Yeah, that ability to actually assign an allocation to a place in time really unlocks a lot of cool things.
- 1:04:09Right.
- 1:04:10So it seems to me that this is really valuable for people with applications.
- 1:04:14You got a web app or some CLI app.
- 1:04:16That's great.
- 1:04:17It also seems like it'd be really valuable for people creating packages that are really popular that other people use.
- 1:04:24Right.
- 1:04:25Right.
- 1:04:26If I was Sebastian creating FastAPI, it might be worth running this a time or two on FastAPI.
- 1:04:30I think they are actually using it on FastAPI.
- 1:04:31Are they?
- 1:04:31Okay.
- 1:04:32No, it's Pydantic.
- 1:04:33I think they are using it in PyEptic.
- 1:04:34And I think our other bigger, I mean, there's a lot of users.
- 1:04:35I'm trying to think of the big ones.
- 1:04:36I think the other ones that are...
- 1:04:37Yeah.
- 1:04:37Or a lib3.
- 1:04:37There was a feature that they came to us and they said we used memory to track down where
- 1:04:50memory was being spent in a new version of URL lib3.
- 1:04:52And they said that they would not have been able to release the new feature that they wanted
- 1:04:56if they hadn't been able to get the memory under control and that we helped them do it very quickly.
- 1:05:00That is awesome.
- 1:05:01Yeah.
- 1:05:02Like all the ORMs, I'm sure that they're doing a lot of like, read this cursor and put this
- 1:05:07stuff in the list.
- 1:05:08We're going to, you know, like there's probably a lot of low hanging fruit actually.
- 1:05:11And the reason this comes to mind for me is we can run it on our code and make it faster.
- 1:05:16But if somebody who's got a popular library, like the ones you all mentioned, can find some
- 1:05:21problem, like the multiplicative improvement across everybody's app, across all the different
- 1:05:27programs and the libraries that use those, it's a huge, huge benefit, I would think.
- 1:05:32We are also very lucky because we have a wonderful community and we have using this GitHub discussions.
- 1:05:40A lot of people probably don't know that that is a thing, but we have in the memory repo,
- 1:05:45a discussion for feedback.
- 1:05:48And a lot of, there is a lot of people from like library maintainers in the Python ecosystem
- 1:05:53that have used memory successfully.
- 1:05:55And they tell us about that.
- 1:05:56And it's quite, quite cool to see like how many problems have been solved by memory.
- 1:06:02Some of them super challenging.
- 1:06:03Yeah.
- 1:06:04I've got to say, I didn't know that discussions existed until we enabled it on this repo.
- 1:06:07So I'm learning things every day.
- 1:06:09Here you are.
- 1:06:10Absolutely.
- 1:06:11Maybe just a quick question to wrap up the conversation here is Brnega Boer out there
- 1:06:16asked, does memory support Python 312 yet?
- 1:06:19Is the short answer.
- 1:06:20We're at the moment blocked on that by, Cython 0.29, not supporting 312 yet.
- 1:06:27We need to get that sorted before we can even build on 312 to start testing on 312.
- 1:06:32Do you have to build on 312 to analyze 312 applications?
- 1:06:35Yes.
- 1:06:36Yes.
- 1:06:37Okay.
- 1:06:38Because these runs on the application itself.
- 1:06:40So this is not something that exists outside.
- 1:06:42This is something that runs within the application.
- 1:06:44It's like inside.
- 1:06:45Yeah.
- 1:06:46Yeah.
- 1:06:46So you need to run your app in 312 to run memory on 312.
- 1:06:49Yes.
- 1:06:50That's the difference between this and PyStack, which we were speaking about last time.
- 1:06:53PyStack can attach to a 312 process from a 311 processor or something like that.
- 1:06:58But memory can't.
- 1:06:59Okay.
- 1:07:00Well, good to know.
- 1:07:01All right, guys.
- 1:07:02Thank you for coming back.
- 1:07:03I'm taking the extra time to tell people about this.
- 1:07:05But mostly, you know, thanks to you all and thanks to Bloomberg for these two apps, memory
- 1:07:10and PyStack.
- 1:07:11They're both the kind of thing that looks like it takes an insane amount of understanding
- 1:07:16the internals of CPython and how code runs and how operating systems work.
- 1:07:21And you've done it for all of us, so we could just run it and benefit, not have to worry about
- 1:07:25it that much.
- 1:07:26You have no idea.
- 1:07:27I will add linkers because we didn't even have the time to go there.
- 1:07:31But memory uses quite a lot of dark linker magic to be able to activate itself in the middle of nowhere, even if you didn't prepare for that.
- 1:07:41Which a lot of memory profiles require you to modify how you run your program.
- 1:07:45Memory can magically activate itself, which allows to attach itself to a running process.
- 1:07:50But yeah, for another time maybe.
- 1:07:53I wrote some of the craziest code of my life last week in support of memory.
- 1:07:57You have no idea how wild it can get.
- 1:07:59It seems intense and even that's not enough.
- 1:08:02Okay, awesome.
- 1:08:03Again, thank you.
- 1:08:04This is an awesome project.
- 1:08:06People should certainly check it out and kind of want to encourage library package authors out there to say, you know, if you've got a popular package and you think it might benefit from this, just give it a quick run and see if there's some easy wins that would help everyone.
- 1:08:18Absolutely.
- 1:08:19Well, and I just want to add a thank you very much for inviting us again.
- 1:08:22We are super thankful for being here and always very happy to talk with you.
- 1:08:26Thanks, Pablo.
- 1:08:27Seconded.
- 1:08:27Yeah.
- 1:08:28Thanks, Matt.
- 1:08:29Bye, you guys.
- 1:08:30Thanks everyone for listening.
- 1:08:31Bye.
- 1:08:31Thank you.
- 1:08:32Thank you.
- 1:08:32Thank you.
- 1:08:32Thank you.
- 1:08:32Thank you.
- 1:08:33Thank you.
- 1:08:33Thank you.
- 1:08:34Thank you.
- 1:08:35Thank you.
- 1:08:36Thank you to our sponsors.
- 1:08:37Be sure to check out what they're offering.
- 1:08:38It really helps support the show.
- 1:08:40The folks over at JetBrains encourage you to get work done with PyCharm.
- 1:08:45PyCharm Professional understands complex projects across multiple languages and technologies, so you can stay productive while you're writing Python code and other code like HTML or SQL.
- 1:08:56Download your free trial at talkpython.fm/donewithpycharm.
- 1:09:01InfluxData encourages you to try InfluxDB.
- 1:09:05InfluxDB is a database purpose-built for handling time series data at a massive scale for real-time analytics.
- 1:09:12Try it for free at talkpython.fm/influxDB.
- 1:09:16Want to level up your Python?
- 1:09:18We have one of the largest catalogs of Python video courses over at Talk Python.
- 1:09:22Our content ranges from true beginners to deeply advanced topics like memory and async.
- 1:09:27And best of all, there's not a subscription in sight.
- 1:09:30Check it out for yourself at training.talkpython.fm.
- 1:09:33Be sure to subscribe to the show, open your favorite podcast app, and search for Python.
- 1:09:37We should be right at the top.
- 1:09:39You can also find the iTunes feed at /itunes, the Google Play feed at /play, and the direct RSS feed at /rss on talkpython.fm.
- 1:09:48We're live streaming most of our recordings these days.
- 1:09:51If you want to be part of the show and have your comments featured on the air, be sure to subscribe to our YouTube channel at talkpython.fm/youtube.
- 1:09:59This is your host, Michael Kennedy.
- 1:10:01Thanks so much for listening.
- 1:10:02I really appreciate it.
- 1:10:03Now get out there and write some Python code.
- 1:10:04Now get out there and write some Python code.
- 1:10:05Now get out there and write some Python code.
- 1:10:06Now get out there and write some Python code.
- 1:10:07Now get out there and write some Python code.
- 1:10:11Now get out there and write some Python code.
- 1:10:11Now get out there and write some Python code.
- 1:10:13Now get out there and write some Python code.
- 1:10:15Now get out there and write some Python code.
- 1:10:17Now get out there and write some Python code.
- 1:10:19Now get out there and write some Python code.
- 1:10:24Now get out there and write some Python code.