0
0
Angularframework~20 mins

Unsubscribing and memory leaks in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Memory Leak Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens if you don't unsubscribe from an Observable in Angular?

Consider an Angular component that subscribes to an Observable but never unsubscribes. What is the most likely outcome?

Angular
import { Component, OnInit } from '@angular/core';
import { interval } from 'rxjs';

@Component({
  selector: 'app-timer',
  template: `<p>Timer running</p>`
})
export class TimerComponent implements OnInit {
  ngOnInit() {
    interval(1000).subscribe(val => console.log(val));
  }
}
AThe Observable will keep emitting values even after the component is destroyed, causing a memory leak.
BThe Observable automatically stops emitting when the component is destroyed, so no memory leak occurs.
CAngular automatically unsubscribes from all Observables when the component is destroyed.
DThe Observable will throw an error if not unsubscribed before component destruction.
Attempts:
2 left
💡 Hint

Think about what happens to subscriptions when components are removed from the DOM.

📝 Syntax
intermediate
2:00remaining
Which code snippet correctly unsubscribes from an Observable in Angular using ngOnDestroy?

Choose the code that properly unsubscribes from a subscription when the component is destroyed.

Angular
import { Component, OnInit, OnDestroy } from '@angular/core';
import { Subscription, interval } from 'rxjs';

@Component({
  selector: 'app-example',
  template: `<p>Example</p>`
})
export class ExampleComponent implements OnInit, OnDestroy {
  private sub?: Subscription;

  ngOnInit() {
    this.sub = interval(1000).subscribe();
  }

  ngOnDestroy() {
    // Unsubscribe here
  }
}
AngOnDestroy() { this.sub.stop(); }
BngOnDestroy() { this.sub = null; }
CngOnDestroy() { this.sub.unsubscribe(); }
DngOnDestroy() { this.sub.complete(); }
Attempts:
2 left
💡 Hint

Look for the method that stops the subscription safely.

🔧 Debug
advanced
2:00remaining
Why does this Angular component cause a memory leak?

Examine the code below. Why does it cause a memory leak?

Angular
import { Component, OnInit } from '@angular/core';
import { interval } from 'rxjs';

@Component({
  selector: 'app-leaky',
  template: `<p>Leaky Component</p>`
})
export class LeakyComponent implements OnInit {
  ngOnInit() {
    interval(1000).subscribe(val => console.log(val));
  }
}
ABecause the subscription is not stored and cannot be unsubscribed later.
BBecause interval() emits only once and then completes.
CBecause console.log causes the memory leak.
DBecause ngOnInit is not called when the component is created.
Attempts:
2 left
💡 Hint

Think about how to stop an Observable subscription.

🧠 Conceptual
advanced
2:00remaining
What is the best practice to avoid memory leaks with multiple subscriptions in Angular?

You have multiple subscriptions in a component. Which approach best avoids memory leaks?

ACall unsubscribe on each subscription separately in ngOnDestroy without grouping.
BStore all subscriptions in a Subscription object and unsubscribe once in ngOnDestroy.
CRely on Angular to automatically unsubscribe all subscriptions on component destroy.
DUse setTimeout to delay unsubscription until after component destruction.
Attempts:
2 left
💡 Hint

Think about managing multiple subscriptions efficiently.

state_output
expert
3:00remaining
What is the console output after destroying this Angular component?

Consider this Angular component. What will be logged to the console after the component is destroyed?

Angular
import { Component, OnInit, OnDestroy } from '@angular/core';
import { interval, Subscription } from 'rxjs';

@Component({
  selector: 'app-test',
  template: `<p>Test Component</p>`
})
export class TestComponent implements OnInit, OnDestroy {
  private sub: Subscription = new Subscription();

  ngOnInit() {
    const sub1 = interval(500).subscribe(val => console.log('A', val));
    const sub2 = interval(700).subscribe(val => console.log('B', val));
    this.sub.add(sub1);
    this.sub.add(sub2);
  }

  ngOnDestroy() {
    this.sub.unsubscribe();
  }
}
AOnly 'B' logs stop; 'A' logs continue after destruction.
BLogs 'A' and 'B' continue indefinitely after destruction.
COnly 'A' logs stop; 'B' logs continue after destruction.
DNo logs appear after component destruction; all intervals stop.
Attempts:
2 left
💡 Hint

Consider what happens when you unsubscribe a Subscription that contains multiple subscriptions.