Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 870
sacha Avatar asked Feb 25 '23 22:02

sacha


1 Answers

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.

like image 108
GS - Apologise to Monica Avatar answered Mar 06 '23 01:03

GS - Apologise to Monica