Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

memory - Python deep getsizeof list with contents?

I was surprised that sys.getsizeof( 10000*[x] ) is 40036 regardless of x: 0, "a", 1000*"a", {}.
Is there a deep_getsizeof which properly considers elements that share memory ?
(The question came from looking at in-memory database tables like range(1000000) -> province names: list or dict ?)
(Python is 2.6.4 on a mac ppc.)

Added: 10000*["Mississippi"] is 10000 pointers to one "Mississippi", as several people have pointed out. Try this:

nstates = [AlabamatoWyoming() for j in xrange(N)]

where AlabamatoWyoming() -> a string "Alabama" .. "Wyoming". What's deep_getsizeof(nstates) ?
(How can we tell ?

  • a proper deep_getsizeof: difficult, ~ gc tracer
  • estimate from total vm
  • inside knowledge of the python implementation
  • guess.

Added 25jan: see also when-does-python-allocate-new-memory-for-identical-strings

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

10000 * [x] will produce a list of 10000 times the same object, so the sizeof is actually closer to correct than you think. However, a deep sizeof is very problematic because it's impossible to tell Python when you want to stop the measurement. Every object references a typeobject. Should the typeobject be counted? What if the reference to the typeobject is the last one, so if you deleted the object the typeobject would go away as well? What about if you have multiple (different) objects in the list refer to the same string object? Should it be counted once, or multiple times?

In short, getting the size of a data structure is very complicated, and sys.getsizeof() should never have been added :S


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...