Haskell(ghc)运行时内存使用情况或我做错了什么 [英] Haskell (ghc) runtime memory usage or what do I do wrong

查看:167
本文介绍了Haskell(ghc)运行时内存使用情况或我做错了什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个小程序,在haskell中有一种特殊的HTTP服务器,它不比下面的代码复杂得多。令我困惑的是它的内存消耗。比如说,当我运行一个由附带代码编译的测试,并发出几个包含高达20Mb主体的POST请求时,整个程序的VM大小将达到〜800Mb,这听起来很奇怪。如果我让这样的程序的一个实例闲置,这个空间就不会返回给系统。



这是什么意思?

  
import System.IO
import Network.HTTP.Server
import Network.Socket
import Network.URL

$ b $ handler :: SockAddr - > URL - > Request String - > IO(Response String)
处理程序sa url rq = do
writeFile/ tmp / out(rqBody rq)
return $ insertHeader HdrContentLength0(响应OK ::响应字符串)

main = serverWith defaultConfig {srvPort = 2121} handler


解决方案

首先,您正在使用字符串。这是大量数据的低效表示;成本是每个字符20字节。你应该使用 ByteString (在 Data.ByteString / Data.ByteString.Char8

其次,GHC高达并包括6.12版本,包括 bytestring 不会将内存返回到操作系统。然而即将到来的GHC 7.0会这样做,所以请尝试使用最新候选版本


I wrote a small program, kind of specialized HTTP server in haskell, which is not much more complex than the code below. What puzzles me is its memory consumption. Say, when I run a test compiled from the enclosed code and make several POST requests containing up to 20Mb body whole program will have VM size of ~800Mb and this sounds odd. And this space is not returned to system if I leave an instance of such program running idle.

What does this mean?


import System.IO
import Network.HTTP.Server
import Network.Socket
import Network.URL


handler :: SockAddr -> URL -> Request String -> IO (Response String)
handler sa url rq = do
  writeFile "/tmp/out" (rqBody rq)
  return $ insertHeader HdrContentLength "0" (respond OK :: Response String)

main = serverWith defaultConfig {srvPort = 2121} handler

解决方案

Firstly, you're using String. This is an inefficient representation for lots of data; the cost is something like 20 bytes per character. You should use ByteString (in the Data.ByteString / Data.ByteString.Char8 modules in the package bytestring) instead.

Secondly, GHC up to and including version 6.12 doesn't return memory to the OS. However the upcoming GHC 7.0 will do this, so try with the latest release candidate.

这篇关于Haskell(ghc)运行时内存使用情况或我做错了什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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