"Python deep 1" memory management

Source: Internet
Author: User

1) Dynamic type

1.1) Core Ideas

All objects in Python. As a "dynamic language", Python follows the core idea of "object and reference separation."

1.2) All Objects

Common variables, such as integers, strings, lists, and so on, everything in Python is object a = 1 integer 1 is an object and is an entity stored in memory. Object name A is a reference, and we cannot directly touch the "object entity", which can only be accessed by referring to a new object at any point.

1.3) Assignment Operation: mutable object, immutable object

Immutable Data Objects: Can not change the object itself, can only change the point of reference, such as shaping, string, ganso, such as a = 5b = AA = A + 2 Resolution: At this point a = 7,b = 5. Verify that immutable objects can only change the point of reference, and each reference is independent from each other. Mutable Data objects: referencing their elements (like L[0]), change the object itself, such as list, dictionary, etc. L1 = [1,2,3]L2 = l1l1[0] = 100 parsing: At this time L1 = L2 = [100,2,3]. A mutable object can be modified by referencing its elements, changing the object itself

1.4) Parameter transfer of function from dynamic type

def f (x): x = $ print X a = 1f (a) print a resolves: parameter x is a new reference to the object referred to by a. Here is an immutable object, where references a and x are independent of each other. The operation of the parameter x does not affect the reference a.def f (x): X[0] = print x a = [1,2,3]f (a) print a parsing: referring to the previous example, here is a Variable object argument, you can change the object by referencing its elements. So both A and X are [100,2,3]

1.5) Garbage collection

Python takes a simple garbage collection mechanism: the reference count (from sys import getrefcount ()). Python cannot perform other tasks when garbage collection Frequent garbage collection can greatly reduce python productivity. As a result, Python automatically starts the garbage collection mechanism under certain circumstances. View garbage collection thresholds from the GC module. Import Gcprint gc.get_threshold () output ( 700,10,10) Resolution: 700 is the threshold for garbage collection: Number of objects allocated in memory-number of unassigned objects = 700; can be set by Set.threshold (). Manually start the garbage collection mechanism: Gc.collect ()

1.6) Generation of recycling

The longer the object, the more likely it is to be trusted and efficient, the more unlikely it is to become garbage in later programs, so less garbage collection scans their frequency. Python divides all the objects into three generations of 0,1,2. All new objects are 0-band objects. When a garbage collection is made, it still survives, Then it is classified as a next-generation object. Reference 1.5, the output contains 2 10, respectively: every 10 times 0 generations of object garbage collection, 1 generations of object garbage collection; 1 generations of object garbage collection 10 times 1 generations of object garbage collection we should avoid frequent scans of 1 generation objects at all times: Gc.set_threshold (700,10,20)


"Python deep 1" memory management

Related Article

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.