Explain memory allocation and how you would debug memory-related issues in a system program.
Implement analyze_memory(events) to simulate allocation and release events. Each event is an array representing either ['alloc', block_id, size] or ['free', block_id]; return a map containing invalid free count, leaked block count, leaked bytes, and peak allocated bytes.
def analyze_memory(events):