Haskell (ghc) 运行时内存使用情况或者我做错了什么

发布于 2024-09-30 00:55:16 字数 570 浏览 1 评论 0原文

我用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泅人 2024-10-07 00:55:16

首先,您使用的是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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文