Hello, this is Ninomiya of LIFULL CO., LTD. Left behind last year's Advent Calendar is a regret, so I will post it though it is a small story.
During an implementation, I needed to get a rough estimate of the memory usage of a variable. As introduced in this article, sys.getsizeof
can display the size of a variable in memory.
import sys
print(sys.getsizeof({"key": "a"}))
# => 248
#The same value appears even though the font size is obviously large
print(sys.getsizeof({"key": "a" * 10000}))
# => 248
Because the substance of a Python dictionary is "a hash table that stores a reference to an object", it doesn't take into account the memory size of the strings inside. I think this article will be helpful for this story.
(I could only find good articles in Japanese for Ruby. However, the story around here is common to both Ruby and Python.)
To consider the size of the objects inside, refer to the article linked here in the Official Document. Must be implemented.
For an example of recursively using
getsizeof ()
to determine the size of a container and its contents, use Recursivesizeof
recipe [https://code.activestate.com/recipes/577504/). please refer to.
However, in my case, it is a dictionary containing only character strings (serializable to json), and I just wanted to know the approximate value, so [StackOverflow here](https://stackoverflow.com/questions/6579757/memory- Following the writing of usage-of-dictionary-in-python), I ended up with the result of json.dumps
.
>>> first = 'abc'*1000
>>> second = 'def'*1000
>>> my_dictionary = {'first': first, 'second': second}
>>> getsizeof(first)
3049
>>> getsizeof(second)
3049
>>> getsizeof(my_dictionary)
288
>>> getsizeof(json.dumps(my_dictionary))
6076
>>> size = getsizeof(my_dictionary)
>>> size += sum(map(getsizeof, my_dictionary.values())) + sum(map(getsizeof, my_dictionary.keys()))
>>> size
6495
Recommended Posts