Python Memory Management and Tips Transcripts
Chapter: Allocating memory in Python
Lecture: Allocation blocks

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's start at the lowest level where the actual objects are stored in memory. Instead of
0:05 allocating as we saw at the very beginning of this chapter 17 bytes or 20 bytes or
0:11 whatever you need exactly for a thing just randomly where you've got a gap in your
0:16 memory, Python uses these things called blocks.
0:20 Blocks are chunks of memory of a certain size,
0:23 and each block is designated to hold objects of a certain size.
0:29 So, for example, we might define a block that holds objects of 24 bytes,
0:34 or around 24 bytes, let's say. The places where the objects go are 24 bytes
0:41 and anything that's between 17 to 24 bytes is allocated into those 24 byte spaces.
0:48 And sure, if you've only got 20 bytes you need and you stick it into a 24 byte
0:52 spot, you're wasting, quote "wasting" 4 bytes,
0:57 right? You could have packed it a little bit tighter.
0:59 But this algorithm allows Python
1:01 to create these sets of memory,
1:03 that it's really easy to allocate stuff into once
1:06 it's freed up, just un-assign it,
1:08 but not give it back to the operating system,
1:10 necessarily. Then when you want to allocate something new,
1:12 maybe next time it's 22 bytes,
1:14 you can use that same little spot and reuse it.
1:17 Okay, so that's the idea of these blocks.
1:20 And there's some rules. One of the rules is it only holds, each block only
1:26 holds things that fit into its block.
1:28 So if you've got one, that's the size for a 24 byte element, things of 17
1:34 to 24 bites go in there,
1:35 but they kind of waste the space if they don't totally fit and you
1:39 can see they're broken into these different categories.
1:42 So once of block is allocated,
1:44 it's always dedicated to its size.
1:45 Its either a bunch of stuff that fits into 24 byte pieces or 16 byte pieces
1:51 and so on. So you can think of Python allocating these blocks,
1:55 for of the different size of allocation it's going to do and then be able to
1:58 just reuse that memory. It doesn't have to go back to the operating system,
2:02 free up memory, ask for more memory,
2:04 get that fragmented on in RAM and things like that.
2:07 It can get a whole bunch of space for those pieces of those small objects that
2:12 it needs and just works with it internally,
2:15 and it's more efficient that way.