Python memory release principles
Dec 09, 2016 pm 01:41 PMdef 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()
Copy after login
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
Copy after login
Reload a and release the memory in each loop
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()
Copy after login
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
Copy after login
A is released only after the end
The memory is released according to the pointer
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot tools Tags

Hot Article
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How Do I Use Beautiful Soup to Parse HTML?

How to Use Python to Find the Zipf Distribution of a Text File

How to Work With PDF Documents Using Python

How to Cache Using Redis in Django Applications

How to Perform Deep Learning with TensorFlow or PyTorch?

Serialization and Deserialization of Python Objects: Part 1

Mathematical Modules in Python: Statistics
