Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Does Python Memory Management Work?

Tags:

python

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 it? Or, does the Python interpreter already do this in a way? Does it destroy classes not in use from memory, and how so?

I know C++/C are very memory orientated with pointers and all that, but is Python? And I'm not saying I have problem with it; I, more or less, want to make a modification to it for my program's design. I want to write a large program that use hundreds of classes and modules. But I'm afraid if I do this I'll bog the application down, since I have no understanding of how Python handles memory management.

I know it is a vague question, but if somebody would link or point me in the right direction it would be greatly appreciated.

like image 619
user1541566 Avatar asked Jul 21 '12 22:07

user1541566


3 Answers

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.

like image 151
Mud Avatar answered Sep 30 '22 18:09

Mud


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.
like image 35
Magimai Orieal Fethics Avatar answered Sep 30 '22 19:09

Magimai Orieal Fethics


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.

like image 43
Michel Keijzers Avatar answered Sep 30 '22 18:09

Michel Keijzers