21.01.2015 Views

color version - PET: Python Entre Todos - Python Argentina

color version - PET: Python Entre Todos - Python Argentina

color version - PET: Python Entre Todos - Python Argentina

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Memory debugging and defragmentation<br />

Author: Claudio Freire<br />

How would you say <strong>Python</strong> manages memory Malloc (a C function that reserves<br />

memory for those unfamiliar with C)<br />

Well, no. <strong>Python</strong> has unusual and very specific requirements, if it used malloc for all of<br />

its memory needs, it would be too inefficient. <strong>Python</strong> uses, instead, a series of techniques<br />

and strategies designed to minimize malloc calls, which are very slow to be used in the<br />

core of <strong>Python</strong>’s virtual machine.<br />

• Pools<br />

Introduction<br />

This lecture was planned to be given in 20 minutes. If you can’t read it in 20 minutes,<br />

reread, reread and re-reread until it takes 20 minutes :-p<br />

Well, actually the text here is only slightly based in the real lecture. I’m one known to<br />

improvise in lectures, all I have are the slides as a guide, the general guidelines for the<br />

lecture, and this format doesn’t lend itself to improvisation. Besides, no freaking way I’m<br />

going to remember the exact words I used ;)<br />

Now, really… this is a deep convoluted subject. Reread and re-reread until you get it.<br />

When I started looking for lecture subjects, I decided for memory fragmentation because<br />

not long ago I though it was a myth. Ok, yes, it can happen… but, does that happen to<br />

anybody Is it a problem How naive of me to doubt that could answers could be “yes”.<br />

In PyConAr 2010 (and here in this space now), not only I had the chance to transmit<br />

around 4 months of picking <strong>Python</strong>’s guts apart, trying to find out what was going on<br />

with my applications (where I suffered from memory fragmentation), I also learned that<br />

this issue is much, much more common than I’d expected.<br />

When the lecture started, I asked: Does anybody know what memory fragmentation is.<br />

Exactly zero attendants raised their hand. Exactly the amount I had expected. But, in the<br />

end, many approached me saying that the same thing that was happening to me (and<br />

that I described, and will describe below) was happening to them.<br />

• Integer<br />

• Floating point<br />

• Free lists<br />

• Tuples<br />

• Lists<br />

• Frames (yes, the frames are objects and need to be managed too)<br />

• Arenas<br />

Lets see what each is about, one by one<br />

Pools<br />

Not for swimming.<br />

They’re arrays, object vectors of the same type, that <strong>Python</strong> uses usually to accelerate<br />

the creation and destruction of common objects, like integers and floating point<br />

numbers.<br />

When python needs one of these objects, it doesn’t create one, it creates many (as many<br />

as they fit in a pool block), and keeps a linked list of which objects are free within each<br />

block.<br />

That, according to my post-PyCon estimations, means a 10% of everyone present<br />

(assuming they’re <strong>Python</strong> developers) suffer from that issue, and to a 3% of them it<br />

means serious operative problems. So the issue was far more relevant than I had<br />

expected.<br />

How does <strong>Python</strong> manage memory<br />

Before going into this fragmentation thing, we must study some internal details of how<br />

memory management works in <strong>Python</strong>.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!