当退出python时是否释放全部内存_为什么在CPython退出时没有释放所有内存?

我想明确一点,我没有看到此问题描述的行为.相反,我的问题是关于问题本身的:

Why isn’t all memory freed when CPython exits?

并提供以下答案:

Objects referenced from the global namespaces of Python modules are

not always deallocated when Python exits. This may happen if there are

circular references. There are also certain bits of memory that are

allocated by the C library that are impossible to free (e.g. a tool

like Purify will complain about these). Python is, however, aggressive

about cleaning up memory on exit and does try to destroy every single

object.

If you want to force Python to delete certain things on deallocation

use the 07001 module to run a function that will force those

deletions.

假设是托管内存操作系统(Linux,Mac,Windows,GNU,BSD,Solaris …),这听起来很废话.

在退出程序(无论是Python还是其他任何程序)上,从操作系统请求的任何内存都将被释放(因为操作系统可以控制虚拟页表等).该程序无需取消分配或解构任何内容(程序曾经必须要做的,正如有人使用cp时获得bottlenecked by a hash table deconstruction所强调的那样),但是我认为任何OS的Python 3都不支持put程序上的此要求.

在我不知道的某些情况下,这有意义吗?这指的是什么?

解决方法:

FAQ只是说cpython本身不会在终止时主动释放其已获取的所有内存

如果您在任何正常的服务器/桌面操作系统上运行cpython,并且在退出该进程时会释放该进程的所有内存,那么内存泄漏就没有问题.当进程退出时,操作系统负责分配所有内存.

FAQ进一步通知您,cpython不会在使用malloc()或类似方法分配的所有内存上调用free()或类似方法.如果您在不退出进程退出时释放该进程获取的所有内存的操作系统上运行cpython,则可能会产生后果(存在这些操作系统,尤其是对于许多嵌入式内核而言).而且,如果您在内存事件探查器/泄漏检测器下运行cpython,该检测器可能会报告内存不为free()的泄漏.

标签:python-3-x,memory-management,python,documentation

来源: https://codeday.me/bug/20191109/2011680.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值