Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8312071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:02:06+00:00 2026-06-08T20:02:06+00:00

In GHCI, I run this simple test: encodeFile test [0..10000000] The line runs really

  • 0

In GHCI, I run this simple test:

encodeFile "test" [0..10000000]

The line runs really quickly (<10sec), but my memory usage shoots up to ~500MB before it finishes. Shouldn’t encodeFile be lazy since it uses ByteString.Lazy?


Edit: Roman’s answer below is great! I also want to point out this answer to another question, that explains why Data.Binary does strict encoding on lists and provides a slightly more elegant work around.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T20:02:09+00:00Added an answer on June 8, 2026 at 8:02 pm

    Here’s how serialization of lists is defined:

    instance Binary a => Binary [a] where
        put l  = put (length l) >> mapM_ put l
    

    That is, first serialize the length of the list, then serialize the list itself.

    In order to find out the length of the list, we need to evaluate the whole list.
    But we cannot garbage-collect it, because its elements are needed for the second
    part, mapM_ put l. So the whole list has to be stored in memory after the
    length is evaluated and before the elements serialization starts.

    Here’s how the heap profile looks like:

    profile

    Notice how it grows while the list is being built to compute its length, and
    then decreases while the elements are serialized and can be collected by the GC.

    So, how to fix this? In your example, you already know the length. So you
    can write a function which takes the known length, as opposed to computing it:

    import Data.Binary
    import Data.ByteString.Lazy as L
    import qualified Data.ByteString as B
    import Data.Binary.Put
    
    main = do
      let len = 10000001 :: Int
          bs = encodeWithLength len [0..len-1]
      L.writeFile "test" bs
    
    putWithLength :: Binary a => Int -> [a] -> Put
    putWithLength len list =
      put len >> mapM_ put list
    
    encodeWithLength :: Binary a => Int -> [a] -> ByteString
    encodeWithLength len list = runPut $ putWithLength len list
    

    This program runs within 53k of heap space.

    You can also include a safety feature into putWithLength: compute the length while serializing the list, and check with the first argument in the end. If there’s a mismatch, throw an error.

    Exercise: why do you still need to pass in the length to putWithLength instead of using the computed value as described above?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tried to run code from http://gregorycollins.net/posts/2011/10/01/cufp2011/index.html#(43 ) in GHCi, but got Floating point
In GHCI, if you run: import Data.Binary encode [1] Everything works smoothly. But, if
Note the second line in this GHCi session. What is it about the Latitude
Consider the following: do putStr return $ map read [2] Run in GHCI, this
In test.hs, I have: doubleMe x = x + x In ghci, I type:
In Python one can say this: python script.py from the command line and receive
I'm using ghci to do some incremental development using Emacs' run-haskell . Every once
When I'm testing my function intervalFinder in GHCI it seems to be working, but
When I do something simple in ghci, like the following: let x = 7
I am experimenting with wxHaskell. I wasn't able to run the app under ghci,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.