From the course: Advanced Python: Object-Oriented Programming

Unlock the full course today

Join today to access over 24,700 courses taught by industry experts.

Reducing memory with __slots__

Reducing memory with __slots__

- [Instructor] In this video, we are going to see how you can reduce memory with dunder slots. Python has a dunder slots and you can edit to a class to reduce memory. Remember that Python usually saves the attributes in the dictionary, which is geared towards fast access but not towards saving memory. And sometimes when you have a lot of small objects, this can be significant. We are going to check out the memory using the Trace model library. Let's see. So let's say I have a car which has an ID and latitude and longitude, the location of the car. So let's run this one and also hide the files. And now what we're going to do is we're going to start with importing tracemalloc and random. We're going to create random latitude and random longitude. And I have an mb, which is a utility function to show something in megabytes and not in bytes. So the size is 100,000. And what we're going to do is we're going to start tracemalloc, create a list of 100,000 cars, and then we are going to ask…

Contents