python basis -python variable memory management

Storage-related variables

Variables are stored in memory among the
definitions will apply for an independent variable in the computer's memory at runtime memory space

Small integer pool

1, in interactive mode
when there is a small Python implementation int integer pool. In order to avoid creating the same value is repeated application efficiency caused by the memory space, Python interpreter created when the starting pool small integer range [-5,256], small integer within the range of the object is global interpreter the range is reused, GC never be recovered
after creating an integer between -5 and 256, a value is taken away from the pool directly directly, e.g.
>>> = Y. 4
>>> ID (Y )
4297641184
>>>
>>> X =. 3
>>> = X +. 1
>>> ID (X)
4297641184
in the PyCharm
but the python program running PyCharm, PyCharm on performance considerations, will expand the pool of small integers range, such as a string of other immutable types are also included a deal will be the same way, we only need to remember this is an optimization mechanism, as to how much scope in the end, no careful study


Garbage collection

1. reference counting: Data in memory if there is no variable name with which they have a binding relationship, it will be automatically recovered
2. Mark Clear: When memory is about to be filled in an application, it will automatically trigger
3 points Generation recycling: Depending worth survival time, classified as different levels, the lower the garbage collection mechanism higher level of frequency scanning
personal understanding:
  the current high-level language such as java, c #, etc., have adopted the garbage collection mechanism, rather than It is c, c ++ memory management and maintenance in the user's own way. Own memory management is extremely free, you can apply for any memory, but like a double-edged sword, a large number of memory leaks, dangling pointers, etc. bug lay hidden.
  Similar to the last two days to see the Go language, it's garbage collection mechanism in place with the presence or absence of a variable garbage directly will complain, and the place of presentation, so that garbage collection is a reduction of the heavy workload and to a certain extent in terms of high-level programming language on a mechanism to increase development efficiency and productivity.
  1 is not the first bind variable relationship will naturally be recycled,
  2 variables will be marked, almost full time, python underlying operation will delete unwanted junk multi-marker
  3 on behalf of youth, the years old's size from small to large, from short to long scan time, not more scanned automatically pass a next-generation garbage.
man has a cool understand: reverse chicken
  in front of safer but also shot kill old as the state's
  late small circle, circle poison can be very dangerous, the mortality rate as high youth generations
  before mid-compromise two
  reverse situation corresponds generational recovery

Guess you like

Origin www.cnblogs.com/suren-apan/p/11374620.html