Python Memory Management and Tips Transcripts
Chapter: Python variables and memory
Lecture: The *real* size of objects
0:00 To answer the question of how much memory do these things actually use in the way
0:03 that I describe you traversing the object graph,
0:05 We're going to use a cool foundational library:
0:08 "psutil" and I said we didn't need the virtual environment right away.
0:11 Well, I forgot about this one,
0:12 so we need it right away.
0:13 We're going to pip install that one.
0:15 You can see PyCharm if I just click This will do that for me.
0:19 I added this requirements file, now I put it in there.
0:24 Alright, that's all happy, but what we're gonna do is we're going to write, or take this code over here that
0:29 basically will do that traversal, so give me the size,
0:33 and it'll just recursively go looking and say,
0:36 "is that a dictionary? Does it have a __dict__?
0:39 As in it's a class. Is it a thing that can be iterated"? and so
0:43 on. And it's just going to recursively dive into those things.
0:47 Okay? So we can use this to traverse that object graph over here,
0:53 if we just import it. Looks like it will import right?
1:01 But no, it doesn't. Even though you can be assured that is what I
1:05 called it "size_util". And the reason is PyCharm is looking for something up
1:09 in this folder called "size_util".
1:10 So what I am going to do is I'm going over here and tell
1:14 PyCharm "also look in this directory as a like a top level directory
1:18 for imports" and the way you do that,
1:19 is you say mark directory as sources route,
1:22 and we're gonna need that for the others,
1:24 potentially as well. Here we go.
1:27 Oh look, now it works. So, what we can do is we can do the this and
1:33 say "get full size of that" and let's do it for the "a" and let's
1:41 do it for "26 a's". Now, those things don't have stuff they contain,
1:46 so these numbers are likely to be the same.
1:51 And they are "28 28 50 50 75 75",
1:55 but where it gets interesting is where you have container objects.
1:59 So if we just put the list in because the List is empty it's probably the
2:07 same size. How big is the list?
2:08 It's still 56. Alright, so those are the same size,
2:13 but here's where it's going to get interesting, where we look at the size of something
2:18 that points to or contains other things that are not empty,
2:22 basically. Look at this one: the 10 items instead of being the 136,
2:27 if you take into account all the stuff out there,
2:30 so the other 280 bytes that you're gonna get from these,
2:34 then that's how big it is,
2:36 okay? And then here's the one where it was a really out of whack.
2:40 Where It was really crazy in terms of,
2:42 we know we allocated tons of memory and yet it said 184. Not likely.
2:49 So let's go get the full size of this data list that we've created.
2:54 There we go. It probably would be better,
2:57 even if we go with this way little comma digit grouping there 31 basically 32 kilobytes for
3:08 this beast that I created here this geometric growing list of lists.
3:13 So if we're going to understand the amount of memory of these objects
3:16 take, we gotta look at all the container objects,
3:19 the things that they contain and recursively do that, right?
3:22 If there's container objects that are contained,
3:23 look at their things that are contained and so on.
3:25 And that's what this little utility does,
3:27 and we're gonna be using this throughout much of the course.
3:31 Do not use this sys socket "getsizeof" which is great for the relative size
3:36 of the like, the core essence of the thing,
3:39 but not the object graph that it relates to,
3:42 and that's what this size utility is all about.