Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Garbage collection in Python for Linux

Tags:

python

linux

I'm a little puzzled how Python allocates memory and garbage-collects, and how that is platform-specific. For example, When we compare the following two code snippets:

Snippet A:

>>> id('x' * 10000000) == id('x' * 10000000)
True

Snippet B:

>>> x = "x"*10000000
>>> y = "x"*10000000
>>> id(x) == id(y)
False

Snippet A returns true because when Python allocates memory, it allocates it in the same location for the first test, and in different locations in the second test, which is why their memory locations are different.

But apparently system performance or platform impacts this, because when I try this on a larger scale:

for i in xrange(1, 1000000000):
    if id('x' * i) != id('x' * i):
        print i
        break

A friend on a Mac tried this, and it ran until the end. When I ran it on a bunch of Linux VMs, it would invariably return (but at different times) on different VMs. Is this because of the scheduling of the garbage collection in Python? Was it because my Linux VMs had less processing speed than the Mac, or does the Linux Python implementation garbage-collect differently?

like image 552
pbanka Avatar asked Dec 09 '22 20:12

pbanka


1 Answers

The garbage collector just uses whatever space is convenient. There are lots of different garbage collection strategies, and things are also affected by paramters, different platforms, memory usage, phase of the moon etc. Trying to guess how the interpreter will happen to allocate particular objects is just a waste of time.

like image 172
Antimony Avatar answered Dec 12 '22 12:12

Antimony