-1

I use two consoles to run python script. One is cmd console, and another is pycharm console. What surprises me is that, the results seem the two different consoles share the same memory space. The detail is as follow pictures shown. There are four phenomena, with which I am confused.

Phenomenon 1: variables in different consoles allocated in the same address

In cmd console, I create three variables, 'a'(the address is 0x7ffaf8346290), 'b'(the address is 0x7ffaf83462b0), and 'c'(the address is 0x7ffaf83462d0).

In pycharm console, I create two variables, 'a'(the address is 0x7ffaf8346290) and 'b'(the address is 0x7ffaf83462b0).

Phenomenon 2: variables in the same address, cannot be accessed by two consoles

'c' is pointing to the address 0x7ffaf83462d0 in cmd console. In pycharm console, the address of 3 is 0x7ffaf83462d0, but 'c' cannot be accessed.

Phenomenon 3: variables created by one console, are affected by the another console allocate memory

In pycharm console, I create two other variables c=5(the address is 0x7ffaf83462f0) and d=6(the address is 0x7ffaf8346310).

Interestingly, in cmd console, the address of 5is 0x7ffaf8346310, same as the address in pycharm console. And the address of 6is 0x7ffaf8346330 with sequential growth.

Phenomenon 4: list object seems to be not affected

I create two list [1,2,3,4] in the two consoles respectively, and the addresses of two seems separate from each other.

cmd console pycharm console

Franco
  • 123
  • 11

2 Answers2

1

Your two Python sessions aren't affecting each other at all. They do not share memory in any way. Modern operating systems use virtual memory, where each process has its own mapping of virtual memory addresses to physical RAM (or files or other weird things that can be memory-mapped). This means that two processes can use the same virtual memory address for different things at the same time without affecting each other.

ID values are only guaranteed to be distinct for different objects in the same process, with overlapping lifetimes.

user2357112
  • 260,549
  • 28
  • 431
  • 505
  • This may be a good explanation. However, I am also confused with that, if `id()` only guaranteed to be distinct for different objects in the same process, the phenomenon 3 is just a coincidence? – Franco Apr 06 '20 at 12:57
  • @Franco: Phenomenon 3 is an artifact of a memory allocation optimization CPython performs for small integers; there's a statically allocated array of int objects, a single contiguous block of memory, and when a small int is needed, it's usually drawn from there. This implementation detail is set to change a bit in CPython 3.9. You should never write code in a way where this actually matters. – user2357112 Apr 06 '20 at 13:01
0

Most probably, your two consoles use one Python engine. The Python engine runs one virtual machine that is responsible for memory allocation and manipulation. You can read up the Python basics elsewhere, try googling Python Virtual Machine (PVM).

s0mbre
  • 361
  • 2
  • 14
  • This may be one possibility. However, in cmd console, list object behavior is different from that in pycharm console. In cmd consle list objects are allocated in sequential growth. But in pycharm console, list will allocated not sequential with a grand skip. – Franco Apr 06 '20 at 12:47