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 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.

3
  • 4
    …but it sounds like your question has more to do with dynamic __import__ than it does with memory management. Are you sure you care about memory here? Commented Jul 21, 2012 at 22:39
  • 5
    -1 for completely garbled text. I literally only understand two of your sentences. Commented Jul 21, 2012 at 23:02
  • 1
    "I'm afraid if I do this I'll bog it down..." -- sounds like a phantom fear. Write a quick benchmark with a couple hundred classes to see if it scales the way you need it to. Commented Jul 21, 2012 at 23:09

5 Answers 5

28

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Okay thank for that tip and yes I want a create dynamic import class. I guess it was two questions.
10
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.

1 Comment

Python manages all objects methods/functions in a private heap.
7

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.

2 Comments

Link 1 is no longer relevant, and link 2 links to nothing.
@Vesanto Thanks for the notification... I copied thos most important part of the link (otherwise it is more a link only answer). I removed one of the links since it is indeed not up to date anymore.
3

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

1 Comment

is second point still happening with Python 3?. About not returning back free memory to operative system
2

Yes its the same behaviour in python3 as well

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.