Skip to content

newStdGen leaks memory when result is not used

When newStdGen is invoked repeatedly without making any use of the resulting StdGen values, it leaks memory, as in the following:

-- Leaks memory
main = forever newStdGen

When a random number produced by the result is evaluated, no leak occurs:

-- Does not leak memory
main = forever $ do
  r <- newStdGen
  evaluate $ fst $ next r

However, evaluating the result itself is not sufficient to eliminate the leak:

-- Leaks memory
main = forever $ do
  r <- newStdGen
  evaluate r

After sufficient iterations of newStdGen, subsequent use will cause a stack overflow:

-- Causes stack overflow
main = do
  replicateM 1000000 newStdGen
  r <- newStdGen
  evaluate $ fst $ next r
Trac metadata
Trac field Value
Version 7.6.3
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component libraries/random
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