Python:Python reference counting guide in memory

Yunqi information:[ Click to see more industry information]
Here you can find the first-hand cloud information of different industries. What are you waiting for? Come on!

This article will introduce you to Python reference counting and the use of variable list objects in the demonstration, but it will not introduce the details of C language implementation.

It should be noted that the output of the code snippets may vary in hardware.

Variable is a memory reference

Variables in Python are memory references. What happens if you enter x = [1, 2]? [1, 2] is the object.

Recall that everything is an object in Python. [1, 2] will be created in memory. x is the memory reference of the [1, 2] object.

Take a look at the following example. The memory address referenced by X can be found. Be sure to use only id (x), which will base on 10, and hex functions will convert it to hex.

x = [1, 2]
   print(hex(id(x)))  # output: 0x32ebea8

Reference count

Now a list object has been created in memory, and X references it. So what's the difference between y=[1,2] and y=x?

When you enter y=[1,2], it creates a new list object in memory, and Y references it.

x = [1, 2]
  y = [1, 2]
  print(hex(id(x)))  # output: 0x101bea8
  print(hex(id(y)))  # output: 0x31a5528

When you enter y=x, it is the same as telling Python that you want y variables to refer to x variables. Because variables are memory referenced.

You can verify that x and y refer to the same object.

x = [1, 2]
   y = x
   print(hex(id(x)))  # output: 0x74bea8
   print(hex(id(y)))  # output: 0x74bea8

Number of reference counts

The next question is, how many variables refer to the same object?

Wrong usage:

I see some people using sys.getrefcount (var) does not know how to pass var, but adds a reference to the object. Let's take a look at the following example.

Output 3, but expect 2(x andy). This happens because an additional reference is added when x is passed to the getrefcount function.

from sys import getrefcount
         x = [1, 2]
         y = x
         print(hex(id(x)))  # output: 0xb65748
         print(hex(id(y)))  # output: 0xb65748
         print(getrefcount(x))  # output: 3

Better use:

You can use the built-in ctypes module to find the expected results. The id of x must be passed to from_address function.

from ctypes import c_long
      x = [1, 2]
      y = x
      print(hex(id(x)))  # output: 0x3395748
      print(hex(id(y)))  # output: 0x3395748
      print(c_long.from_address(id(x)).value)  # output: 2

In short, the wrong use is to pass variables, and the better use is to pass the id of variables, which means that only numbers with a base of 10 are passed, not variables.

When the object disappears

What happens when no variable references an object?

The object will be removed from memory because the object's contents are not referenced. But there are exceptions: if there are circular references, garbage collector will start to work.

Why use mutable objects

Immutable objects may have different results than expected due to performance reasons. Look at the example below to see how the output changes.

import sys
      import ctypes
             """Some Mutable Objects """
      a =list()
      b =set()
      c =dict()
      d =bytearray()
      """ Some ImmutableObjects """
      e =tuple()
      f =int()
      g =str()
      print(sys.getrefcount(a),ctypes.c_long.from_address(id(a)).value)  # output: 2 1
      print(sys.getrefcount(b),ctypes.c_long.from_address(id(b)).value)  # output: 2 1
      print(sys.getrefcount(c),ctypes.c_long.from_address(id(c)).value)  # output: 2 1
      print(sys.getrefcount(d),ctypes.c_long.from_address(id(d)).value)  # output: 2 1
      print(sys.getrefcount(e),ctypes.c_long.from_address(id(e)).value)  # output: 1298 1297
      print(sys.getrefcount(f),ctypes.c_long.from_address(id(f)).value)  # output: 209 208
      print(sys.getrefcount(g),ctypes.c_long.from_address(id(g)).value)  # output: 59 58

Everything in this article works for CPython. I hope it helps you.

[yunqi online class] product technology experts share every day!
Course address: https://yqh.aliyun.com/live

Join the community immediately, face to face with experts, and keep abreast of the latest news of the course!
[cloud habitat online classroom community] https://c.tb.cn/F3.Z8gvnK

Original release time: May 31, 2020
Author: core reading
This article comes from:“ Core reading ”, you can pay attention to“ Core reading"

Tags: Python C

Posted on Sun, 31 May 2020 23:50:20 -0700 by Scott_J