[Haskell-cafe] Garbage collection and finalizer thread priority

Simon Marlow simonmar at microsoft.com
Mon Jun 13 09:15:33 EDT 2005

On 13 June 2005 11:30, Gracjan Polak wrote:

> My space problems continued... :)
> I have some ForeignPtr's, with finalizers (finalizerFree only). And I
> have lazyly produced list of those (yes, there is some unsafePerformIO
> magic behind, but I think I got this right). The problem is I get
> out-of-memory condition because elements get produced faster than
> those consumed are garbage collected.
> Example:
> list = ...
> mapM_ writeOut list
> writeOut :: Ptr Word8
> writeOut dat = do
>      hPutBuh handle dat 1024
>      -- Control.Concurrent.threadDelay 1000
> Uncommenting this line allows gc thread to run finalizers, memory gets
> freed, everything runs smoothly...
> As far as I know finalizers are run in separate thread. How do I
> increase priority of this thread so it runs faster?

I presume you're running GHC.  There's no way to increase the priority
of a thread - GHC's scheduler doesn't have a concept of priorities.

I would look into whether you can use mallocForeignPtr: this is much
faster than using newForeignPtr with finalizerFree, because the memory
is garbage collected directly and there's no need for a finalizer.


More information about the Haskell-Cafe mailing list