Fix the heap trend check for OOM triage
Your task
A Java service throws OutOfMemoryError under load. An AI kept answering with a bigger -Xmx, then System.gc() calls, then a different collector, and the errors kept coming back. Your team now classifies the heap from the live set after each full collection before anyone changes a flag. A live set that keeps growing is a leak, and more heap only delays it.
An assistant wrote classify_heap_trend(post_gc_used_mb, heap_max_mb). It is in the editor. It has a bug. Find it and fix it.
Input and output:
post_gc_used_mb: heap in use right after each full collection, in MB, oldest first.heap_max_mb: the maximum heap size, in MB.- Return
"insufficient_data","leak","undersized"or"healthy".
Rules the tests check, in order:
- If
heap_max_mbis 0 or less, or there are fewer than 3 samples:"insufficient_data". - If no sample is lower than the one before it, and the last sample is higher than the first:
"leak". Samples may stay level for a while. - If the highest sample is at least 85% of
heap_max_mb, that is, highest × 100 is at leastheap_max_mb× 85:"undersized". - Otherwise:
"healthy".
Examples
classify_heap_trend(post_gc_used_mb: list[int], heap_max_mb: int) -> strExample 1
Input
classify_heap_trend([900, 1200, 1500, 1800], 4096)Output
"leak"Example 2
Input
classify_heap_trend([3700, 3650, 3720, 3690], 4096)Output
"undersized"Example 3
Input
classify_heap_trend([400, 380, 410, 395], 4096)Output
"healthy"