Haskell (ghc) runtime memory usage or what do I do wrong

江枫思渺然 提交于 2019-12-10 18:27:07

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/4082375/haskell-ghc-runtime-memory-usage-or-what-do-i-do-wrong

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