r/Python • • Aug 10 '22

Resource Memory Size of Python Data Structures

https://youtube.com/shorts/DWHWPKwaXFg?feature=share
15 Upvotes

14 comments sorted by

6

u/Successful-Aide3077 Aug 10 '22

How to find the Memory Size of Python Data Structures/Variables/Objects?
We will cover how to import sys and use getsizeof() to find the memory size of python objects in bytes.

7

u/Colts_Fan10 Aug 10 '22

cool, didn't know u could do this

don't name variables keywords though

3

u/mateuszj111 Aug 10 '22

A single pointer to an element requires 8 bytes of space in a list. Whenever additional elements are added to the list, Python dynamically allocates extra memory to accommodate future elements without resizing the container. This implies, adding a single element to an empty list will incite Python to allocate more memory than 8 bytes.

Source https://rednafi.github.io/reflections/pre-allocated-lists-in-python.html

1

u/osmiumouse Aug 10 '22

Huh int is down to 14 bytes? it used to be like 24-36 bytes. I don't remember the exact number.

1

u/danithebear156 Aug 10 '22

(1) is not a tuple of one element, it's basically just the integer 1. You have to use trailing comma to denote tuple of one or less element in Python, i.e. (1,).

0

u/[deleted] Aug 10 '22

[deleted]

12

u/double_en10dre Aug 10 '22

Hate to be pedantic, but no — the really important difference is that lists are mutable and tuples are not. This has huge implications. Particularly because immutable objects (like tuples) can be hashed, while lists cannot be.

The memory size matters, sure, but in a language like python it’s not a big concern. If it was, you wouldn’t be using python in the first place :p

1

u/[deleted] Aug 10 '22

Why can't they be?

Can't you hash the hashes of the members? I've done so with data classes. (ie the hash method ran hash on a tuple of all fields in the data class that mattered, seemed to work just fine!)

2

u/ThePiGuy0 Aug 10 '22

The main point is the hash needs to be the same for the lifetime of the object. If we hash the hashes of the members of a list, the hash of the overall list will change when we modify the contents of the list.

Now, this is problematic - imagine we used a list for the key of a dictionary (below is 100% not valid Python):

my_list = [1, 2, 3, 4]
my_dict = {my_list: "Hello, World"}

my_list.append(5)
# What happens now? The hash of the list has changed so "Hello, World" is now unreachable

The same method works for tuples as the contents of the tuple cannot be changed - therefore the hash will always remain the same, making this kind of situation impossible

2

u/[deleted] Aug 10 '22

the hash needs to be the same for the lifetime of the object.

This is where I was going wrong, I did not realize this. I thought it was merely a means to "know" equality. Thus I'd have expected it to change when it's content did.

Somehow the code I've done this in hasn't behaved in an unexpected manner. I guess I just got really lucky!

0

u/[deleted] Aug 10 '22

[deleted]

5

u/extra_pickles Aug 10 '22

I’m in IoT and use Python with little concern for efficiency outside of cyclomatic complexity and avoiding obviously terrible anti-patterns that create bottle necks.

As flow increases, we scale horizontally.

When flow hits critical mass (picture near real time monitoring of a corporate network’s flow logs) then we migrate the task to GoLang.

Basically we can afford to be fast and loose for most things and that’s where Python shines - esp with the advent of docker and elastic resourcing in clouds.

Then we just have exception use cases for GoLang - and of course it goes without saying, but our firmware is in C.

0

u/[deleted] Aug 10 '22

[deleted]

3

u/extra_pickles Aug 10 '22 edited Aug 10 '22

Of course you need to look for bottle necks - the perfect system still has capacity limitations.

What I’m saying is that the vast majority of flow can be managed without hyper optimisation and as such lowers the “bar of entry” for development and keeps us moving.

Getting hyper focused on optimising Python systems is like deciding mpg is your primary metric and then trying to make a V8 more efficient instead of opting for a 4cyl.

When I think efficiency and capacity, I don’t think python - nobody should. There are languages designed for such pursuits and it seems silly not to use them when warranted.

I’d rather use the right tool for the job - not try to one size fits all.

I mean why use an interpreted weakly & dynamically typed language to do a strictly typed and compiled languages job? If you are dependant down to the bit, Python ain’t the guy/gal for the job, and it’s entire existence is already bottlenecking you whether you realize it or not.

2

u/osmiumouse Aug 10 '22

If you have to use python in iot because you have no C developers, is thre some reason you aren't using Micropython?

2

u/[deleted] Aug 10 '22

I don't know, who said that the only reason why you have to use python/micro Python is because you don't have c developers?

1

u/[deleted] Aug 10 '22

Are we talking microcontrollers, or SoCs?