Unexpected result from sys.getrefcount

孤街醉人 提交于 2019-12-29 07:54:30

问题


When I typed:

>>> astrd = 123
>>> import sys
>>> sys.getrefcount(astrd)
3
>>> 

I am not getting where is astrd used 3 times ?


回答1:


It's not astrd that is referenced three times, but the value 123. astrd is simply a name for the (immutable) number 123, which can be referenced however many times. Additionally to that, small integers are usually shared:

>>> astrd = 123
>>> sys.getrefcount(astrd)
4
>>> j = 123
>>> sys.getrefcount(astrd)
5

In the second assignment, no new integer is created, instead j is just a new name for the integer 123.

However, given very large integers, this does not hold:

>>> i = 823423442583
>>> sys.getrefcount(i)
2
>>> j = 823423442583
>>> sys.getrefcount(i)
2

Shared integers are an implementation detail of CPython (among others). Since small integers are instantiated very often, sharing them saves a lot of memory. This is made possible by the fact that integers are immutable in the first place.

For the additional reference in the second example, cf. codeape's answer.




回答2:


From the getrefcount docstring:

... The count returned is generally one higher than you might expect, because it includes the (temporary) reference as an argument to getrefcount().

The other two references means that python internally is holding two references to the object. Maybe the locals() and globals() dictionaries count as one reference each?




回答3:


I think it counts the references to 123, try other examples, like

>>> import sys
>>> astrd = 1
>>> sys.getrefcount(astrd)
177
>>> astrd = 9802374987193847
>>> sys.getrefcount(astrd)
2
>>> 

The refcount for 9802374987193847 fits codeape's answer.

This is probably because numbers are immutables. If you for example use a list, it will always be 2 (from a clean prompt that is).

Btw, I get 2 for 123 as well, perhaps your setup is somewhat different? Or it might be time related or so?




回答4:


ints are implemented in a special way, they are cached and shared, that why you don't get 1.

And python, uses reference counted objects. astrd is itself a reference, so you actually get the number of references to the int '123'. Try with another (user-defined) type and you'll get 1.



来源:https://stackoverflow.com/questions/759740/unexpected-result-from-sys-getrefcount

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!