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
0:03 that you can use while your program is running.
0:07 So we use psutil in our size utility to get the object graph closure
0:12 size of the whole object, not just the base object.
0:16 And that's what we were just working with.
0:18 But psutil does all kinds of cool stuff and one of the things you might
0:22 want to use it for is just asking "how much memory is my current process using
0:27 right now?" If it gets over some level,
0:30 you could take some action, right?
0:32 If it gets too high, let's clear out the caches and throw that away.
0:35 But instead of letting it crash or start to swap and become extremely slow,
0:39 let's just say "clear the data that can be cleared and then carry on". That doesn't
0:43 always work. You're not always in a situation where that's possible, and we're going talk
0:47 about other options as well, Remember.
0:49 But if monitoring the amount of memory you're using over time is
0:53 something want to do, well we've already been using the library, indirectly,
0:58 that'll help you. So this is a cross platform library for retrieving
1:02 information on running processes and how much they're using of the system. CPU, memory disk,
1:07 all kinds of stuff. So you can go crazy. And it's really good for system monitoring and
1:11 profiling, and it does things like ps and top and whatnot that you might know
1:16 of from like the UNIX command monitoring tools.
1:20 The one you probably care most about,
1:22 at least in the scenario that I've laid out for you,
1:25 is "How do I go and get the current working memory in Bytes?" is what we do
1:30 we get the OS, get the current running id of our process and we go to
1:35 psutil, grab the process directly,
1:38 and then we can ask for a memory info.
1:39 A bunch of stuff in there,
1:41 and the one that we want to care about is,
1:43 like the resident memory, so RSS, and print that out. That comes out and bytes. How many
1:48 bytes your current processes using. Now the OS can move this around and like it can
1:53 be shared and all kinds of funky stuff,
1:55 but it'll at least give you a sense of how much memory you're using.