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?
Space leaks in Haskell
3.4k Views Asked by missingfaktor At
2
There are 2 best solutions below
0
afkbowflexin
On
I've run into this problem when doing recursion over large data structures. The built up thunks get to be too much and then you get a space leak.
In Haskell, you need to be constantly aware of the possibility of running into a space leak. Since iteration doesn't exist, basically any recursive function has the potential to generate a space leak.
To avoid this problem, memoize recursive functions, or rewrite them tail-recursively.
Related Questions in HASKELL
- Cabal sandbox is using a global dependency. Could not resolve
- Haskell lens: let binding of Traversal'
- How can I parse fixed-length, non-delimited integers with attoparsec?
- Pipeline-like operation using TChan
- compile-time vs. run-time cost of Hamlet templates
- Date-time package in haskell - error in the current one, can't find an analog
- How does one debug infinite recursion in Haskell?
- Force GHC using local files
- List with random numbers in Haskell
- Changes in other elements based on listbox selections in threepenny-gui
- Multithreading and gtk2hs
- Operator section for applicative with <$> and <*>
- Unable to create a custom header to use it in "withManager"
- How do I reuse an intermediate value in chain of Haskell Either binds?
- Haskell, Tree problems
Related Questions in FUNCTIONAL-PROGRAMMING
- Access into a Binary Search Tree via a bound function in a function template
- Convert loop to Maybe monad
- Lazy concat in Immutable.js?
- Erlang syntax error unclear
- What is the type of the variable in do-notation here in Haskell?
- Lazy functions evaluation in swift
- Standard ML / NJ: Loading in file of functions
- First Object in Set<Future<Object>> that satisfies a predicate
- How to write a type that is isomorphic to Tree without nested lists?
- Functional way of doing a loop of operations on an array
- Good practice on how to store the result of a function for later use in R
- Apply a list of Functions to a Java stream's .map() method
- First word of binary string erlang
- Easier way to apply multiple arguments in Haskell
- scala : use of braces for a function when the parameter is a predicate
Related Questions in LAZY-EVALUATION
- What is the exact difference in behavior between `++` and `#:::` when you concatenate two streams?
- Lazy functions evaluation in swift
- Advantages of setting variables inside a boolean evaluation expression
- Haskell return lazy string from file IO
- Python lazy evaluation numpy ndarray
- Writing lazy curried in scala with multiple parameters
- Why is lazy evaluation in Haskell "not being lazy"?
- Haskell and laziness
- Groovy lazy object construction
- Is there a lazy functional (immutable) language where functions have intermediate variables+return?
- Should I avoid exposing the Lazy<T> class in public API?
- Is the "where" keyword lazy?
- Calling non-strict function in Scala with explicit types doesn't compile, inferred types works
- In R, how do I define a function which is equivalent to `deparse(substitute(x))`?
- sl4j with log4j, lazy evaluation
Related Questions in SPACE-LEAK
- Is there a space leak in this Haskell implementation of LPath?
- Apparent space leak in variant upon Brent's "teleporting turtle" algorithm
- Why is there a space leak in my haskell program using `transpose`
- Space leak with redundant use of seq in GHC interpreter
- Space leaks, and Writers, and Sums (oh my!)
- Space leaks in Haskell
- Small file upload to s3 hangs with 100% CPU usage using Paperclip
- Confused: Haskell IO Laziness
- Growing stack size w/ inadvertent loop statement - expected behavior?
- Space leak in Haskell - old compiler's fault or mine? Apparently the latter
- Can I exploit lazy evaluation to reference future values without space leaks?
- How do I get lazy streaming into the foldl'?
- Space leak in C++?
- Can we avoid space leaks in the Writer monad by making mappend non-strict on the second parameter
- STUArray s i e - space leak only when i == Int?
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
You will get probably many answeres, this is the one, I have encountered when trying to do some 'real-world' application. I was using multithreading and some MVars to pass data around (MVar is something like locked shared memory). My typical pattern was:
And then, just sometimes, when a proper condition happened I did something like:
The problem is that the content of mvar was essentially (0 + 1 + 1 + 1 + ....)...which was quite intensive for numbers like 100k... This type of problem was quite pervasive in my code; unfortunately for multithreading applications it's very easy to get into such problems.
Detecting...what I did was starting haskell in the mode that produces data regarding memory consumption, starting and stopping different threads and looking if memory footprint is stable or not...
Anotomy of a thunk leak (with instructions how to debug it)
An example: Thunk memory leak as a result of map function