如何限制Haskell线程的内存使用量 [英] How to cap memory usage of Haskell threads

查看:89
本文介绍了如何限制Haskell线程的内存使用量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在用GHC编译的Haskell程序中,是否有可能以编程方式防止内存使用过多?也就是说,当内存使用量达到指定的限制时,让它通知程序,最好是指出攻击的线程。

例如,假设我想写一个服务器,托管一个服务器脚本语言解释器,用户可以连接到。它是图灵完备的,所以程序理论上可以使用无限的内存或时间。假设每个客户端都使用单独的线程处理。如果客户端写入一个消耗非常快的内存的无限循环,我想确保线程在被异常提醒之前消耗的内存不超过1 MB。我不希望其他用户在发生这种情况时受到影响。



这可能是使用单独的进程和 ulimit ,但是:


  • 我宁愿将它保存在一个程序中,以避免进程间通信的复杂性。


  • 我需要同时支持Linux和Windows,因此如果可能的话,我宁愿保持其与平台无关。


  • <解析方案Edward Z. Yang和DavidMazières开发了一个支持动态资源限制的GHC扩展,并在 http://ezyang.com/rlimits.html 他们还提供了一个支持这个版本的GHC 7.8版本。 p>

    不幸的是,他们的工作并未包含在GHC上游。


    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.

    解决方案

    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.

    这篇关于如何限制Haskell线程的内存使用量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆