Summary - Memory internals
- Node.js course
- Expert
- Lesson 24/47
- 5 min
Combine heap, GC and snapshot to write a memory profiler.
Summary - Memory internals
In this group we saw Node.js memory internals: the heap/stack split, generational GC, mark-sweep, memory leaks and heap snapshots. Each addresses how memory is managed and how leaks are found.
Heap/stack: objects on the heap, primitives on the stack. Generational GC: promotes survivors between young and old generations. Mark-sweep: marks reachable from roots, sweeps the rest. Memory leak: cache growth past a limit. Heap snapshot: a list of objects with name and size. In the final lesson we combine them in a report.
The report function
The final task combines memory concepts into one report. The memoryReport() function joins region, gc and leak info with ' | '. Output: stack+heap | generational | detected.
This report summarizes three aspects of memory: regions (stack+heap), garbage collection style (generational) and leak status (detected). Inside the function you place each piece in a variable, then join them with +. regions.join('+') combines the regions. This lets you see what you learned on the console; you model the structure without the uncertainty of running real memory management.
Practice Write the memory report
Write function memoryReport(). Define const regions = ['stack', 'heap']. Define const gc = 'generational'. Define const leak = 'detected'. Return regions.join('+') + ' | ' + gc + ' | ' + leak. Print console.log(memoryReport()).
Starter code
// Write a memoryReport function
// Combine region, gc and leak info
// Return 'stack+heap | generational | detected'
// Call and print
// write hereSample solution
function memoryReport() {
const regions = ['stack', 'heap'];
const gc = 'generational';
const leak = 'detected';
return regions.join('+') + ' | ' + gc + ' | ' + leak;
}
console.log(memoryReport());Expected output
stack+heap | generational | detected
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which generation is mark-sweep used for?
- Young
- Old Correct answer
- Stack
- Yok
Explanation: Mark-sweep is used for the old generation.
2 Match each concept to its role
| Term | Meaning |
|---|---|
Stack |
Primitives |
Heap |
Objects |
Generational GC |
Young/old generations |
Mark-sweep |
Marks reachable |