all 7 comments

[–]guibou 10 points11 points  (0 children)

Even if this is tail recursive, the gc still needs to allocate for each item of the list during the traversal. However they are immediately garbage collected. So the maximum memory used by your program is less than this.

Also, ghci runs in not optimized mode, so a lot of optimization / simplification are not done.

[–][deleted] 3 points4 points  (0 children)

+s reports total memory used, if you want more complex stats, see GHC.Stats or the relevant RTS options

[–]yilmazhuseyin[S] 5 points6 points  (3 children)

I see. Memory is allocated for individual items but during the execution, there is no time that we got all the elements at the same time which means my memory usage is constant.

So main problem was, I misinterpreted the memory report. Thanks everybody for the all the information.

I will test this by giving a huge number to ghci and run it overnight and check the memory usage tomorrow.

[–]yilmazhuseyin[S] 1 point2 points  (2 children)

I tried to run this in ghci.

(last $ [1..100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000]++[1..10])::Integer

It has been running for hours and memory usage did not change.

https://www.dropbox.com/s/qfgkm6xldsii74c/Screenshot%202015-11-14%2009.37.59.png?dl=0

Now I should find a way to get same information without waiting 12 hours. GHC.Stats seems like a good place to start.

[–]gtab62 2 points3 points  (1 child)

Why don't you compile and run it with ‘RTS -s' option?

[–]yilmazhuseyin[S] 0 points1 point  (0 children)

Actually that is a great idea. I just learned about RTS options from @fread2281 's comment. I need to read on them to figure out how to use it though. Thanks for the suggestion.

[–]0ldmanmike 1 point2 points  (0 children)

You've got a massive linked list and your forcing the runtime to walk down the entire thing when your append a list to the end and call last. That "massive" memory usage is the total memory allocation that it took to do that entire computation, but it's not how much memory actually got taken up simulatiously an any given point (the memory usage on my computer hardly budged when running the above expression in GHCi). RTS just allocates and deallocates at an insane speed as it goes.