Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python creates everything from heap?

in c/c++, you have variables in stack when you create a local variable inside a function.

http://effbot.org/zone/call-by-object.htm

CLU objects exist independently of procedure activations. Space for objects is allocated from a dynamic storage area /.../ In theory, all objects continue to exist forever. In practice, the space used by an object may be reclaimed when the object isno longer accessible to any CLU program.

Does this mean objects in python is created from heap(as in malloc in c/c++)? and the objects are deallocated when there 's no name associated with them?(like smart pointers)?

Example:

def foo(a): 
  result = []
  result.append(a)
  return result

foo("hello")

myList = foo("bye")

So the first result([]) was created in the heap and got deallocated because there's no name associated with it?

like image 210
eugene Avatar asked Jul 27 '12 13:07

eugene


People also ask

Does Python allocate everything on the heap?

All objects and instance variables are stored in the heap memory. When a variable is created in Python, it is stored in a private heap which will then allow for allocation and deallocation. The heap memory enables these variables to be accessed globally by all your program's methods.

Does Python use stack or heap?

Memory Allocation in Python The methods/method calls and the references are stored in stack memory and all the values objects are stored in a private heap.

Are objects always created on the heap?

Heap space is used for the dynamic memory allocation of Java objects and JRE classes at runtime. New objects are always created in heap space, and the references to these objects are stored in stack memory. These objects have global access and we can access them from anywhere in the application.

How do you prevent an object from creating heap?

You could declare a function called "operator new" inside the Foo class which would block the access to the normal form of new.


2 Answers

Yes, all values in CPython are allocated on the heap and reference-counted to know when to deallocate them. Unlike in C, there is no way to know in most cases if a value will outlive its function, so the only safe thing to do is to heap-allocate everything.

Certainly you could do some analysis and determine that certain values are never passed to functions and thus couldn't escape, but that's of limited use in Python and the extra overhead probably wouldn't be worth it.

like image 139
Gabe Avatar answered Oct 22 '22 20:10

Gabe


Yes, all Python objects live on the heap (at least on CPython.) They are reference-counted: they are de-allocated when the last reference to the object disappear. (CPython also has a garbage collector to break cycles.)

In CPython your first list disappears as soon as the function returns since you did not bind the return value to a name and the reference count dropped to zero. In other implementation the object may live longer until the garbage-collector kicks in.

Some objects (like open files) have resources attached that are automatically freed when the object is deallocated, but because of the above it is not recommended to rely on this. Resources should be closed explicitly when you are done with them.

like image 24
Simon Sapin Avatar answered Oct 22 '22 20:10

Simon Sapin