The introduction
The following code shows that when using runhaskell
Haskell Garbage Collector releases the memory, when a
is no longer used. It results in core dump while releasing variable a
- for a purpose, to inspect the behaviour - a
has got nullFunPtr
as a finalizer.
module Main where
import Foreign.Ptr
import Foreign.ForeignPtr
main :: IO ()
main = do
a <- newForeignPtr nullFunPtr nullPtr
putStrLn "Hello World"
The problem
When running the same in ghci it does not release memory. How can I force ghci to release no longer used variables?
$ ghci
> import Foreign.Ptr
> import Foreign.ForeignPtr
> import System.Mem
> a <- newForeignPtr nullFunPtr nullPtr
> a <- return () -- rebinding variable a to show gc that I'm no longer using it
> performGC
> -- did not crash - GC didn't release memory
> ^D
Leaving GHCi.
[1] 4396 segmentation fault (core dumped) ghci
Memory was released on exit, but this is too late for me. I'm extending GHCi and using it for other purpose and I need to release the memory earlier - on demand or as fast as possible would be really great.
I know that I can call finalizeForeignPtr
, but I'm using foreignPtr
just for debug purposes. How can I release a
in general in last example?
If there is no possibility to do it with ghci prompt, I can also modify ghci
code. Maybe I can release this a
by modyfing ghci Interactive Context or DynFlags? So far I've got no luck with my reaserch.