有没有办法限制内存,ghci可以有?

时间:2010-09-22 05:53:14

标签: debugging haskell memory-management ghc ghci

我习惯使用ghci来调试我的代码。通常,会发生类似这样的事情(当然不是那么明显):

ghci> let f@(_:x) = 0:1:zipWith(+)f x
ghci> length f

然后,一段时间没有任何反应,如果我反应不够快,ghci已经吃掉了2 GB的RAM,导致我的系统冻结。如果为时已晚,解决此问题的唯一方法是[ALT] + [打印] + [K]。

我的问题:是否有一种简单的方法来限制内存,ghci可以使用,比方说1 GB?如果超过限制,计算应该中止或ghci应该被杀死。

2 个答案:

答案 0 :(得分:18)

完成此任务的平台独立方法是向Haskell运行时选项提供-M选项,如下所示

ghci +RTS -M1m

有关详细信息,请参阅the GHC documentation’s page on how to control the RTS (runtime system)

ghci输出现在看起来像:

>ghci +RTS -M10m
GHCi, version 6.12.3: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.
Prelude> let f@(_:x) = 0:1:zipWith(+)f x
Prelude> length f
Heap exhausted;
Current maximum heap size is 10485760 bytes (10 MB);
use `+RTS -M<size>' to increase it.

答案 1 :(得分:2)

在设置ulimit -m的shell下运行它是一种相当简单的方法。如果您希望定期运行某些限制,则可以在运行ulimit之前创建一个ghci的包装脚本。