Skip to content

High memory usage after performing GC

Hello, lets consider following program:

{-# LANGUAGE NoMonomorphismRestriction #-}

import System.Mem.Weak
import Control.Concurrent
import System.Mem

data Tst a = Tst a deriving (Show, Eq)

tst a = do
    let arr = [0 .. a*a*a]
        v = Tst (seq arr arr)
    ptr <- mkWeakPtr v Nothing
    return ptr

main = do
    ptrs <- mapM tst [1..100000000]
    --performGC
    --performMajorGC
    --performMinorGC
    threadDelay 1000000
    xr <- mapM deRefWeak ptrs
    print $ length $ filter (/= Nothing) xr

    threadDelay 5000000

    return ()

It simply creates 10 million of weak references to values of Tst. These weak pointers are returned in the main function. After that we sleep a second and ask how meany references are alive. I get the 0 as a result here - so everything seems ok - garbage collection worked. There is a problem though - somehow the memory was not released, because during the last 5-second sleep the program uses over 14 Gb of RAM on my computer.

I'm compiling it simply with ghc -O2 Main.hs. I'm pretty sure this is a bug, because such behaviour is not expected I think.

Edited by danilo2
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information