문제

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top