Python进阶
  • 简介
  • 序
  • 译后感
  • 原作者前言
  • args 和 *kwargs
    • *args 的用法
    • **kwargs 的用法
    • 使用 args 和 *kwargs 来调用函数
    • 啥时候使用它们
  • 调试 Debugging
  • 生成器 Generators
    • 可迭代对象(Iterable)
    • 迭代器(Iterator)
    • 迭代(Iteration)
    • 生成器(Generators)
  • Map,Filter 和 Reduce
    • Map
    • Filter
    • Reduce
  • set 数据结构
  • 三元运算符
  • 装饰器
    • 一切皆对象
    • 在函数中定义函数
    • 从函数中返回函数
    • 将函数作为参数传给另一个函数
    • 你的第一个装饰器
      • 使用场景
      • 授权
      • 日志
    • 带参数的装饰器
      • 在函数中嵌入装饰器
      • 装饰器类
  • Global和Return
    • 多个return值
  • 对象变动 Mutation
  • slots魔法
  • 虚拟环境 Virtualenv
  • 容器 Collections
  • 枚举 Enumerate
  • 对象自省
    • dir
    • type和id
    • inspect模块
  • 推导式 Comprehension
    • 列表推导式
    • 字典推导式
    • 集合推导式
  • 异常
    • 处理多个异常
      • finally从句
      • try/else从句
  • 类
  • lambda表达式
  • 一行式
  • For - Else
    • else语句
  • 使用C扩展
    • CTypes
    • SWIG
    • Python/C API
  • open函数
  • 目标Python2+3
  • 协程
  • 函数缓存
    • Python 3.2+
    • Python 2+
  • 上下文管理器
    • 基于类的实现
    • 处理异常
    • 基于生成器的实现
  • 推荐阅读
  • 捐赠名单
由 GitBook 提供支持
在本页

这有帮助吗?

slots魔法

在 Python 中,每个类都有实例属性。默认情况下 Python 用一个字典来保存一个对象的实例属性。这非常有用,因为它允许我们在运行时去设置任意的新属性。

然而,对于有着已知属性的小类来说,它可能是个瓶颈。这个字典浪费了很多内存。Python 不能在对象创建时直接分配一个固定量的内存来保存所有的属性。因此如果你创建许多对象(我指的是成千上万个),它会消耗掉很多内存。 不过还是有一个方法来规避这个问题。这个方法需要使用 __slots__ 来告诉 Python 不要使用字典,而且只给一个固定集合的属性分配空间。

这里是一个使用与不使用 __slots__ 的例子:

  • 不使用 __slots__:

class MyClass(object):
    def __init__(self, name, identifier):
        self.name = name
        self.identifier = identifier
        self.set_up()
    # ...
  • 使用 __slots__:

class MyClass(object):
    __slots__ = ['name', 'identifier']
    def __init__(self, name, identifier):
        self.name = name
        self.identifier = identifier
        self.set_up()
    # ...

第二段代码会为你的内存减轻负担。通过这个技巧,有些人已经看到内存占用率几乎40%~50%的减少。

稍微备注一下,你也许需要试一下 PyPy。它已经默认地做了所有这些优化。

Python 3.4.3 (default, Jun  6 2015, 13:32:34)
Type "copyright", "credits" or "license" for more information.

IPython 4.0.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import ipython_memory_usage.ipython_memory_usage as imu

In [2]: imu.start_watching_memory()
In [2] used 0.0000 MiB RAM in 5.31s, peaked 0.00 MiB above current, total RAM usage 15.57 MiB

In [3]: %cat slots.py
class MyClass(object):
        __slots__ = ['name', 'identifier']
        def __init__(self, name, identifier):
                self.name = name
                self.identifier = identifier

num = 1024*256
x = [MyClass(1,1) for i in range(num)]
In [3] used 0.2305 MiB RAM in 0.12s, peaked 0.00 MiB above current, total RAM usage 15.80 MiB

In [4]: from slots import *
In [4] used 9.3008 MiB RAM in 0.72s, peaked 0.00 MiB above current, total RAM usage 25.10 MiB

In [5]: %cat noslots.py
class MyClass(object):
        def __init__(self, name, identifier):
                self.name = name
                self.identifier = identifier

num = 1024*256
x = [MyClass(1,1) for i in range(num)]
In [5] used 0.1758 MiB RAM in 0.12s, peaked 0.00 MiB above current, total RAM usage 25.28 MiB

In [6]: from noslots import *
In [6] used 22.6680 MiB RAM in 0.80s, peaked 0.00 MiB above current, total RAM usage 47.95 MiB
上一页对象变动 Mutation下一页虚拟环境 Virtualenv

最后更新于3年前

这有帮助吗?

以下你可以看到一个例子,它用 IPython 来展示在有与没有 __slots__ 情况下的精确内存占用,感谢

https://github.com/ianozsvald/ipython_memory_usage