How does Python memory management work?

Well, I got this class concept, which would allow other classes to import classes as a basis, and if you use it, you have to import them. How can I implement it? Or, is the Python interpreter already doing this in some way? Do they destroy classes that are not used in memory, and how to do it?

I know that C ++ / C is very memory oriented with pointers and all that, but is it Python? And I'm not saying that I have a problem with this; I, more or less, want to make changes to his project. I want to write a great program that uses hundreds of classes and modules. But I'm afraid that if I do, I will kill the application, since I do not understand how Python handles memory management.

I know this is a vague question, but if someone connects or directs me in the right direction, it will be very grateful.

+5
source share
5 answers

Python - like C #, Java, Perl, Ruby, Lua, and many other languages ​​- uses garbage collection rather than manual memory management. You simply arbitrarily create objects and the language memory manager periodically (or when you specifically direct it) searches for any objects that no longer reference your program.

So, if you want to hold an object, just keep a link to it. If you want the object to be freed up (eventually), remove any references to it.

def foo(names):
  for name in names:
    print name

foo(["Eric", "Ernie", "Bert"])
foo(["Guthtrie", "Eddie", "Al"])

foo Python list, . foo names, - , , .

+16

Python:

Python:

Exerpt: ( ):

Python , Python. Python. Python , , , , preallocation .

Python . , - . , - , , , /. , Python - , , .

, Python , . Python Python API Python/C, .

+4

5 :

  • , python ( , ). , python , int, float, string, [], {} () . , , .

  • , python " " "GC" ( , ), ( Windows ), , python . , python . python .

: http://deeplearning.net/software/theano/tutorial/python-memory-management.html

0

, python3

0
x =10
print (type(x))

(MM):  x 10

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

():  y 10

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

(MM): x points to another object - 11, the previously selected 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 divided into two parts.
    • Stack memory
    • Heap memory
  • Methods and variables are created in the stack memory.
  • The values ​​of the variable objects and instances are created in the heap memory.
  • In the memory stack, a stack frame is created whenever methods and variables are created.
  • These stack blocks are automatically destroyed; functions / methods are returned.
  • Python has a garbage collection mechanism, as soon as variables and functions return, the garbage collector cleans dead objects.
0
source

All Articles