python内存释放原则
def getInit(class_name): """动态加载模块""" resultmodule = __import__(class_name, globals(), locals(), [class_name]) resultclass = getattr(resultmodule, class_name) return resultclass() import threading, time class b: def __init__(self, *args, **kwargs): self.name = "b" self.class_name = "a" print "%s is inited" % (self.name) def __del__(self): print "%s is deleted" % (self.name) def other_run(self, obj=None): if obj: obj.run() def run(self, *args, **kwargs): obj = getInit(self.class_name) obj.run() self.other_run(obj) print "%s is run" % (self.name) def treading(): n = 0 c = b() while n<2: n += 1 print "\n" print "start" c.run() print "end" print "\n" if __name__ == '__main__': param = {} th = threading.Thread(target = treading, args = ()) th.start() exit()
登录后复制
b is inited start a is inited a is run a is run b is run a is deleted end start a is inited a is run a is run b is run a is deleted end b is deleted
登录后复制
每个循环内都把a重新加载并且释放内存
def getInit(class_name): resultmodule = __import__(class_name, globals(), locals(), [class_name]) resultclass = getattr(resultmodule, class_name) return resultclass() import threading, time import a class b: def __init__(self, *args, **kwargs): self.name = "b" self.class_name = "a" self.obj = getInit(self.class_name) print "%s is inited" % (self.name) def __del__(self): print "%s is deleted" % (self.name) def other_run(self): if self.obj: self.obj.run() def run(self, *args, **kwargs): self.obj.run() self.other_run() print "%s is run" % (self.name) def treading(): n = 0 c = b() while n<2: n += 1 print "\n" print "start" c.run() print "end" print "\n" if __name__ == '__main__': param = {} th = threading.Thread(target = treading, args = ()) th.start() exit()
登录后复制
a is inited b is inited start a is run a is run b is run end start a is run a is run b is run end b is deleted a is deleted
登录后复制
最后结束a才释放
内存释放是根据指向的
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章
刺客信条阴影:贝壳谜语解决方案
3 周前
By DDD
Windows 11 KB5054979中的新功能以及如何解决更新问题
2 周前
By DDD
在哪里可以找到原子中的起重机控制钥匙卡
3 周前
By DDD
<🎜>:死铁路 - 如何完成所有挑战
4 周前
By DDD
Atomfall指南:项目位置,任务指南和技巧
4 周前
By DDD

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题
gmail邮箱登陆入口在哪里
7655
15


CakePHP 教程
1393
52


steam的账户名称是什么格式
91
11


win11激活密钥永久
73
19


NYT迷你填字游戏答案
37
110



Linux终端中查看Python版本时遇到权限问题的解决方法当你在Linux终端中尝试查看Python的版本时,输入python...

如何在10小时内教计算机小白编程基础?如果你只有10个小时来教计算机小白一些编程知识,你会选择教些什么�...

在使用Python的pandas库时,如何在两个结构不同的DataFrame之间进行整列复制是一个常见的问题。假设我们有两个Dat...

使用FiddlerEverywhere进行中间人读取时如何避免被检测到当你使用FiddlerEverywhere...

Uvicorn是如何持续监听HTTP请求的?Uvicorn是一个基于ASGI的轻量级Web服务器,其核心功能之一便是监听HTTP请求并进�...

在Python中,如何通过字符串动态创建对象并调用其方法?这是一个常见的编程需求,尤其在需要根据配置或运行...
