Haskell (ghc) 运行时内存使用情况或者我做错了什么
我用haskell写了一个小程序,一种专门的HTTP服务器,它并不比下面的代码复杂多少。让我困惑的是它的内存消耗。比如说,当我运行一个从所附代码编译的测试并发出多个包含高达 20Mb 主体的 POST 请求时,整个程序的 VM 大小将约为 800Mb,这听起来很奇怪。如果我让此类程序的实例空闲运行,则该空间不会返回到系统。
这意味着什么?
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您使用的是
String
。对于大量数据来说,这是一种低效的表示方式;成本约为每个字符 20 个字节。您应该使用ByteString
(在bytestring
包中的Data.ByteString
/Data.ByteString.Char8
模块中)反而。其次,GHC 6.12 及之前版本不会将内存返回给操作系统。不过,即将推出的 GHC 7.0 将执行此操作,因此请尝试使用 最新候选版本。
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 useByteString
(in theData.ByteString
/Data.ByteString.Char8
modules in the packagebytestring
) 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.