How Does Python Memory Management Work?

后端 未结 5 1330
耶瑟儿~
耶瑟儿~ 2020-12-23 22:22

Okay, I got this concept of a class that would allow other classes to import classes on as basis versus if you use it you must import it. How would I go about implementing i

5条回答
  •  自闭症患者
    2020-12-23 23:00

    x =10
    print (type(x))
    

    memory manager (MM): x points to 10

    y = x
    if(id(x) == id(y)):
            print('x and y refer to the same object')
    

    (MM): y points to same 10 object

    x=x+1
    if(id(x) != id(y)):
        print('x and y refer to different objects')
    

    (MM): x points to another object is 11, previously pointed object was destroyed

    z=10
    if(id(y) == id(z)):
        print('y and z refer to same object')
    else:
        print('y and z refer different objects')
    
    • Python memory management is been divided into two parts.
      1. Stack memory
      2. Heap memory
    • Methods and variables are created in Stack memory.
    • Objects and instance variables values are created in Heap memory.
    • In stack memory - a stack frame is created whenever methods and variables are created.
    • These stacks frames are destroyed automaticaly whenever functions/methods returns.
    • Python has mechanism of Garbage collector, as soon as variables and functions returns, Garbage collector clear the dead objects.

提交回复
热议问题