ond*_*dra 16 haskell memory-leaks
这段代码泄漏了内存(非常快,如果你尝试的话,准备很快杀死它):
import Control.Monad (forever)
main = do
forever $ forever $ return ()
Run Code Online (Sandbox Code Playgroud)
(编译-O2,-O,-O0 ......,ghc 7.0.3)我不明白为什么要泄漏 - 我使用了很多这样的代码与异常处理程序之间的永远和我不很明白为什么这应该泄漏记忆..
我只是查看了Control.Monad的源代码,发现了这个:
{- Note [Make forever INLINABLE]
If you say x = forever a
you'll get x = a >> a >> a >> a >> ... etc ...
and that can make a massive space leak (see Trac #5205)
In some monads, where (>>) is expensive, this might be the right
thing, but not in the IO monad. We want to specialise 'forever' for
the IO monad, so that eta expansion happens and there's no space leak.
To achieve this we must make forever INLINABLE, so that it'll get
specialised at call sites.
Still delicate, though, because it depends on optimisation. But there
really is a space/time tradeoff here, and only optimisation reveals
the "right" answer.
-}
Run Code Online (Sandbox Code Playgroud)
这个错误据说是"固定的"; 不幸的是,嵌套永远会再次触发bug.有趣的是,这个永远的定义(借用Control.Monad)触发了错误:
forever a = a >> forever a
Run Code Online (Sandbox Code Playgroud)
虽然以下定义没有问题:
forever a = a >>= \_ -> forever a
Run Code Online (Sandbox Code Playgroud)
>>运算符中有些东西可疑,因为我将此代码视为等效.