Python Memory Management and Tips Transcripts
Chapter: Investigating memory usage
Lecture: Line level memory profiling

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Alright, let's try to use our memory profiler, or the memory profiler, on our
0:04 code. Now, I'm gonna go put that in the requirements here,
0:08 but I copied It, so I'll just paste it "memory
0:10 underscore profiler" is what we want, and
0:15 will PyCharm know that this has to be installed?
0:18 It does, beautiful. Just let it go ahead and install it.
0:21 Of course, you could do "pip install
0:22 -r requirements"
0:26 with your virtual environment activated. down over here,
0:29 we're going to just say "@profile" and import that from Memory Profiler.
0:35 Okay, that's a good start,
0:37 although we didn't get the new line that we were hoping for there.
0:39 So this is going to give us detailed information about how memory is being allocated
0:45 when we run this. So,
0:47 that's step one. Step two is to come down here,
0:51 either run in the PyCharm terminal, or we can come over here
0:55 and if you want to see it
0:56 big screen, we could run it like this.
0:59 We know that that is the full thing.
1:03 Great long thing. Now all we gotta do say "-m memory_profiler".
1:09 Run that and we wait for a second.
1:16 Alright, let's see what we got here.
1:18 So it says we started out using 12.4 megabytes to run this function,
1:22 and then this line, at this line we're using 51,
1:26 So the delta, this line here of loading data has added 38.6 megabytes to the overall
1:34 memory required. And this one we got up to 64 total, and this added 13
1:41 MB's of RAM used. We're up to 95 here,
1:45 and this added another 30 and so on.
1:49 Print out some stuff added zero,
1:51 and we just kind of stayed there.
1:53 Print f is done. Pretty cool,
1:55 right? So this gives us really detailed information.
1:59 Not just "well, now the current memory usage is such and such",
2:02 and it kind of shows you that here,
2:04 but it shows you the delta and lines it up with the source code,
2:09 even for every single line,
2:11 at least in the function we put the decorator on,
2:13 right, you could put this in multiple places.
2:16 This is cool, right? So here's a really easy way to run your code
2:20 and get this output. Now it takes longer,
2:22 like five times longer, to run with all of this analysis and all the instrumentation going
2:29 on here to make this happen.
2:31 But you do it once or twice,
2:33 you see what you get, and then you take away the profile stuff and you
2:36 just let it go normal, right?
2:38 So this is really, really cool,
2:39 and I think it's super easy to use.
2:41 You just alter the command by saying,
2:43 You know, "-m memory_profiler" and you put the decorator on it and it's
2:49 off to the races. It gives you this great output.
2:50 I'm a fan. I think this is gonna be super useful for people digging into
2:54 how much memory their program is using.