Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Haskell
Wiki community
Recent changes
Random page
HaskellWiki
Search
Search
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Memory leak
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Types of leaks == === Holding a reference for a too long time === Consider for example: <haskell> let xs = [1..1000000::Integer] in sum xs * product xs </haskell> Since most Haskell compilers expect that the programmer used <hask>let</hask> in order to share <hask>xs</hask> between the call of <hask>sum</hask> and the call of <hask>product</hask>, the list <hask>xs</hask> is completely materialized and held in memory. However, the list <hask>xs</hask> is very cheap to compute, and thus memory usage can be reduced considerably by computing <hask>xs</hask> in each call. To achieve this while avoiding code duplication, we can turn the list definition into a function with a dummy argument. <haskell> let makeXs n = [1..n::Integer] in sum (makeXs 1000000) * product (makeXs 1000000) </haskell> === Building up unevaluated expressions === Another typical cause of memory leaks are unevaluated expressions, the classical example being to sum up the numbers of a list (known as <hask>sum</hask> function). <haskell> foldl (+) 0 [1..1000000::Integer] </haskell> The problem is, that the runtime system does not know, whether the intermediate sums are actually needed at a later point, and thus it leaves them unevaluated. I.e. it stores something equivalent to <hask>1+2+3+4</hask> instead of just <hask>10</hask>. You may be lucky that the [[strictness analyzer]] already removes the laziness at compile time, but in general you cannot rely on it. The safe way is to use [[seq]] to force evaluation of intermediate sums. This is done by <hask>foldl'</hask>. <haskell> foldl' (+) 0 [1..1000000::Integer] </haskell> === Keeping not needed references alive === Consider the following definition: <haskell> x = fst (a, b) </haskell> Until we evaluate <hask>x</hask>, both references <hask>a</hask> and <hask>b</hask> are kept alive. After evaluating <hask>x</hask>, <hask>b</hask> can be garbage collected if <hask>b</hask> is not referenced elsewhere.
Summary:
Please note that all contributions to HaskellWiki are considered to be released under simple permissive license (see
HaskellWiki:Copyrights
for details). If you don't want your writing to be edited mercilessly and redistributed at will, then don't submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
DO NOT SUBMIT COPYRIGHTED WORK WITHOUT PERMISSION!
Cancel
Editing help
(opens in new window)
Toggle limited content width