bug in mallocForeignPtrBytes (both 6.4 and 6.6)
Duncan Coutts
duncan.coutts at worc.ox.ac.uk
Sat Sep 23 16:22:38 EDT 2006
On Sat, 2006-09-23 at 13:58 +0400, Bulat Ziganshin wrote:
> Hello glasgow-haskell-users,
>
> the attached program show up the bug in mallocForeignPtrBytes (and
> newPinnedByteArray#) implementation - it allocates two times more
> memory as requested. The bug seen both on 6.6rc and june 6.4 windows
> builds, namely:
>
> http://www.haskell.org/ghc/dist/current/dist/ghc-6.5.20060901-i386-unknown-mingw32.tar.gz
> http://www.haskell.org/ghc/dist/stable/dist/ghc-6.4.2.20060609-i386-unknown-mingw32.tar.gz
>
> after program prints "40 mb allocated" look at Task Manager indication
> - it shows that two times more memory actually in use. it seems that
> problem is only with allocating memory buffers whose sizes are powers
> of 2 or very close - bug shows for bufsize = 4kb, 1mb or 4095, but not
> not for 4000 or 10^6
The reason for this is that for larger objects ghc has an allocation
granularity of 4k. That is it always uses a multiple of 4k bytes.
However a byte array has some overhead: it needs one word for the heap
cell header and another for the length. So if you allocate a 4k byte
array then it uses 8k. So the trick is to allocate 4k - overhead. This
is what the Data.ByteString library does.
Duncan
More information about the Glasgow-haskell-users
mailing list