I understand that I must unsubscribe certains Observable (ie: Observables that have infinite value) when Components are destroyed in order to prevent memory leaks. I do not need to do that for finite Observables as they will complete and automatically unsubscribe
.
But if I create an infinite Observable
in my component (for example FormGroup.valueChanges
, or QueryList.changes
), this one will be destroyed with the component that contains it, so I think that they will be no memory leak even if I do not unsubscribe from it.
Here is a simple example:
@Component({})
export class DummyComponent {
form: FormGroup;
constructor(private fb: FormBuilder) {
this.form = this.fb.group({
firstName: [''],
lastName: ['']
});
this.form.valueChanges.subscribe(
x => console.log(x)
);
}
}
Here, I do not unsubscribe
from this.form.valueChanges
; when my component is destroyed, the this.form.valueChanges
will be destroyed too.
Will there be a memory leak in this case?
this.form.valueChanges
). – petitcl