[Tutor] Memory management in Python
steve at pearwood.info
Thu Nov 27 01:48:23 CET 2014
On Wed, Nov 26, 2014 at 01:57:52PM +0100, Mohamed Ben Mosbah wrote:
> Hi I'm new to Python and I would like to know how he deals with memory
That will depend on *which* Python you are using.
Jython uses the Java Virtual Machine, including the Java garbage
collector. IronPython uses .Net. The "standard" Python, CPython is
written in C and mostly uses the C malloc to manage memory.
But NONE of these things matter when you are writing pure Python code.
Your Python code will behave mostly the same regardless of whether you
use CPython, Jython or IronPython.
> I thought I had understood but I made a test and the results were
> uncoherenent with my understanding, here is the thing:
> >>> a=[1,2]
> >>> l=[a,a]
> >>> id(a); id(l); id(l);
> >>> #All Have the same ID
Correct. You have two lists. The first list is [1, 2], and that object
is bound to the name "a", also bound to item 0 of list "l", and bound to
item 1 of list "l".
Since a, l and l are three references to the same object, not
three different objects, id() returns the same value each time.
Forget any idea you have that id() tells you where objects are in
memory. The Java and .Net memory managers can move objects around in
memory, so the IDs used there are simple counters:
steve at orac:~$ jython
Jython 2.5.1+ (Release_2_5_1, Aug 4 2010, 07:18:19)
[OpenJDK Server VM (Sun Microsystems Inc.)] on java1.6.0_27
Type "help", "copyright", "credits" or "license" for more information.
>>> mylist = 
> >>> l=[0,0]
> >>> l
> [[0, 0], [1, 2]]
> >>> #Why didn't l change as well?
Here you create a new object, the list [0, 0], and bind it to the
reference l. You are not *modifying* the original list "a", but
Think of references like names or job positions. A single
person can have many different names:
John, father, son, husband, brother, boss, captain
can all refer to the same person.
Assignment in Python just changes the name:
boss = Fred
now means that John is no longer the boss, but he is still father, son,
husband, brother, captain.
So you start with a name for an object:
a = [1, 2]
Now you give it two more names:
l = [a, a] # three names: a, l, l
Then you re-assign or re-bind one of those names to a different object:
l = [0, 0]
But you haven't mutated the object, you have only reassigned the name.
Instead, if you do this:
l = 999
that takes the list called "l" and binds 999 to the first item. Since
that list is also called "a", then it doesn't matter whether you print a
or l, you will see the same object:
More information about the Tutor