• Stars
    star
    101
  • Rank 338,166 (Top 7 %)
  • Language
    Haskell
  • Created over 8 years ago
  • Updated about 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Notes on space leaks

Spaceleak detection

Every large Haskell program almost inevitably contains space leaks. Space leaks are often difficult to detect, but relatively easy to fix once detected (typically insert a !). This page gives a simple technique to detect some space leaks, along with a set of blog posts using that technique and detailing other space leaks.

Spaceleak stack-limiting technique

The stack-limiting technique is useful for detecting a common kind of spaceleak, a situation where an excessive number of interdependent thunks are created and eventually evaluated. The technique works because thunks are evaluated on the stack, so by limiting the stack size we effectively limit the number of interdependent thunks that we can evaluate.

To find space leaks, given a program and a representative run (e.g. the test suite, a suitable input file):

  • Compile the program for profiling, e.g. ghc --make Main.hs -rtsopts -prof -fprof-auto.
  • Run the program with a specific stack size, e.g. ./Main +RTS -K100K to run with a 100Kb stack.
  • Increase/decrease the stack size until you have determined the minimum stack for which the program succeeds, e.g. -K33K.
  • Reduce the stack by a small amount and rerun with -xc, e.g. ./Main +RTS -K32K -xc.
  • The -xc run will print out the stack trace on every exception, look for the one which says stack overflow (likely the last one) and look at the stack trace to determine roughly where the leak is.
  • Attempt to fix the space leak, confirm by rerunning with -K32K.
  • Repeat until the test works with a small stack, typically -K1K.
  • Add something to your test suite to ensure that if a space leak is ever introduced then it fails, e.g. ghc-options: -with-rtsopts=-K1K in Cabal.

This technique does not detect when an excessive number of thunks are created but never evaluated, or when a small number of thunks hold on to a large amount of live data.

Note that typically the main thread requires greater than 1K stack, and that once GHC crosses 1K it makes the stack 32K bigger, as a result anything less than 33K (e.g. 1K) is often rounded up to 33K. To obtain a more precise stack measurement use -kc2k which increases the stack in 2K chunks (but does cause only half the stack to be used, so bear that in mind when scaling -K numbers). That said, 32K corresponds to approximately 1000 stack evaluation slots, which suggests you don't have any significant space leaks.

Below are links to further information, including lots of practical examples of real space leaks caught using the method above.

Talks

Blog tales

Fixes

Code changes

Publications

Other approaches

Notes

On the main thread the stack limit is less effective, usually more like 8K if you request 1K. On spawned threads it seems much better. Solution is to always join . onceFork on the main thread.

More Repositories

1

hlint

Haskell source code suggestions
Haskell
1,459
star
2

ghcid

Very low feature GHCi based IDE
Haskell
1,130
star
3

shake

Shake build system
Haskell
744
star
4

hoogle

Haskell API search engine
Haskell
689
star
5

tagsoup

Haskell library for parsing and extracting information from (possibly malformed) HTML/XML documents
Haskell
231
star
6

bake

UNMAINTAINED: Continuous integration server
Haskell
130
star
7

record-dot-preprocessor

A preprocessor for a Haskell record syntax using dot
Haskell
129
star
8

weeder

Detect dead exports or package imports
Haskell
124
star
9

debug

Haskell library for debugging
JavaScript
121
star
10

rattle

Forward build system with speculation and caching
Haskell
102
star
11

extra

Extra Haskell functions
Haskell
93
star
12

cmdargs

Haskell library for command line argument processing
Haskell
91
star
13

build-shootout

Comparison of build program expressive power
Haskell
88
star
14

uniplate

Haskell library for simple, concise and fast generic operations.
Haskell
74
star
15

safe

Haskell library for safe (pattern match free) functions
Haskell
45
star
16

ghc-make

An alternative to ghc --make which supports parallel compilation of modules and runs faster when nothing needs compiling.
Haskell
40
star
17

interpret

Rust
38
star
18

neil

General tools for Neil
Haskell
38
star
19

profiterole

GHC prof manipulation script
Haskell
30
star
20

nsis

Haskell DSL for producing Windows Installer using NSIS
Haskell
26
star
21

derive

A Haskell program and library to derive instances for data types
TeX
25
star
22

supero

Haskell optimisation tool based on supercompilation
Haskell
25
star
23

hexml

A bad XML parser
C
19
star
24

offline-stack

Install Stack without internet access
Haskell
18
star
25

catch

Haskell pattern match analsyis checker
Haskell
15
star
26

js-jquery

Haskell library to obtain minified jQuery code
Haskell
9
star
27

rexe

.exe forwarder, to allow replacing binaries on PATH
Haskell
8
star
28

office

Macros for Microsoft Office
VBA
7
star
29

VSHaskell

Visual Studio 2010 addin
C#
7
star
30

record-hasfield

A version of HasField that will be available in future GHC
Haskell
7
star
31

shake-paper

Paper on the new GHC Shake-based build system
HTML
6
star
32

lasagna

Checker for Haskell layering violations
Haskell
5
star
33

guihaskell

A graphical REPL and development environment for Haskell
Haskell
5
star
34

hwwg

Haskell Website Working Group
5
star
35

shake-bazel

Experimenting with Shake and Bazel combined
Haskell
4
star
36

blogs

TeX
4
star
37

haskell-parser

Haskell parser based on that from GHC
Haskell
4
star
38

filepattern

A file path matching library
Haskell
4
star
39

idris-playground

Playing around with Idris
Idris
4
star
40

proplang

A Haskell library for functional GUI development
Haskell
4
star
41

thesis

My PhD thesis - Transformation and Analysis of Functional Programs
Haskell
3
star
42

js-flot

Haskell library to obtain minified Flot code
Haskell
3
star
43

qed

Experiments writing a prover
Haskell
3
star
44

core-playground

Simple Core language for Haskell experiments
Haskell
3
star
45

js-dgtable

Haskell library to obtain minified jquery.dgtable code
JavaScript
3
star
46

neil-check

Run neil and hlint on all my projects
Haskell
3
star
47

shake-examples

3
star
48

proof

Haskell library for writing proofs
Haskell
2
star
49

ci-check

Test the various supported CI's
2
star
50

hogle-dead

This repo has been moved to the master branch of https://github.com/ndmitchell/hoogle.
Haskell
2
star
51

stack-3137

Reproduce https://github.com/commercialhaskell/stack/issues/3137
Haskell
2
star
52

winhaskell

Windows Haskell GUI interpretter
C++
1
star
53

fossilizer

Generate 3D images of fossil bedding surfaces
JavaScript
1
star
54

bug-i386-ghc84

1
star
55

proposition

Haskell library for manipulating propositions.
Haskell
1
star
56

tex2hs

A program to check for type errors in a Latex document
Haskell
1
star
57

ndmitchell

1
star
58

hlint-test

Haskell
1
star
59

ghc-process

Compiling and linking files in a single process using the GHC API
Haskell
1
star
60

shark

A bad replacement for cabal/stack
1
star
61

index-search

Searching compressed text indicies
C
1
star
62

awesomo

Prototype optimiser for Haskell programs
Haskell
1
star
63

ninjasmith

Ninja file generator and tester
Haskell
1
star
64

firstify

A Haskell library to transform Yhc Core programs to first-order
TeX
1
star