Skip to content

Memory leak in print when RTS option -N is >= 2

In GHC 802 the code below runs with constant memory. In GHC 822, 843 and 861 the code below runs with constant memory only if at most one core is used. If the RTS option -N is set to anything greater than 1, the memory usage grows with the size of "big_file".

import Data.ByteString.Builder.Extra (defaultChunkSize)
import Data.Function
import System.IO
import qualified Data.ByteString as BS

main :: IO ()
main = do
    h <- openFile "big_file" ReadMode
    fix $ \loop -> do
      bs <- BS.hGetSome h defaultChunkSize
      if BS.null bs
      then pure ()
      else do
        print bs
        loop

You can reproduce this by cloning https://github.com/nmattia/ghc-print-leak and running "nix-shell". Alternatively save the code above to "Main.hs" and run "ghc ./Main.hs -threaded -rtsopts" and then "./Main +RTS -M2M -N[1|2] > /dev/null".

Side note: the memory does not grow as quickly if the size of the bytestring is printed, i.e. it seems to be a problem specific to hPutStrLn/print as opposed to the actual file content/bytestring being retained. I first encountered this issue when using conduit for the loop.

Trac metadata
Trac field Value
Version 8.6.1
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Compiler
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information