Haskell (GHC) runtime memory usage or what I did wrong

I wrote a small program in Haskell, a special HTTP server, which is not much more complex than the following code What puzzles me is its memory consumption For example, when I run a test compiled from the attached code and issue several post requests containing an entire program up to 20MB, the VM size of the entire program will reach ~ 800MB, which sounds strange If I leave an instance of such a program idle, this space will not be returned to the system

What's the meaning of this?

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

Solution

First, you are using string This is an inefficient representation of a large amount of data; The cost is about 20 bytes per character You should use bytestring (in the data. Bytestring / data. Bytestring. Char8 module in the package bytestring)

Secondly, GHC (including version 6.12) does not return memory to the operating system However, the upcoming GHC 7.0 will do so, so try the latest release candidate

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>