开发者

How to cap memory usage of Haskell threads

开发者 https://www.devze.com 2023-04-02 12:16 出处:网络
In a Haskell program compiled with GHC, is it possible to programmatically guard against excessive memory usage?That is, have it notify the program when memory usage reaches a specified limit, prefera

In a Haskell program compiled with GHC, is it possible to programmatically guard against excessive memory usage? That is, have it notify the program when memory usage reaches a specified limit, preferably indicating the offending thread.

For example, suppose I want to write a server, hosting a scripting language interpreter, that users can connect to. It's Turing-complete, so programs could theoretically use unlimited memory or time. Suppose each client is handled with a separate thread. If a client writes an infinite loop that consumes memory very quickly, I want to ensure that the thread consumes no more than, say, 1 MB of memory, before being alerted with an exception. I do not want other users to be affected when that happens.

This is probably possible using separate processes and ulimit, but:

  • I would rather keep it in one program, to avoid the complexity of inter-process communication.

  • I need to support both Linux and Windows, 开发者_如何学Cso I would prefer to keep it platform-agnostic if possible.


Edward Z. Yang and David Mazières have developed an extension to GHC that supports dynamic resource limits, and discuss it at http://ezyang.com/rlimits.html They also provide a version of GHC 7.8 that supports this.

Unfortunately, their work was not included in GHC upstream.


May not be exactly what you want. But, as documented here you have a ghc compile option: -Ksize, update: Oops, sorry, -K is for stack overflows. Still, you can check that link.


In your example, you may need to modify the source of the scripting language interpreter, make some twists to the memory mgmt. module(s), of course IF it has some managed memory allocation features, the interpreter can complain about an execessive use of memory quota by an API callback to your host application.

0

精彩评论

暂无评论...
验证码 换一张
取 消