Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Variable memory size

I am trying to save timeseries data into MongoDB using pymongo. However the average object size is 14 bytes, while I need to store it in 4 bytes.

The issue is Python pre-allocates variables to 24 bytes (or based on the variable size, larger) , and after conversion in pymongo is a bloated collection.

For example :

x=1
sys.getsizeof(x)

24 Bytes

x=1000**1000
sys.getsizeof(x)

1356 Bytes

The above example is an illustration, I am using smaller sizes, typically 32 bit for voltages current etc How can I ensure 4 byte fit to python variables and a smaller mongoDB timeseries collection

like image 573
Shuvashish Chatterjee Avatar asked Sep 16 '26 00:09

Shuvashish Chatterjee


1 Answers

The runtime size of the Python object is mostly irrelevant here - specially for small integers - since what get stored is a bson representation of the object not - obviously - the Python object itself.

like image 108
bruno desthuilliers Avatar answered Sep 18 '26 12:09

bruno desthuilliers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!