[Tutor] Memory management in Python

Steven D'Aprano 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
> space.

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[0]); id(l[1]);
> 61659528
> 61659528
> 61659528
> >>> #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[0] and l[1] 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 = []
>>> id(mylist)

> >>> l[0]=[0,0]
> >>> l
> [[0, 0], [1, 2]]
> >>> #Why didn't l[1] change as well?

Here you create a new object, the list [0, 0], and bind it to the 
reference l[0]. You are not *modifying* the original list "a", but 
*replacing* it.

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[0], l[1]

Then you re-assign or re-bind one of those names to a different object:

    l[0] = [0, 0]

But you haven't mutated the object, you have only reassigned the name.

Instead, if you do this:

    l[1][1] = 999

that takes the list called "l[1]" 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[1], you will see the same object:

    [1, 999]


More information about the Tutor mailing list