How to use a contiguous memory location of n bytes in python

Nick Craig-Wood nick at
Fri Nov 14 14:29:58 CET 2008

bearophileHUGS at <bearophileHUGS at> wrote:
>  chachi:
> > I want to know how to instantiate a data structure which has n bytes
> > (given by me) and is internally stored in a contiguous fashion.
>  array.array("B", ...) may be fit for you. You can also use a numpy
>  array of bytes.

The mmap module is useful also for larger amounts (>4k say).
mmap's are individually free-able so they don't fragment your memory.

Eg create a 1 GB anonymous mmap, access it and then delete it

>>> from mmap import mmap
>>> a = mmap(-1, 1000000000)
>>> a[0]
>>> a[0] = 'z'
>>> a[999999999]
>>> a[999999999]='q'
>>> a[999999999]
>>> del a

Nick Craig-Wood <nick at> --

More information about the Python-list mailing list