Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cap memory usage of Haskell threads

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, so I would prefer to keep it platform-agnostic if possible.

like image 294
Joey Adams Avatar asked Sep 04 '11 05:09

Joey Adams


3 Answers

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.

like image 137
Joachim Breitner Avatar answered Nov 08 '22 11:11

Joachim Breitner


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.

like image 22
sinelaw Avatar answered Nov 08 '22 09:11

sinelaw


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.

like image 27
claude Avatar answered Nov 08 '22 10:11

claude