Skip to main content
 首页 » 编程设计

Python 引用计数 : Why isn't Py_REFCNT(o) = 1

2024年10月17日10zhujiabin

我正在尝试追踪 Python C 扩展中的内存泄漏,但我无法理解为什么我的代码会提供实际的引用计数。

引用下面的代码示例,我想了解以下内容,

  1. 为什么 block 2 中的引用计数从 118 而不是 1 开始? (请注意, block 2 使用与 block 1 不同的变量。)
  2. 为什么 block 4 中的引用计数从 120 而不是 1 开始? (请注意, block 4 使用与 block 1 相同的变量名称。)
  3. 既然 block 4 中的引用计数不是从 1 开始的,为什么现在在 block 5 的开头是 1?
  4. 最后打印的数字似乎是内存地址。为什么它不打印 0 的引用计数?

这是一个简短的代码示例。 (我在 Windows 10 上使用 64 位 Python 2.7,使用 VS2012。)

// test.c : Test python reference counting 
// 
 
#include <stdlib.h>  
#include <Python.h> 
 
int main() 
{ 
    PyObject *PythonFunctionInput; 
    FILE *fid; 
 
    PyObject *tempPyObj; 
    PyObject *tempPyObjInt; 
    const int x = 1;  // This is to mimic data in my actual application 
    const int y = 1; 
    const int z = 1; 
 
 
    Py_InitializeEx(0); 
    PythonFunctionInput = PyDict_New(); 
 
    fid = fopen("TestOutputFile.txt", "wt"); 
 
    fprintf(fid,"%d\n",(int)Py_REFCNT(PythonFunctionInput));  // 1 
 
    // Block 1 
    tempPyObj = PyString_FromString("name1"); 
    fprintf(fid,"%d ",(int)Py_REFCNT(tempPyObj));  // 1 
    PyDict_SetItemString(PythonFunctionInput,"fieldname1",tempPyObj); 
    fprintf(fid,"%d ",(int)Py_REFCNT(tempPyObj));  // 2 
    Py_DECREF(tempPyObj); 
    fprintf(fid,"%d\n",(int)Py_REFCNT(tempPyObj));  // 1 
 
    // Block 2 
    tempPyObjInt = PyInt_FromLong((long)x); 
    fprintf(fid,"%d ",(int)Py_REFCNT(tempPyObjInt)); // 118  why? 
    PyDict_SetItemString(PythonFunctionInput,"fieldname2",tempPyObjInt); 
    fprintf(fid,"%d ",(int)Py_REFCNT(tempPyObjInt));  // 119 
    Py_DECREF(tempPyObjInt); 
    fprintf(fid,"%d\n",(int)Py_REFCNT(tempPyObjInt));  //118 
 
    // Block 3 
    tempPyObjInt = PyInt_FromLong((long)y); 
    fprintf(fid,"%d ",(int)Py_REFCNT(tempPyObjInt));  // 119 
    PyDict_SetItemString(PythonFunctionInput,"fieldname3",tempPyObjInt); 
    fprintf(fid,"%d ",(int)Py_REFCNT(tempPyObjInt));  // 120 
    Py_DECREF(tempPyObjInt); 
    fprintf(fid,"%d\n",(int)Py_REFCNT(tempPyObjInt));  // 119 
 
    // Block 4 
    tempPyObj = PyInt_FromLong((long)z); 
    fprintf(fid,"%d ",(int)Py_REFCNT(tempPyObj));   // 120  why? 
    PyDict_SetItemString(PythonFunctionInput,"fieldname4",tempPyObj); 
    fprintf(fid,"%d ",(int)Py_REFCNT(tempPyObj));  // 121 
    Py_DECREF(tempPyObj); 
    fprintf(fid,"%d\n",(int)Py_REFCNT(tempPyObj));  // 120 
 
    // Block 5 
    tempPyObj = PyString_FromString("name5");  
    fprintf(fid,"%d ",(int)Py_REFCNT(tempPyObj));  // 1  why? 
    PyDict_SetItemString(PythonFunctionInput,"fieldname5",tempPyObj); 
    fprintf(fid,"%d ",(int)Py_REFCNT(tempPyObj));  // 2 
    Py_DECREF(tempPyObj); 
    fprintf(fid,"%d\n",(int)Py_REFCNT(tempPyObj)); // 1 
 
    // Block 6 
    Py_DECREF(PythonFunctionInput); 
    fprintf(fid,"%d\n",(int)Py_REFCNT(PythonFunctionInput));  // 0 
    fprintf(fid,"%d ",(int)Py_REFCNT(tempPyObj));  // why not 0 ? 
 
    Py_Finalize(); 
 
    fclose(fid); 
 
    return 0; 
} 

上面的代码生成一个如下所示的文件:

1 
1 2 1 
118 119 118 
119 120 119 
120 121 120 
1 2 1 
0 
-1825751608  

我在代码中的每一行 fprintf 末尾都做了注释,以显示它显示的数字。

请您参考如下方法:

您正在打印由 PyInt_FromLong() 创建的对象的引用计数。您很惊讶它是 118(或其他一些值),而不是 1。

这是因为 Python 中的小数字可以“共享”,这意味着运行时不需要在每次创建小数字时都物理实例化一个新对象。这是为了提高效率:大多数 Python 程序创建一堆数字,例如 0、1、2、3 等,共享它们可以减少内存消耗和垃圾收集时间。