Fix the heap snapshot diff for a memory leak
Your task
A Node.js service's memory climbs until it runs out every few days. An AI blamed a listener leak with high confidence. Before you refactor hot-path code on that guess, you diff two heap snapshots taken an hour apart and rank the object types that actually grew.
An assistant wrote rank_heap_growth(labels, before_counts, after_counts, min_growth). It is in the editor. It has a bug. Find it and fix it.
Input and output:
labels: one object type per row, such asClosure. A label may appear in more than one row.before_counts,after_counts: the retained count for each row in the first and second snapshot. Either list may be shorter thanlabels.min_growth: the noise floor.- Return
"<label>=<growth>"strings for the suspects.
Rules the tests check:
- A row's growth is its after count minus its before count. A count missing because its list is too short counts as 0.
- Add up the growth of all rows with the same label.
- A label is a suspect only when its total growth is greater than 0 and at least
min_growth. A label that stayed flat or shrank is never a suspect, whatevermin_growthis. - Sort the suspects by growth, largest first. Break ties by label in ascending order.
Examples
rank_heap_growth(labels: list[str], before_counts: list[int], after_counts: list[int], min_growth: int) -> list[str]Example 1
Input
rank_heap_growth(["EventEmitter", "String", "Buffer", "Closure"], [120, 5000, 40, 900], [131, 41000, 38, 5200], 100)Output
["String=36000", "Closure=4300"]Example 2
Input
rank_heap_growth(["Zebra", "Alpha", "Zebra"], [0, 0, 0], [5, 10, 5], 1)Output
["Alpha=10", "Zebra=10"]