由于库文档说与newCString
创建必须释放与free
函数。我一直期待在创建CString
时需要一些内存,当它与free
一起发布时,内存使用量会下降,但它不会!下面是示例代码:释放分配有newCString的内存
module Main where
import Foreign
import Foreign.C.String
import System.IO
wait = do
putStr "Press enter" >> hFlush stdout
_ <- getLine
return()
main = do
let s = concat $ replicate 1000000 ['0'..'9']
cs <- newCString s
cs `seq` wait -- (1)
free cs
wait -- (2)
当程序停在(1),htop
程序显示内存使用某处大约410M - 这是确定。我按回车,程序停在第(2)行,但内存使用仍然是410M,尽管cs
已经free
d!
这怎么可能?用C编写的类似程序的行为应该与它应该一样。我在这里错过了什么?
您正在使用哪个版本的GHC?将内存返回到操作系统的能力仅在去年被添加到GHC中。 –
'ghc --version'输出'Glorious Glasgow Haskell编译系统,版本7.4.1' –