Python Memory Management and Tips Transcripts
Chapter: Investigating memory usage
Lecture: Graphing memory usage over time

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Well, having that printout and showing line by line how the memory is changing, is interesting,
0:05 but it's also useful to know how the memory changes over time. And you know, a picture is worth 1000 words,
0:12 as they say. So let's go make some pictures. What we can do is we can come down here and we can use this "mprof"
0:18 command. So mprof does a bunch of things. One, You can see is plot, and you can say list,
0:24 but the one we care about is Run, given command or Python files. So we'll say "mprof run", actually, we want to go into the right directory
0:32 first. There we go, that'll make it easier. "mprof run" this one file. Let's see what we get. Takes a moment. Sampling 10 times a second.
0:45 And it's done because of our little decorator, it prints out this stuff, but if you didn't have the decorator, you could still totally do this,
0:51 you don't need this part at all. You just get slightly different outputs. Now, PyCharm is saying "Oh, we found a new file,
0:58 maybe we should add here", and we could see what that is, as if we say "mprof list", like this. We'll say "here's the ones that we ran",
1:10 but most significantly, most relevant here, is we can say "plot". Now, very likely this will fail. Let's see what happens. No module named PyLab. Hmmm.
1:21 Well, what do we do here? Let's try this. Nope, that didn't work either. Well, maybe a better error message would be relevant here.
1:34 But what we need to do is we need to have "matplotlib" if what we're gonna do is try to graph it,
1:39 Okay? So then we can just say "pip install -r requirements" like that. Alright, with that installed, let's give it another shot. Beautiful,
2:01 beautiful! Alright, here's an interactive matplotlib that will let us zoom in and check out all sorts of stuff. Go back to the regular graph,
2:11 but here we can see it starting up. It's taking more and more memory. Here must be the completion of load_data,
2:17 here's filter_data, and here's scale_data. You can see at the top that were up here around 94.8 megabytes of memory usage.
2:27 You can do things like set the title and stuff up here, but not super relevant to us. You can use this little control bar, right?
2:34 Standard matplotlib type of things, but nonetheless, here's a cool graph, and probably the most relevant is just,
2:40 you know, save this as a PNG that you could go work on. So we can get these cool graphs and all we had to do to get them was go over here and say,
2:49 "mprof run, mprof plot". by default, it will plot the latest one, but if you have a bunch of these mprofile files laying around,
3:00 you can actually tell it which one you'd like to graph, put them side by side, all those kinds of things.


Talk Python's Mastodon Michael Kennedy's Mastodon