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 6057861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:32:06+00:00 2026-05-23T08:32:06+00:00

Here’s a simple program that blows my heap to Kingdom Come: intersect n k

  • 0

Here’s a simple program that blows my heap to Kingdom Come:

intersect n k z s rs c
  | c == 23   = rs
  | x == y    = intersect (n+1) (k+1) (z+1) (z+s) (f : rs) (c+1)
  | x < y     = intersect (n+1) k (z+1) s rs c
  | otherwise = intersect n (k+1) z s rs c
    where x = (2*n*n) + 4 * n
          y = (k * k + k )
          f = (z, (x `div` 2), (z+s))
p = intersect 1 1 1 0 [] 0

main = do
  putStr (show p)

What the program does is calculate the intersection of two infinite series, stopping when it reaches 23 elements. But that’s not important to me.

What’s interesting is that as far as I can tell, there shouldn’t be much here that is sitting on the heap. The function intersect is recursives with all recursions written as tail calls. State is accumulated in the arguments, and there is not much of it. 5 integers and a small list of tuples.

If I were a betting person, I would bet that somehow thunks are being built up in the arguments as I do the recursion, particularly on arguments that aren’t evaluated on a given recursion. But that’s just a wild hunch.

What’s the true problem here? And how does one fix it?

  • 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-05-23T08:32:07+00:00Added an answer on May 23, 2026 at 8:32 am

    If you have a problem with the heap, run the heap profiler, like so:

    $ ghc -O2 --make A.hs -prof -auto-all -rtsopts -fforce-recomp
    [1 of 1] Compiling Main             ( A.hs, A.o )
    Linking A.exe ...
    

    Which when run:

    $ ./A.exe +RTS -M1G -hy
    

    Produces an A.hp output file:

    $ hp2ps -c A.hp
    

    Like so:

    enter image description here

    So your heap is full of Integer, which indicates some problem in the accumulating parameters of your functions — where all the Integers are.

    Modifying the function so that it is strict in the lazy Integer arguments (based on the fact you never inspect their value), like so:

    {-# LANGUAGE BangPatterns #-}
    
    intersect n k !z !s rs c
      | c == 23   = rs
      | x == y    = intersect (n+1) (k+1) (z+1) (z+s) (f : rs) (c+1)
      | x < y     = intersect (n+1) k (z+1) s rs c
      | otherwise = intersect n (k+1) z s rs c
        where x = (2*n*n) + 4 * n
              y = (k * k + k )
              f = (z, (x `div` 2), (z+s))
    
    p = intersect 1 1 1 0 [] 0
    
    main = do
      putStr (show p)
    

    And your program now runs in constant space with the list of arguments you’re producing (though doesn’t terminate for c == 23 in any reasonable time).

    enter image description here

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

Sidebar

Related Questions

Here is my question. I am having this simple menu. <div id=menu> <ul> <li>
Here's a simple, 45 line console application that reproduces the bug on my Win
Here's a basic regex technique that I've never managed to remember. Let's say I'm
Here is the issue I am having: I have a large query that needs
Here's my scenario - I have an SSIS job that depends on another prior
Here's a coding problem for those that like this kind of thing. Let's see
Here is the scenario: I'm writing an app that will watch for any changes
Here is a shell script: echo Starting Jarvis Program D. ALICE_HOME=. SERVLET_LIB=lib/servlet.jar ALICE_LIB=lib/aliceserver.jar JS_LIB=lib/js.jar
I'm getting an error here that says I haven't defined a method, but it
This is beyond both making sense and my control. That being said here is

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.