Garbage collection is the automatic process where Node.js frees up memory by removing objects that are no longer needed or referenced in the program.
Click to reveal answer
beginner
Why should you avoid global variables in Node.js for memory management?
Global variables stay in memory for the lifetime of the application, which can cause memory to be used unnecessarily and lead to memory leaks.
Click to reveal answer
intermediate
How can you detect memory leaks in a Node.js application?
You can use tools like Chrome DevTools, Node.js built-in inspector, or third-party modules to monitor memory usage and find objects that keep growing without being released.
Click to reveal answer
intermediate
What is the benefit of using streams in Node.js for memory management?
Streams process data in small chunks instead of loading everything into memory at once, which reduces memory usage and improves performance.
Click to reveal answer
intermediate
Explain the role of the 'heap' and 'stack' in Node.js memory management.
The stack stores simple, short-lived data like function calls and variables, while the heap stores objects and data that can grow dynamically. Efficient use of both helps manage memory well.
Click to reveal answer
Which of the following helps prevent memory leaks in Node.js?
AAvoiding global variables
BUsing synchronous file reads only
CIgnoring unused objects
DDisabling garbage collection
✗ Incorrect
Avoiding global variables helps prevent memory leaks because globals stay in memory for the app's lifetime.
What Node.js feature processes data in chunks to save memory?
ACallbacks
BPromises
CStreams
DEvents
✗ Incorrect
Streams handle data in small parts, reducing memory use compared to loading all data at once.
Which tool can you use to inspect memory usage in Node.js?
AChrome DevTools
BGit
CNPM
DWebpack
✗ Incorrect
Chrome DevTools can connect to Node.js to inspect memory and debug leaks.
What does garbage collection do in Node.js?
AManually frees memory
BAutomatically frees unused memory
CIncreases memory usage
DPrevents code execution
✗ Incorrect
Garbage collection automatically frees memory that is no longer used.
Where are objects stored in Node.js memory?
ARegister
BStack
CCache
DHeap
✗ Incorrect
Objects are stored in the heap, which handles dynamic memory allocation.
Describe three best practices to manage memory effectively in a Node.js application.
Think about how to keep memory usage low and detect problems early.
You got /3 concepts.
Explain how garbage collection works and why it is important in Node.js.
Consider how Node.js cleans up memory without programmer intervention.
You got /3 concepts.
Practice
(1/5)
1. Which of the following is a good practice to help Node.js manage memory efficiently?
easy
A. Use streams to process large data instead of loading it all at once
B. Keep all data in global variables for easy access
C. Avoid using asynchronous functions to reduce complexity
D. Never clear timers or intervals once set
Solution
Step 1: Understand memory usage with large data
Loading large data all at once uses a lot of memory. Streams process data in small chunks, saving memory.
Step 2: Evaluate options for memory efficiency
Using global variables or never clearing timers can cause memory leaks. Avoiding async functions doesn't help memory.
Final Answer:
Use streams to process large data instead of loading it all at once -> Option A
Quick Check:
Streams save memory by chunking data [OK]
Hint: Streams handle big data efficiently, avoid globals [OK]
Common Mistakes:
Thinking global variables are memory efficient
Ignoring the need to clear timers
Believing async functions cause memory leaks
2. Which syntax correctly clears a timeout timer in Node.js to prevent memory leaks?
easy
A. clearTimeout(timerId);
B. clearInterval(timerId);
C. stopTimer(timerId);
D. removeTimer(timerId);
Solution
Step 1: Identify the correct function to clear a timeout
Node.js uses clearTimeout() to stop timers set by setTimeout().
Step 2: Match function to timer type
clearInterval() clears intervals, not timeouts. stopTimer() and removeTimer() are invalid.
Final Answer:
clearTimeout(timerId); -> Option A
Quick Check:
clearTimeout() stops setTimeout timers [OK]
Hint: Use clearTimeout for setTimeout timers [OK]
Common Mistakes:
Confusing clearTimeout with clearInterval
Using non-existent functions like stopTimer
Not clearing timers at all
3. What will be the output of the following code snippet regarding memory usage?