Python memory release principle

Source: Internet
Author: User
DefgetInit (class_name): & amp; quot; dynamic loading Module & amp; quot; resultmodule _ import _ (class_name, globals (), locals (), [class_name]) resultclassgetattr (resultmodule, class_n...
Def getInit (class_name): "dynamic loading module" 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 initedstarta is initeda is runa is runb is runa is deletedendstarta is initeda is runa is runb is runa is deletedendb is deleted

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, timeimport 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 initedb is initedstarta is runa is runb is runendstarta is runa is runb is runendb is deleteda is deleted

Release only after end


Memory is released based on

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.