Python Forum
Do lists of classes take up more space in memory?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Do lists of classes take up more space in memory?
#1
I'm going through refactoring and cleaning up my code.

At the moment, I have data written to a file to store it, then later on I take the file and create two lists out of the data.
One list is multiple lists of 3 rgb values:
rgb = [[55,35,133],[54,75,34],[,122,186,55],[42,134,5],[22,4,145]...]
and one is a list of absolute paths to the images.
Each are 30,000 items longs.

As I'm refactoring, to remove the use of any files, and only have one list, I thought about creating a simple class like this:
class CustomImage(): #my custom image class
    def __init__(self, name, path, size, rgb, ext="png"):
        self.name = name
        self.path = path
        self.size = size
        self.ext = ext
        self.avg = rgb
    @property
    def rgb(self):
        return self.avg
    @property
    def size(self):
        return self.size
    @property
    def path(self):
        return self.path
    @property
    def name(self):
        return self.name
    @property
    def ext(self):
        return self.ext
Once I have downloaded an image, I simply create a "CustomImage" out of that image, which I can easily use later in my code. I would store all the these "CustomImages" in a list (also of 30,000 items)

What would be most likely to take up more memory? I would test it but I'm not sure about how I can measure list sizes in memory.

Found out how to test it:
The two lists take up a total of 507,248 bytes in memory.

The class, which has become:
class CustomImage(): #my custom image class
    def __init__(self, name, path, size, rgb, ext="png"):
        self.name = name
        self.path = path
        self.size = size
        self.ext = ext
        self.avg = rgb
takes up only 253,624 bytes in an array of 30,000
Reply
#2
Since each image always has the same attributes, you could save even more space using slots. Classes that use slots instead of __dict___ provide faster access and have a smaller memory footprint.
class CustomImage(): #my custom image class
    __slots__ = 'name', 'path', 'size', 'ext', 'avg'
    def __init__(self, name, path, size, rgb, ext="png"):
        self.name = name
        self.path = path
        self.size = size
        self.ext = ext
        self.avg = rgb
With slots you cannot add an attribute to the class. You can access or set attribute values, but I could not do custimg.rating = 4 because there is no slot for "rating".
If CustomImage is immutable you could also use a named tuple (import collections).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using my REPL to bisect numbers and lists with classes (PyBite #181) Drone4four 2 2,032 Sep-24-2020, 01:47 PM
Last Post: Drone4four
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,361 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  from global space to local space Skaperen 4 2,304 Sep-08-2020, 04:59 PM
Last Post: Skaperen
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 3,254 Mar-20-2019, 08:01 PM
Last Post: stillsen
  How to reallocating memory space in multithreading program in python ? siva 2 3,390 Mar-08-2018, 03:49 AM
Last Post: siva
  Using classes? Can I just use classes to structure code? muteboy 5 5,033 Nov-01-2017, 04:20 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020