<div class="Gk Jk" dir="ltr" style="color:rgb(34,34,34);line-height:22px;background-color:rgb(255,255,255)">See the comment for hGetContents:<div><br></div><div>&quot;<span style="line-height:18px;font-size:13px;font-family:sans-serif">This function reads chunks at a time, doubling the chunksize on each read. The final buffer is then realloced to the appropriate size. For files &gt; half of available memory, this may lead to memory exhaustion. Consider using </span><code style="line-height:16px;font-size:13px;margin:0px;padding:0px"><a href="http://hackage.haskell.org/packages/archive/bytestring/0.9.2.1/doc/html/Data-ByteString.html#v:readFile" target="_blank" style="color:rgb(171,105,84);text-decoration:none;margin:0px;padding:0px">readFile</a></code><span style="line-height:18px;font-size:13px;font-family:sans-serif"> in this case.&quot;</span></div>
<div><br></div><div><a href="http://hackage.haskell.org/packages/archive/bytestring/0.9.2.1/doc/html/Data-ByteString-Char8.html#g:31" target="_blank" style="color:rgb(17,85,204);text-decoration:none">http://hackage.haskell.org/packages/archive/bytestring/0.9.2.1/doc/html/Data-ByteString-Char8.html#g:31</a></div>
<div><br></div><div>Maybe try lazy bytestrings?</div><div style="clear:both"></div></div><div style="color:rgb(34,34,34);line-height:22px;background-color:rgb(255,255,255);clear:both"></div><div class="Gk Kk" dir="ltr" style="padding-top:1em;color:rgb(0,123,53);line-height:22px;background-color:rgb(255,255,255)">
<div><br></div><div>  - Clark</div></div><br>On Tuesday, April 16, 2013, Anatoly Yakovenko  wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>
-- So why does this code run out of memory?</div><div><br></div><div>import Control.DeepSeq</div><div>import System.IO</div><div>import qualified Data.ByteString.Char8 as BS</div><div><br></div>
<div>scanl&#39; :: NFData a =&gt; (a -&gt; b -&gt; a) -&gt; a -&gt; [b] -&gt; [a]</div><div>scanl&#39; f q ls =  q : (case ls of</div><div>                        []   -&gt; []</div><div>                        x:xs -&gt; let q&#39; = f q x</div>

<div>                                in q&#39; `deepseq` scanl&#39; f q&#39; xs)</div><div><br></div><div><br></div><div>main = do</div><div>   file &lt;- openBinaryFile &quot;/dev/zero&quot; ReadMode</div><div>   chars &lt;- BS.hGetContents file</div>

<div>   let rv = drop 100000000000 $ scanl&#39; (+) 0 $ map fromEnum $ BS.unpack chars</div><div>   print (head rv)</div><div><br></div><div>-- my scanl&#39; implementation seems to do the right thing, because </div><div>

<br></div><div>main = print $ last $ scanl&#39; (+) (0::Int) [0..]<br></div><div><br></div><div>-- runs without blowing up.  so am i creating a some thunk here?  or is hGetContents storing values?  any way to get the exception handler to print a trace of what caused the allocation?</div>

<div><br></div></div>
</blockquote>