Is there a reason why there the array.array constructor does not allow to simply specify the number of items that should be allocated? (I do not really care about the contents.) Would this be a worthwhile addition to / modification of the array module?
I think it would be. Rather than an additional constructor parameter, perhaps it could be a separate method (like we already have extend(), fromfile(), etc.).
In the meantime, on 3.x you should be able to use the following trick, at least under modern Linux / Unix systems:
f = open("/dev/zero", "rb") m = mmap.mmap(f.fileno(), 2*1024*1024*1024, access=mmap.ACCESS_READ)
# I'm putting 2GB for the sake of example. Since the mmap is # created over /dev/zero, kernel optimizations should avoid any # physical RAM consumption (virtual memory aka address space # will still be used, of course).
a = array.array("i") a.frombytes(m) len(a)
# This releases any virtual memory used by the mmap object