内存中的Python:Python引用计数指南

开发 后端
本文将会为你介绍Python引用计数,演示中使用可变列表对象,不过本文不介绍C语言实现细节。

本文转载自公众号“读芯术”(ID:AI_Discovery)

本文将会为你介绍Python引用计数,演示中使用可变列表对象,不过本文不介绍C语言实现细节。

[[328257]]

需要注意的是,代码段的输出在硬件上可能有所不同。

变量是内存引用

Python中的变量是内存引用。如果输入x = [1,2]时会发生什么?[1,2]是对象。

回想一下,一切都是Python中的对象。[1,2]将在内存中创建。x是[1,2]对象的内存引用。

来看看下面的例子。可以找到x所引用的内存地址。请务必只使用id(x),它会以10为基数,而十六进制函数会将其转换为十六进制。

 

  1. x = [1, 2] 
  2.    print(hex(id(x)))  # output: 0x32ebea8 

 

内存中的Python:Python引用计数指南

 

 

引用计数

现在已经在内存中创建了一个list对象,而且x对该对象进行了引用。那么y=[1,2]和y=x有什么区别?

当输入y=[1,2]时,它将在内存中创建一个新的list对象,并且y将引用它。

 

  1. x = [1, 2] 
  2.    y = [1, 2] 
  3.    print(hex(id(x)))  # output: 0x101bea8 
  4.    print(hex(id(y)))  # output: 0x31a5528 

而当输入y=x时,等同于告诉Python希望y 变量引用x变量引用的内容。因为变量是内存引用的。

可以确认x和y引用同一个对象。

 

  1. x = [1, 2] 
  2.    y = x 
  3.    print(hex(id(x)))  # output: 0x74bea8 
  4.    print(hex(id(y)))  # output: 0x74bea8 

 

内存中的Python:Python引用计数指南

 

 

引用计数的数目

接下来的问题是,有多少变量引用同一个对象?

错误的用法:

我看到有些人在使用sys.getrefcount(var)时不知道如何传递var,而是向对象添加引用。一起看看下面的例子。

输出3,而期望的却是2(x andy)。发生这种情况是因为将x传递给getrefcount函数时又添加了一个引用。

 

  1. from sys import getrefcount 
  2.           x = [1, 2] 
  3.           y = x 
  4.           print(hex(id(x)))  # output: 0xb65748 
  5.           print(hex(id(y)))  # output: 0xb65748 
  6.           print(getrefcount(x))  # output: 3 

更好的用法:

可以使用内置的ctypes模块来找到预期的结果。必须将x的id传递给from_address函数。

 

  1. from ctypes import c_long 
  2.       x = [1, 2] 
  3.       y = x 
  4.       print(hex(id(x)))  # output: 0x3395748 
  5.       print(hex(id(y)))  # output: 0x3395748 
  6.       print(c_long.from_address(id(x)).value)  # output: 2 

概言之,错误的用法是传递变量,而更好的用法则是传递变量的id,这意味着只传递基数为10的数字,而不是变量。

当对象消失时

当没有变量引用对象时会发生什么?

对象将从内存中删除,因为没有引用该对象的内容。不过也有例外:如果有循环引用,garbage collector 将开始奏效。

为什么使用可变对象

不可变对象由于性能原因,结果可能与预期不同。查看下面的例子,观察输出是如何变化的。

 

  1. import sys 
  2.       import ctypes 
  3.              """Some Mutable Objects """ 
  4.       a =list() 
  5.       b =set() 
  6.       c =dict() 
  7.       d =bytearray() 
  8.       """ Some ImmutableObjects """ 
  9.       e =tuple() 
  10.       f =int() 
  11.       g =str() 
  12.       print(sys.getrefcount(a),ctypes.c_long.from_address(id(a)).value)  # output: 2 1 
  13.       print(sys.getrefcount(b),ctypes.c_long.from_address(id(b)).value)  # output: 2 1 
  14.       print(sys.getrefcount(c),ctypes.c_long.from_address(id(c)).value)  # output: 2 1 
  15.       print(sys.getrefcount(d),ctypes.c_long.from_address(id(d)).value)  # output: 2 1 
  16.       print(sys.getrefcount(e),ctypes.c_long.from_address(id(e)).value)  # output: 1298 1297 
  17.       print(sys.getrefcount(f),ctypes.c_long.from_address(id(f)).value)  # output: 209 208 
  18.       print(sys.getrefcount(g),ctypes.c_long.from_address(id(g)).value)  # output: 59 58 

 

文中所谈及的一切都对CPython有效。希望对你有帮助。

 

责任编辑:华轩 来源: 读芯术
相关推荐

2020-11-10 08:45:35

Python

2023-03-26 22:48:46

Python引用计数内存

2010-03-29 09:11:02

Python引用计数

2017-10-12 12:41:11

PHP圾回收机制变量容器

2015-02-01 10:38:47

Linus并行计算

2010-08-19 09:24:41

iPhone

2020-02-09 17:23:17

Python数据字典

2021-05-27 21:47:12

Python垃圾回收

2013-08-21 10:53:46

iOS定义区别

2010-03-15 12:36:26

Python列表

2023-10-26 11:19:21

指针Go

2021-08-10 07:27:42

Python引用计数法

2021-06-28 08:00:00

Python开发编程语言

2017-11-15 19:30:08

Python内存泄露循环引用

2021-12-09 15:45:09

Python弱引用代码

2011-07-07 09:54:01

Cocoa Core Foundation

2017-07-18 11:12:39

环境设置内存分析Python

2017-02-09 21:24:22

iOS内存管理

2021-09-09 17:05:36

C++智能指针语言

2015-11-12 16:21:38

Python计数方法
点赞
收藏

51CTO技术栈公众号