Questions tagged [space-leak]

Space leak in lazy languages like Haskell refers to a situation when a program uses much more memory than seemingly needed and anticipated.

Space leak in lazy languages like Haskell refers to a situation when a program uses much more memory than seemingly needed and anticipated.

28 questions
53
votes
2 answers

Small file upload to s3 hangs with 100% CPU usage using Paperclip

I have a directory of <20MB pdf files (each pdf represents an ad) on an AWS EC2 large instance. I'm trying to upload each pdf file to S3 using ruby and DM-Paperclip. Most files upload successfully but some seem to take hours with the CPU hanging at…
UserFrank
  • 531
  • 3
  • 5
25
votes
3 answers

Space leaks, and Writers, and Sums (oh my!)

I've been playing with the Writer Monad recently, and I've run into what appears to be a space leak. I can't say I fully understand these things yet, so I'd like to know what's happening here, and how to fix it. First, here's how I can trigger this…
Adam Wagner
  • 15,469
  • 7
  • 52
  • 66
22
votes
2 answers

Space leak with redundant use of seq in GHC interpreter

I type this code into the interpreter and memory is rapidly consumed: last [1..10^7] `seq` () I can't see why this needs more than O(1) space. If i do just (which should be the same, because Show forces weak head normal form, so seq is…
18
votes
2 answers

Space leaks in Haskell

I have read it many times that lazy evaluation in Haskell may sometimes lead to space leaks. What kind of code can lead to space leaks? How to detect them? And what precautions can be taken on part of a programmer to avoid them?
missingfaktor
  • 90,905
  • 62
  • 285
  • 365
14
votes
1 answer

Irrefutable pattern does not leak memory in recursion, but why?

The mapAndSum function in the code block all the way below combines map and sum (never mind that another sum is applied in the main function, it just serves to make the output compact). The map is computed lazily, while the sum is computed using an…
buechse
  • 163
  • 6
13
votes
2 answers

STUArray s i e - space leak only when i == Int?

I am confused by the behaviour of the following snipped: import Data.Int import Data.Array.ST import Control.Monad.ST {-# INLINE fib #-} fib _ 0 = return 0 fib _ 1 = return 1 fib c n = do f1 <- memo c (fib c) (n-1) f2 <- memo c (fib c) (n-2) …
Ed'ka
  • 6,595
  • 29
  • 30
12
votes
1 answer

double stream feed to prevent unneeded memoization?

I'm new to Haskell and I'm trying to implement Euler's Sieve in stream processing style. When I checked the Haskell Wiki page about prime numbers, I found some mysterious optimization technique for streams. In 3.8 Linear merging of that…
9
votes
2 answers

Space leak with recursive list zipWith

My space leak happens in one of my personal project. But I don't want someone to solve it in my project. I want to understand it. I reproduced my space leak by making up this algorithm: u is a sequence defined by: u(0) = 1 u(1) = 2 u(2) = 1 u(4) =…
Antoine Catton
  • 380
  • 3
  • 16
9
votes
2 answers

How do I parse a large data block into memory in Haskell?

On reflection, this entire question can be boiled down to something much more concise. I'm looking for a Haskell data structure that looks like a list has O(1) lookup has either O(1) element replacement or O(1) element append (or prepend... I…
Savanni D'Gerinel
  • 2,379
  • 17
  • 27
8
votes
1 answer

Performance of Floyd-Warshall in Haskell – Fixing a space leak

I wanted to write an efficient implementation of the Floyd-Warshall all pairs shortest path algorithm in Haskell using Vectors to hopefully get good performance. The implementation is quite straight-forward, but instead of using a 3-dimensional…
beta
  • 2,380
  • 21
  • 38
5
votes
1 answer

Debug memory issue in Haskell

I'm trying to solve the whole Advent of Code series in Haskell. I'm encountering a memory issue while solving the 2015/06 exercise where there is a bunch of instructions to turn on, off and toggle lights on a grid. The goal is to count the number of…
David Costa
  • 1,738
  • 18
  • 33
5
votes
1 answer

Space leak in C++?

In Google's C++ test framework, my eyes read: .. returns from the current function immediately, possibly skipping clean-up code that comes after it, it may cause a space leak. while my brain expected to see a memory leak. Is that terminology used…
gsamaras
  • 71,951
  • 46
  • 188
  • 305
4
votes
1 answer

Can I exploit lazy evaluation to reference future values without space leaks?

I'm looking to try to run a moderately expensive function on a large list of inputs, using part of the output of that function as one of its inputs. The code runs as expected, unfortunately it consumes a large amount of memory in the process (just…
Stephen Morgan
  • 327
  • 1
  • 9
3
votes
0 answers

Can we avoid space leaks in the Writer monad by making mappend non-strict on the second parameter

I recently read about the space leak issue of the Writer/WriterT monad. If I correctly understood this problem, it is because the bind operator, i.e. (>>=) is not tail-recursive: m >>= f = WriterT $ do (a, w1) <- runWriterT m (b, w2) <-…
Ruifeng Xie
  • 813
  • 4
  • 16
3
votes
1 answer

Confused: Haskell IO Laziness

I'm having difficulties in understanding Haskell lazy evaluation. I wrote simple test program. It reads 4 lines of data and second and fourth input line has lots of numbers. consumeList :: [Int] -> [Int] -> [Int] consumeList [] _ = error "hi" -- to…
Chul-Woong Yang
  • 1,223
  • 10
  • 17
1
2