From HaskellWiki
< Performance
Revision as of 15:59, 5 November 2007 by Ketil (talk | contribs) (sketched a brief page describing heap profiling)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Haskell Space Performance

Haskell programs will sometimes consume a lot more memory than necessary, and this is often due to too much, or too little, laziness.


One common culprit is Haskell's [Char]-based IO. For example, if you are reading a file lazily, and storing information into lazy data structure, the data structure will keep unevaluated pointers to the list of characters, potentially keeping the whole file in memory as a very expensive linked list of characters.

One solution is to use Data.ByteString to reduce the cost per character. This is often effective, but you should also consider if you can avoid the problem by more careful evaluation.

Heap profiling

Heap profiling is an important tool in combating excessive memory usage, and is available (at least) in GHC and NHC.

For GHC, compile your program for profiling, using the '-prof -auto-all' flags. Then run your program, adding +RTS -h to the command line. This will result in a file with the same name as your program, but with an additional .hp suffix.

Use the 'hp2ps' program to turn this into a nice graph of the heap in Postscript format, suitable for printing or viewing with e.g. 'gv'. The graph should help you pinpoint the culprit.

Further reading

See the GHC heap profiling documentation for further options.