2

We are asking from deferred a promise, register callback on it, and never destroy this object or clean the callback. Who is manage the promise life cycle (Who is destroy it)? Who (and how) is manage the deffered life cycle? What about memory-leaks?

Thanks!

Shula
  • 167
  • 1
  • 18
  • whenever the scope of promise object is over, unless in closure, like every other type of object, it maybe garbage collected, as garbage collection is [undecidable problem](https://en.wikipedia.org/wiki/Undecidable_problem). – vinayakj Jul 19 '15 at 13:06
  • @vinayakj scope resolution and finding references is not an undecidable problem - it's quite decidable if you forbid `eval` (technically, variable unbound eval) and the function constructor (as eval). That said that's not all there is to it - it might leak anyway :) – Benjamin Gruenbaum Jul 19 '15 at 16:11
  • @BenjaminGruenbaum If you read my comment carefully its `as garbage collection is undecidable problem.` for further information please read [JavaScript/Memory_Management](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_Management#Release_when_the_memory_is_not_needed_anymore) once. – vinayakj Jul 19 '15 at 17:25
  • For OP too please read [JavaScript/Memory_Management](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_Management#Release_when_the_memory_is_not_needed_anymore) once. The promise object is also just an object. – vinayakj Jul 19 '15 at 17:28
  • The fact I read your comment carefully doesn't make it less irrelevant. Here, read something relevant. In this paper Mark Miller shows how by a few restrictions on `eval` usage you can track references http://www-cs-students.stanford.edu/~ataly/Papers/sp11.pdf it's actually a pretty trivial proof here. Anyway, this is irrelevant because even if GC was an `O(1)` problem it would still not ensure no memory leaks and even if GC is undecidable by an oracle turning machine it doesn't mean it doesn't leak. – Benjamin Gruenbaum Jul 19 '15 at 17:40

1 Answers1

3

This depends on the promise implementation.

I've answered this question for Angular promises in particular, however the conclusions from there generally apply to other implementations as well.

Basically, you have nothing to worry about with reasonable promise implementations, the garbage collector will detect when your promise goes "out of scope" and there are no references to it and deallocates it.

Community
  • 1
  • 1
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504