In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article shares with you the content of a sample analysis of Python memory management. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
The details are as follows:
A = 1
An is a reference and 1 is an object. Python caches integers and short strings, with only one object, but long strings and other objects (list dictionaries) have many objects (assignment statements create new objects).
From sys import getrefcounta= (getfrecount (a)) print
Return 4, when a reference is passed to getfrecount as a parameter, a temporary reference is created, + 1.
Object reference object
Class from_obj (object): def _ init__ (self, to_obj): self.to_obj = to_objb = [1Mae 2jue 3] a = from_obj (b) print (id (a.to_obj)) print (id (b)) c = [BMIT b]
A refers to the object b _ ~ C references b twice.
Comb through the reference topology through the objgraph package (previously installed with xdot).
X = [1,2,3] y = [x, dict (key1=x)] z = [y, (x, y)] import objgraphobjgraph.show_refs ([z], filename='ref_topo.png')
Never refer two objects to each other or yourself, and form a reference ring to cause trouble to the garbage collection mechanism.
Garbage collection
Recycle objects with a reference count of 0. When garbage collection, python can not do other tasks, reduce efficiency, so it is not always garbage collection. Python records allocation objects (object allocation) and de-allocation objects (object deallocation), and the difference is higher than a certain threshold to start recycling.
Import gcprint (gc.get_threshold ())
(700, 10, 10) 700 is the threshold, and two 10s are generation-related thresholds, which are reset by set_threshold (). Manually enable garbage collection gc.collect ().
Generation by generation recovery
Basic assumption: the longer you live, the less likely you are to become rubbish.
The subjects were divided into three generations: 0, 1, 2 and 3. New object 0 generation, after a garbage collection is still alive to the next generation. All 0 generations must be scanned when garbage collection is started. If 0 generations pass through a certain number of garbage collections, 0 and 1 generations will be scanned. Similarly, 0 and 2 generations will be scanned. The results showed that 10 times 0 generation and 10 times 1 generation and 10 times 1 generation and 2 generations respectively.
Isolated reference ring
Originally, the reference is deleted after the two table objects reference each other, but the object reference count part 0 will not be recycled.
Then python copies each object reference count, traverses all object j referenced by object I for an object I, and subtracts gc_ref_j by 1. 0.
Thank you for reading! This is the end of this article on "sample Analysis of Python memory Management". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.