May I limit memory usage per function/monad/thread in Haskell?

心已入冬 提交于 2019-11-30 23:47:41

问题


I'm working on a research compiler project intended to work as a service. One of the requirements is that certain users might have a limited memory usage (e.g., "calls from IP a.b.c.d may use up to 30mb of heap memory") while handling its calls.

My prototype implementation, written in C, simply uses a memory pool indead of malloc'ing directly (which is actually pretty hard to get right due to effective types). Manual memory management, though.

Is there any way to achieve this in Haskell, by limiting heap usage on a function, monad, or lightweight thread? (I'd accept suggestions of other functional languages which might allow me to do this.)


回答1:


In the latest versions of GHC, it is possible to set per-thread allocation counters and limits, using setAllocationCounter and enableAllocationLimit from GHC.Conc. When a limit is set and the counter reaches 0, the thread receives an asynchronous exception.

The counters measure allocation, and not the size of the live set. For example, this code hits the limit, despite its live set never becoming very big:

{-# LANGUAGE NumDecimals #-}
module Main where

import Data.Foldable (for_)
import System.IO
import GHC.Conc (setAllocationCounter,enableAllocationLimit)

main :: IO ()
main = 
  do setAllocationCounter 2e9
     enableAllocationLimit
     let writeToHandle h =
            for_ ([1..]::[Integer])
                 (hPutStrLn h . show)
     withFile "/dev/null" WriteMode writeToHandle
     return ()

Allocation is a bit crude as a measure, but it can still be useful to detect some "out of control" computations.

This blog post by Simon Marlow goes into more detail.



来源:https://stackoverflow.com/questions/42353661/may-i-limit-memory-usage-per-function-monad-thread-in-haskell

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