0

I have an observable that I want to release the memory that is uses. When setting in to null, the Garbage Collector is not collecting it becuase other he is referenced in some subscription.

The question is - how can I remove all the subscribers?

for example:

creating:

this.x = ko.observable();

removing:

this.x(null);

but it is not removed becuase it is referenced in some subscription:

this.y.subscribe(function (newValue) {
   var z = this.x();
};

I dont want to dispose the subscription - becacuse for a new value for x I want y to subscribe to it.

I hope it is clear.

Thanks

Michael Best
  • 16,623
  • 1
  • 37
  • 70
amhev
  • 313
  • 1
  • 3
  • 12

1 Answers1

1

The problem is that you've made a copy of its contents in code that does not update when x is updated. I realize you're just giving an example, but subscribe is generally a code smell, and your situation suggests to me that you should be making a computed that would update when x changes, which would solve your problem.

You cannot get rid of all the memory associated with x and at the same time keep a copy of its contents.

Roy J
  • 42,522
  • 10
  • 78
  • 102