r/programming • • Oct 12 '17

JavaScript's Promise Leaks Memory

https://alexn.org/blog/2017/10/11/javascript-promise-leaks-memory.html
26 Upvotes

19 comments sorted by

11

u/slaymaker1907 Oct 12 '17

I agree with the author and think this is a rather serious issue. I think a lot of people are wondering when this would be an issue, but there are actually a lot of common cases where this could happen.

For instance, if I'm using promises to break up a long running synchronous task, it would be very easy to get 1000+ chained promises.l This would be useful for things like parsers or encryption, very CPU itensive processes.

20

u/EntroperZero Oct 13 '17

tl;dr: Infinite tail call recursion uses infinite memory.

The author argues that tail call optimization, or the equivalent for Promises, could be used to avoid consuming infinite memory for infinite tail call recursion.

8

u/hotrodx Oct 12 '17

Patient: Doctor, It Hurts Whenever I Do This.
Doctor: Well, Then Don't DO That!

11

u/freebit Oct 12 '17

It's not true. The title is misleading.

11

u/drysart Oct 12 '17

It is true, just conditional. Add "in certain unlikely circumstances" to the title and you've got a more complete summary.

8

u/[deleted] Oct 12 '17

What should it say?

Should be more like node promises leak memory in certain scenarios?

12

u/evaned Oct 12 '17

leak memory

I'm not even convinced it's a memory leak. In a GC'd language that would mean a runtime or GC bug. I don't really fully understand what is going on, but it doesn't sound to me like that is what is happening. It sounds like it's just chaining a bunch of objects together and because everything is still accessible it's not able to clean things up. But if everything is accessible, it's not a leak.

(I've heard the term "memory drag" used for objects that could be cleaned up from a program semantic view but aren't, but that aren't actually garbage. Using "memory leak" for that makes it harder to talk about both that and actual memory leaks.)

10

u/freebit Oct 12 '17

I'm not convinced either. It just looks like an incorrectly used(abused?) promise and a bunch of recursion that uses up all memory.

7

u/[deleted] Oct 12 '17

It sounds like it's just chaining a bunch of objects together and because everything is still accessible it's not able to clean things up.

From my understanding, that's exactly what it is. There seems to be an optimization that can drop unneeded links, but I wouldn't exactly call unoptimized a leak/bug.

2

u/Eirenarch Oct 12 '17

object leak is my preferred term.

4

u/[deleted] Oct 13 '17

[removed] — view removed comment

3

u/EntroperZero Oct 14 '17

My first task at my first job was to find a memory leak in an importer. The importer was caching things in 3 caches, and someone did a copy/paste error and was searching the wrong cache for the object, always failing to find it, fetching it from the database, and then storing it in the correct cache. The correct cache got infinitely large.

And of course there was much arguing over whether to call this a "memory leak" or something else. Shrug, the application was steadily consuming memory and never, ever accessing those objects.

1

u/evaned Oct 13 '17

The distinction has major implications for what you look for when diagnosing the problem, how you look and what tools you use, how you fix it, and what part of the program or runtime needs to be fixed.

If you just look at the user-visible effect of it crashing, you might as well call it a null-pointer dereference.

(OK, I'm exaggerating there because an NPD won't gobble up resources used by other programs. But we're on a programming forum for programmers discussing a technically-in-depth blog entry written by a programmer for programmers. This isn't some Guardian article written for a wider audience or something like that. The distinction is important.)

0

u/[deleted] Oct 13 '17

[deleted]

3

u/swaggler Oct 13 '17

Oh yes, please tell us the difference.

0

u/[deleted] Oct 13 '17

[deleted]

3

u/swaggler Oct 14 '17

Can you please explain the difference between functional programming, and pure functional programming?

0

u/[deleted] Oct 14 '17

[deleted]

7

u/swaggler Oct 14 '17

There is a FP course running next week. Want to come?

I don't want anyone to get shot.

1

u/Tarmen Oct 16 '17 edited Oct 16 '17

The problem is that "have no side effects" is a really wonky category. Consuming cpu time is a side effect from the view of a side channel attack. No side effects from the view of the program works better but runs into other issues.

Also, the world-passing thing is more of an implementation detail of the current implementation of monadic io in ghc - it just encodes the implicit sequencing similar to a memory barrier in cpu's.

And tco is used in a ton of non-pure functional languages. Well, your definition of functional includes languages like java so maybe it isn't the majority but virtually all traditional functional languages at least allow it.