1

Because in pure functional programming objects are all immutable, is it still possible to create memory leak?

By pure functional program, I meant there are no side effect. Of course, this is not realistic since every program has I/O. But, let's just ignore the I/O for now.

Will Ness
  • 70,110
  • 9
  • 98
  • 181
gyoho
  • 799
  • 2
  • 9
  • 25
  • You can cause `Stack Overflow` with recursive functions. very easy. – Tamar Jun 20 '17 at 04:06
  • You can also hold onto the head of a sequence. – Nathan Hughes Jun 20 '17 at 04:13
  • You might find this interesting: https://stackoverflow.com/questions/9952602/does-haskell-require-a-garbage-collector – Evan Trimboli Jun 20 '17 at 04:14
  • 1
    How do you define memory leak in this context? Also, there is no pure functional program, otherwise you would never see a result of your program. – Henry Jun 20 '17 at 04:15
  • I think by the definition, `stack overflow` is not memory leak since you don't loose any references. – gyoho Jun 20 '17 at 04:15
  • @gyoho You also don't loose any references in Java. Yet one can have memory leaks by keeping around references that are no longer needed. – Henry Jun 20 '17 at 04:17
  • @Henry I see your point. So, memory leak is not a matter of FP or OOP. It's really about memory management. – gyoho Jun 20 '17 at 04:23
  • Echoing previous comments, how do you define a memory leak? – Joel Cornett Jun 20 '17 at 04:41
  • @Henry "there is no pure functional program, otherwise you would never see a result of your program." not true, if printing is part of run-time system that runs your pure program. which it is, for any pure FPL. your pure FP program constructs an IO program, that is run by RTS *later*. that's the trick. – Will Ness Jun 20 '17 at 06:50

1 Answers1

2

I think this is a matter of how we define a memory leak. A program that runs for a very long time (potentially endlessly, like a server) can have a memory usage that is bounded or that grows more and more the longer the program is running. In the latter case, one speaks usually of a memory leak.

It is easy to write a functional program that needs more and more memory the longer it runs. So in that sense, memory leaks are possible.

Henry
  • 42,982
  • 7
  • 68
  • 84