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
0:02 how the memory is changing, is interesting,
0:04 but it's also useful to know how the memory changes over time. And
0:09 you know, a picture is worth 1000 words,
0:11 as they say. So let's go make some pictures.
0:13 What we can do is we can come down here and we can use this "mprof"
0:17 command. So mprof does a bunch of things.
0:20 One, You can see is plot, and you can say list,
0:23 but the one we care about is Run, given command or Python files. So we'll say "mprof
0:28 run", actually, we want to go into the right directory
0:31 first. There we go, that'll make it easier.
0:33 "mprof run" this one file.
0:37 Let's see what we get. Takes a moment. Sampling 10 times a second.
0:44 And it's done because of our little decorator,
0:46 it prints out this stuff, but if you didn't have the decorator,
0:49 you could still totally do this,
0:50 you don't need this part at all.
0:52 You just get slightly different outputs. Now,
0:55 PyCharm is saying "Oh, we found a new file,
0:57 maybe we should add here", and we could see what that is, as if we
1:02 say "mprof list", like this.
1:06 We'll say "here's the ones that we ran",
1:09 but most significantly, most relevant here,
1:11 is we can say "plot". Now, very likely this will fail.
1:15 Let's see what happens. No module named PyLab. Hmmm.
1:20 Well, what do we do here?
1:23 Let's try this. Nope, that didn't work either.
1:29 Well, maybe a better error message would be relevant here.
1:33 But what we need to do is we need to have "matplotlib"
1:37 if what we're gonna do is try to graph it,
1:38 Okay? So then we can just say "pip install -r requirements" like that.
1:53 Alright, with that installed,
1:54 let's give it another shot. Beautiful,
2:00 beautiful! Alright, here's an interactive matplotlib that will let us zoom in
2:05 and check out all sorts of stuff.
2:08 Go back to the regular graph,
2:10 but here we can see it starting up.
2:11 It's taking more and more memory.
2:13 Here must be the completion of load_data,
2:16 here's filter_data, and here's scale_data. You can see at the top that
2:20 were up here around 94.8 megabytes of memory usage.
2:26 You can do things like set the title and stuff up here, but not super relevant to
2:30 us. You can use this little control bar, right?
2:33 Standard matplotlib type of things,
2:36 but nonetheless, here's a cool graph,
2:37 and probably the most relevant is just,
2:39 you know, save this as a PNG that you could go work on. So we
2:43 can get these cool graphs and all we had to do to get them
2:47 was go over here and say,
2:48 "mprof run, mprof plot". by default,
2:52 it will plot the latest one,
2:54 but if you have a bunch of these mprofile files laying around,
2:59 you can actually tell it which one you'd like to graph,
3:01 put them side by side, all those kinds of things.