Opened 8 years ago

Closed 8 years ago

Last modified 21 months ago

#5729 closed bug (duplicate)

ForeignPtr leak in ghci

Reported by: judahj Owned by:
Priority: normal Milestone:
Component: GHCi Version: 7.2.1
Keywords: FloatOut Cc:
Operating System: MacOS X Architecture: x86_64 (amd64)
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:

Description

I have a program which creates an infinite list of Storable Vectors and then scans through the list until some condition has been met. When compiled, the program runs in constant space. When run in ghci, however, the program leaks memory.

I can reproduce the behavior with the following module, using both ghc-7.2.1 and ghc-7.4.0.20111219 on Mac OS X 10.7 x86_64. More precisely, I do not see a leak (using top) when the program is

  • Compiled with ghc --make -O2 and run from the command-line
  • Compiled with ghc --make -O0 and run from the command-line
  • Compiled with ghc --make -O0, loaded into ghci and run with :main

But I do see a leak (top shows a growing memory usage) when the program is

  • Loaded uncompiled into ghci and run with :main
  • Compiled with ghc --make -O2 (or -O), loaded into ghci and run with :main.
module Main where

import System.IO.Unsafe
import Foreign.ForeignPtr
import Control.Exception

main = do
    let steps = infinite 256
    evaluate $ myLast steps

myLast :: [a] -> a
myLast [x] = x
myLast (x:xs) = myLast xs

infinite :: Int -> [ForeignPtr ()]
infinite n = let
    xs = makeVector n
    in xs `seq` (xs : infinite n)

makeVector :: Int -> ForeignPtr ()
makeVector n = unsafePerformIO $ mallocForeignPtrBytes n

Change History (2)

comment:1 Changed 8 years ago by judahj

Resolution: duplicate
Status: newclosed

OK, it turns out this was an instance of #917. GHC was turning the infinite list into a CAF. When the program was run from the command-line, the initial parts of that list could be garbage collected since they would only be used once. But when the program was run from within GHCi, the entire list would be retained since the program might be run multiple times in the same session.

comment:2 Changed 21 months ago by simonpj

Keywords: FloatOut added
Note: See TracTickets for help on using tickets.