Is there a way to limit the memory, ghci can have?

血红的双手。 提交于 2019-11-27 17:45:28

问题


I'm used to debug my code using ghci. Often, something like this happens (not so obvious, of course):

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

Then, nothing happens for some time, and if I don't react fast enough, ghci has eaten maybe 2 GB of RAM, causing my system to freeze. If it's too late, the only way to solve this problem is [ALT] + [PRINT] + [K].

My question: Is there an easy way to limit the memory, which can be consumed by ghci to, let's say 1 GB? If limit is exceed, the calculation should ve aborted or ghci should be killed.


回答1:


A platform independant way to accomplish this is to supply the -M option as on option to the Haskell runtime like this

ghci +RTS -M1m

see the GHC documentation’s page on how to control the RTS (runtime system) for details.

The ghci output now looks like:

>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.



回答2:


Running it under a shell with ulimit -m set is a fairly easy way. If you want to run with some limit on a regular basis, you can create a wrapper script that does ulimit before running ghci.



来源:https://stackoverflow.com/questions/3766656/is-there-a-way-to-limit-the-memory-ghci-can-have

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!