Haskell Socket Programming - Memory Leaks

I play with some basic haskell network files, but there is a problem that turns me nuts. The following fairly simple server code seems to be leaking memory, and I just don't know why.

First, I accept the fork-method and just echo all incoming messages. I tested the server using Apache Benchmark Tool ab. But after several tests using the server, the process begins to flow memory, but not a lot, but continuously. I would guess about 1 MB every 10,000 requests.

Question: is this some kind of internal garbage collector optimization or is it really a memory leak? I tested the program with up to 200 MB of memory usage, still growing.

In this example, I use strict ByteStrings. Another test I did was using a manual I / O descriptor, which did not cause the memory to grow by more than 3 MB.

Using lazy I / O (ByteString.Lazy) caused the same problem, but even faster. I am using GHC 7.6 (Windows 8).

echoClient :: Socket -> IO () echoClient nextSock = do -- Get a stream of bytes stream <- NetStrictBS.recv nextSock 120 -- Echo back NetStrictBS.send nextSock stream -- Kill the socket sClose nextSock acceptClients :: Socket -> IO () acceptClients sock = do -- Start with zero index loop sock 0 where loop sock sockId = do -- Accept socket (nextSock, addr) <- accept sock -- Disable Nagle setSocketOption nextSock NoDelay 1 -- Process client echoClient nextSock -- Accept next client loop sock (sockId + 1) main = withSocketsDo $ do -- Setup server socket sock <- tcpSock setSocketOption sock ReuseAddr 1 bindSocket sock (SockAddrInet 1337 iNADDR_ANY) listen sock 128 -- Fork the acceptor forkIO $ acceptClients sock print "Server running ... " >> getLine >>= print 
+4
source share
1 answer

I just talked with a colleague and he told me that the problem is in this section

 loop sock (sockId + 1) 

I collect tricks without any evolution that definitely fills the bunch. Hope this helps other people facing similar problems.

+6
source

Source: https://habr.com/ru/post/1486795/


All Articles