Memory occupied by basic type
- type
-
# -*- coding: utf-8 -*-
# @Time : 2019-12-19 11:16
# @Author : binger
import sys
a = None
b = 1000.2311
c = 1000
d = True
e = ""
f = ()
g = []
h = set([])
i = {}
print(" %s size is %d " % (type(a), sys.getsizeof(a)))
print(" %s size is %d " % (type(b), sys.getsizeof(b)))
print(" %s size is %d " % (type(c), sys.getsizeof(c)))
print(" %s size is %d " % (type(d), sys.getsizeof(d)))
print(" %s size is %d " % (type(e), sys.getsizeof(e)), sys.getsizeof("12"))
print(" %s size is %d " % (type(f), sys.getsizeof(f)), sys.getsizeof((1,)))
print(" %s size is %d " % (type(g), sys.getsizeof(g)), sys.getsizeof([1, ]))
print(" %s size is %d " % (type(h), sys.getsizeof(h)), sys.getsizeof(set([1, ])))
print(" %s size is %d " % (type(i), sys.getsizeof(i)), sys.getsizeof({1: 1}))
- Result:
Memory size ranking: none < float < int = bool < STR < tuple < list < set < Dict
-
<class 'NoneType'> size is 16 Byte
<class 'float'> size is 24 Byte
<class 'int'> size is 28 Byte
<class 'bool'> size is 28 Byte
<class 'str'> size is 49 Byte 51
<class 'tuple'> size is 56 Byte 64
<class 'list'> size is 72 Byte 80
<class 'set'> size is 232 Byte 232
<class 'dict'> size is 248 Byte 248
Analysis:
- int and float:
- Python 3 memory consumption in 64 bit systems of int and float: int > float
- The overhead cost (pyobject · head) has doubled, but the size of the integer has changed from 32-bit to 64 bit, while the size of the floating-point number (double precision) is still 64:
-
32 place
int: cost = 10 bytes, value = 4 bytes
float: cost = 8 bytes, value = 8 bytes
64 place
int: cost = 20 bytes, value = 8 bytes
float: cost = 16 bytes, value = 8 bytes
-
- Int and float are both 24B in 64 bit Python 2. But int does not contain long type (28B)
- Dictionary and linked list
from bintrees import bintree
import uuid, time, sys
import random
def create_uuid(msg):
src_uuid = uuid.uuid4()
name = "{}{}".format(time.time(), msg)
return uuid.uuid3(src_uuid, name=name).hex
a = {create_uuid(i): random.randint(0, 10) for i in range(2000)}
b = {i: i for i in range(2000)}
ring = bintree.BinaryTree()
c = [ring.insert(create_uuid(i), i) for i in range(2000)]
ring2 = bintree.BinaryTree()
d = [ring2.insert(i, i) for i in range(2000)]
print("Dictionary 1", sys.getsizeof(a))
print("Dictionary 2", sys.getsizeof(b))
print("Two fork tree:", sys.getsizeof(c))
print("Two fork tree:", sys.getsizeof(d))
Dictionary 1 73832
Dictionary 2 73832
Binary tree: 16568
Binary tree: 16568