Vol. I · Case No. 001 Status: Solved Price: One Clue

Bugisman.com

Case No. 001 · Solved

The Python Script That Ate Its Own Memory

A nightly script was caught eating 8 GB of RAM. The suspect: a list that never let go.

The Incident

Every night at 02:00, a data-processing script woke up, read a few million records, and went quiet. By morning the server was gasping, with 8 GB of RAM gone and no ransom note left behind. Monitoring flagged the crime scene, but the script always exited cleanly, which made it look innocent.

The Investigation

Investigators profiled memory usage line by line and found the same suspect at every turn: a list that kept every record it had ever seen. Old rows were never released, so the list grew all night long. It was not malicious, just very clingy. Exhibit A below shows the suspect in action.

Exhibit A: the suspect

# Loads every record into memory at once
def process(path):
    records = []
    with open(path) as f:
        for line in f:
            records.append(parse(line))   # the list never lets go

    for record in records:
        save(record)

The Verdict

The list was replaced by a generator that handles one record at a time and lets go of it right after. Memory dropped from 8 GB to a few hundred MB, and the server has slept peacefully ever since. Exhibit B shows the fix.

Exhibit B: the fix

# Handles one record at a time using a generator
def read_records(path):
    with open(path) as f:
        for line in f:
            yield parse(line)

def process(path):
    for record in read_records(path):
        save(record)                      # released right after use

← Back to all investigations