Python Memory Management and Tips Transcripts
Chapter: Efficient data structures
Lecture: Monitoring memory usages for current process

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now you've already seen us use psutil, but I want to call on a specific capability that you can use while your program is running.
0:08 So we use psutil in our size utility to get the object graph closure size of the whole object, not just the base object.
0:17 And that's what we were just working with. But psutil does all kinds of cool stuff and one of the things you might
0:23 want to use it for is just asking "how much memory is my current process using right now?" If it gets over some level,
0:31 you could take some action, right? If it gets too high, let's clear out the caches and throw that away.
0:36 But instead of letting it crash or start to swap and become extremely slow,
0:40 let's just say "clear the data that can be cleared and then carry on". That doesn't
0:44 always work. You're not always in a situation where that's possible, and we're going talk about other options as well, Remember.
0:50 But if monitoring the amount of memory you're using over time is something want to do, well we've already been using the library, indirectly,
0:59 that'll help you. So this is a cross platform library for retrieving
1:03 information on running processes and how much they're using of the system. CPU, memory disk,
1:08 all kinds of stuff. So you can go crazy. And it's really good for system monitoring and
1:12 profiling, and it does things like ps and top and whatnot that you might know of from like the UNIX command monitoring tools.
1:21 The one you probably care most about, at least in the scenario that I've laid out for you,
1:26 is "How do I go and get the current working memory in Bytes?" is what we do we get the OS, get the current running id of our process and we go to
1:36 psutil, grab the process directly, and then we can ask for a memory info. A bunch of stuff in there, and the one that we want to care about is,
1:44 like the resident memory, so RSS, and print that out. That comes out and bytes. How many
1:49 bytes your current processes using. Now the OS can move this around and like it can be shared and all kinds of funky stuff,
1:56 but it'll at least give you a sense of how much memory you're using.

Talk Python's Mastodon Michael Kennedy's Mastodon