Python arenas are used for allocation of objects less than size 512bytes. They are very efficient at memory management for the python VM.

Let us look at the structures for the arenas

typedef struct _block {
/* Total number of bytes owned by this block available to pass out.
* Read-only after initialization. The first such byte starts at
* ab_mem.
size_t ab_size;

/* Total number of bytes already passed out. The next byte available
* to pass out starts at ab_mem + ab_offset.
size_t ab_offset;

/* An arena maintains a singly-linked, NULL-terminated list of
* all blocks owned by the arena. These are linked via the
* ab_next member.
struct _block *ab_next;

/* Pointer to the first allocatable byte owned by this block. Read-
* only after initialization.
void *ab_mem;
} block;

/* The arena manages two kinds of memory, blocks of raw memory
and a list of PyObject* pointers. PyObjects are decrefed
when the arena is freed.

struct _arena {
/* Pointer to the first block allocated for the arena, never NULL.
It is used only to find the first block when the arena is
being freed.
block *a_head;

/* Pointer to the block currently used for allocation. It’s
ab_next field should be NULL. If it is not-null after a
call to block_alloc(), it means a new block has been allocated
and a_cur should be reset to point it.
block *a_cur;

/* A Python list object containing references to all the PyObject
pointers associated with this area. They will be DECREFed
when the arena is freed.
PyObject *a_objects;

#if defined(Py_DEBUG)
/* Debug output */
size_t total_allocs;
size_t total_size;
size_t total_blocks;
size_t total_block_size;
size_t total_big_blocks;

More about how arenas work is defined here :

The code implementation is in file obmalloc.c in line number 792. I would urge you to debug further and understand how arenas are used in memory management.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s