How Does Python Memory Management Work?

后端 未结 5 1309
耶瑟儿~
耶瑟儿~ 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 22:35

    Python -- like C#, Java, Perl, Ruby, Lua and many other languages -- uses garbage collection rather than manual memory management. You just freely create objects and the language's memory manager periodically (or when you specifically direct it to) looks for any objects that are no longer referenced by your program.

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

    def foo(names):
      for name in names:
        print name
    
    foo(["Eric", "Ernie", "Bert"])
    foo(["Guthtrie", "Eddie", "Al"])
    

    Each of these calls to foo creates a Python list object initialized with three values. For the duration of the foo call they are referenced by the variable names, but as soon as that function exits no variable is holding a reference to them and they are fair game for the garbage collector to delete.

    0 讨论(0)
  • 2020-12-23 22:38

    Yes its the same behaviour in python3 as well

    0 讨论(0)
  • 2020-12-23 22:47

    My 5 cents:

    1. most importantly, python frees memory for referenced objects only (not for classes because they are just containers or custom data types). Again, in python everything is an object, so int, float, string, [], {} and () all are objects. That mean if your program don't reference them anymore they are victims for garbage collection.

    2. Though python uses'Reference count' and 'GC' to free memory (for the objects that are not in used), this free memory is not returned back to the operating system (in windows its different case though). This mean free memory chunk just return back to python interpreter not to the operating system. So utlimately your python process is going to hold the same memory. However, python will use this memory to allocate to some other objects.

    Very good explanation for this given at: http://deeplearning.net/software/theano/tutorial/python-memory-management.html

    0 讨论(0)
  • 2020-12-23 22:53

    Read through following articles about Python Memory Management :

    Python : Memory Management (updated to version 3)

    Exerpt: (examples can be found in the article):

    Memory management in Python involves a private heap containing all Python objects and data structures. The management of this private heap is ensured internally by the Python memory manager. The Python memory manager has different components which deal with various dynamic storage management aspects, like sharing, segmentation, preallocation or caching.

    At the lowest level, a raw memory allocator ensures that there is enough room in the private heap for storing all Python-related data by interacting with the memory manager of the operating system. On top of the raw memory allocator, several object-specific allocators operate on the same heap and implement distinct memory management policies adapted to the peculiarities of every object type. For example, integer objects are managed differently within the heap than strings, tuples or dictionaries because integers imply different storage requirements and speed/space tradeoffs. The Python memory manager thus delegates some of the work to the object-specific allocators, but ensures that the latter operate within the bounds of the private heap.

    It is important to understand that the management of the Python heap is performed by the interpreter itself and that the user has no control over it, even if she regularly manipulates object pointers to memory blocks inside that heap. The allocation of heap space for Python objects and other internal buffers is performed on demand by the Python memory manager through the Python/C API functions listed in this document.

    0 讨论(0)
  • 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.
    0 讨论(0)
提交回复
热议问题