Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
535 views
in Technique[技术] by (71.8m points)

python - size of nested dictionary and list

I have dictionary with following format:

Demo code:

>>> import pprint
>>> pprint.pprint(data)
{'lookup': {'F01': '
.custom1 {
    background-color: #f5e9dc;
    padding: 10px;
    border-radius: 10px;
    font-family: sans-serif;
    font-size: 0.9em;
    margin-top: 1em;
    }
.custom2 .style8-rw {
    font-family: sans-serif;
    font-weight: bold;
    color: #F57215;
    }',
            'F02': '
.custom1 {
    background-color: #f5e9dc;
    padding: 10px;
    border-radius: 10px;
    font-family: sans-serif;
    font-size: 0.9em;
    margin-top: 1em;
    }
.custom2 .style8-rw {
    font-family: sans-serif;
    font-weight: bold;
    color: #F57215;
    }',
            'F03': '
.custom1 {
    background-color: #f5e9dc;
    padding: 10px;
    border-radius: 10px;
    font-family: sans-serif;
    font-size: 0.9em;
    margin-top: 1em;
    }
.custom2 .style8-rw {
    font-family: sans-serif;
    font-weight: bold;
    color: #F57215;
    }',
            'F04': '
.custom1 {
    background-color: #f5e9dc;
    padding: 10px;
    border-radius: 10px;
    font-family: sans-serif;
    font-size: 0.9em;
    margin-top: 1em;
    }
.custom2 .style8-rw {
    font-family: sans-serif;
    font-weight: bold;
    color: #F57215;
    }',
            'F05': '
.custom1 {
    background-color: #f5e9dc;
    padding: 10px;
    border-radius: 10px;
    font-family: sans-serif;
    font-size: 0.9em;
    margin-top: 1em;
    }
.custom2 .style8-rw {
    font-family: sans-serif;
    font-weight: bold;
    color: #F57215;
    }',
            'F06': '
.custom1 {
    background-color: #f5e9dc;
    padding: 10px;
    border-radius: 10px;
    font-family: sans-serif;
    font-size: 0.9em;
    margin-top: 1em;
    }
.custom2 .style8-rw {
    font-family: sans-serif;
    font-weight: bold;
    color: #F57215;
    }',
            'F07': '
.custom1 {
    background-color: #f5e9dc;
    padding: 10px;
    border-radius: 10px;
    font-family: sans-serif;
    font-size: 0.9em;
    margin-top: 1em;
    }
.custom2 .style8-rw {
    font-family: sans-serif;
    font-weight: bold;
    color: #F57215;
    }',
            'F08': '
.custom1 {
    background-color: #f5e9dc;
    padding: 10px;
    border-radius: 10px;
    font-family: sans-serif;
    font-size: 0.9em;
    margin-top: 1em;
    }
.custom2 .style8-rw {
    font-family: sans-serif;
    font-weight: bold;
    color: #F57215;
    }',
            'F09': '
.custom1 {
    background-color: #f5e9dc;
    padding: 10px;
    border-radius: 10px;
    font-family: sans-serif;
    font-size: 0.9em;
    margin-top: 1em;
    }
.custom2 .style8-rw {
    font-family: sans-serif;
    font-weight: bold;
    color: #F57215;
    }',
            'F10': '
.custom1 {
    background-color: #f5e9dc;
    padding: 10px;
    border-radius: 10px;
    font-family: sans-serif;
    font-size: 0.9em;
    margin-top: 1em;
    }
.custom2 .style8-rw {
    font-family: sans-serif;
    font-weight: bold;
    color: #F57215;
    }',
            'F11': '
.custom1 {
    background-color: #f5e9dc;
    padding: 10px;
    border-radius: 10px;
    font-family: sans-serif;
    font-size: 0.9em;
    margin-top: 1em;
    }
.custom2 .style8-rw {
    font-family: sans-serif;
    font-weight: bold;
    color: #F57215;
    }',
            'F12': '
.custom1 {
    background-color: #f5e9dc;
    padding: 10px;
    border-radius: 10px;
    font-family: sans-serif;
    font-size: 0.9em;
    margin-top: 1em;
    }
.custom2 .style8-rw {
    font-family: sans-serif;
    font-weight: bold;
    color: #F57215;
    }'},
 'sequence': ['F01',
              'F02',
              'F03',
              'F04',
              'F05',
              'F06',
              'F07',
              'F08',
              'F09',
              'F10',
              'F11',
              'F12']}
>>> import sys
>>> sys.getsizeof(data)
136
>>> sys.getsizeof(data["sequence"])
80
>>> sys.getsizeof(data["lookup"])
520
>>> 

I can not get how nested dictionary is store in memory because size if data is 136 bytes and size of data["sequence"] is 80 bytes and also size of data["lookup"] is 520 bytes.

Also when I do type casting of variable data from dictionary to string then size of string variable is 3587 bytes.

Demo Code:

>>> data_str = str(data)
>>> sys.getsizeof(data_str)
3587

Can any on explain me why?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Dictionaries and lists store references (like every other standard container in Python). sys.getsizeof() doesn't follow references, it gives you the memory footprint of the C structure only. The references are C pointers; their size depends on your specific platform.

Converting a dictionary to a string recursively converts the contents to (repr()) strings too, so all those references are then dereferenced and included in the output. Note that this is not an accurate reflection of the memory size for the original object; strings contain characters, it depends on your exact Python version, OS and range of Unicode codepoints used how much memory each character takes, and the character count has a non-linear relationship to the actual object being reflected.

If you want to know the memory footprint of a dictionary with the contents you need to do so recursively. Take into account that a dictionary can contain references to itself (directly or indirectly), or that any object can have multiple references to it and should only be counted once. I'd use the id() function to track what objects have already been handled.

There are already several posts on Stack Overflow that discuss calculating memory size for containers using recursion or other tools, see Deep version of sys.getsizeof, Python deep getsizeof list with contents?, and Memory-usage of dictionary in Python? for some examples.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...