SoFunction
Updated on 2024-12-10

Variables and memory usage in python

I. Structural relationship between variables and memory

Python's variables act similarly to tags; content is scattered throughout memory, and for ease of management, variables are used to label this in-memory content.

As shown in the figure below:

In general, the memory structure of python is 64 bytes, so if you declare the variable abcdef in python and assign it a value, the corresponding memory structure will be roughly like this:

Something to keep in mind:

The allocated memory may be contiguous, with the next memory being 64 bytes more than the previous one, or it may be non-contiguous, being a multiple of 64 bytes.

In this way it can be seen that the variable a or b is just a label pointing to the actual content, making it easy for humans to recognize what they represent.

What happens if the value pointed to by a is assigned to b,and the string pointed to by e is assigned to f .

This can be represented in the figure below:

The labels a and b point to the actual content 0x12345678, e and f point to the actual content "dog", for the value 0x98981234 at address 0x16a0c466650 still exists at this address, there is just no variable pointing to it for the time being (until it is reclaimed by the system), and the string "cat" represented at address 0x16a788e32f0 also has no variable pointing to it. 0x16a788e32f0 also has no variable pointing to the string "cat".

II. Memory representation of variables

The memory representation of a variable is simple, id() can represent the memory in decimal, to represent it in hexadecimal, add hex to it

The addresses pointed to by variables a and b in the first figure are, respectively

  • >>>hex(id(a))
  • '0x16a0c466610'
  • >>>hex(id(b))
  • '0x16a0c466650'

In the second figure variables a and b point to the same memory address

  • >>>hex(id(a))
  • '0x16a0c466610'
  • >>>hex(id(b))
  • '0x16a0c466610'

III. Variables for viewing memory addresses

You can use string_at in ctypes, getsizeof in sys, and hexlify in binascii.

# -*- coding: utf-8 -*-
from ctypes import string_at
from sys import getsizeof
from binascii import hexlify
 
a = 0x12345678
print(hexlify(string_at(id(a), getsizeof(a))))

Print out the code:

b'0300000000000000d00fcd4dfb7f0000010000000000000078563412'

Here you see 78563412 at the end, which is equivalent to 12, 34, 56, 78 bytes in reverse order.

From this we realize that the representation of data in memory is somewhat different from the representation of our variables.

summarize

The above is a personal experience, I hope it can give you a reference, and I hope you can support me more.