---
title: "Will this collection really grow without limit? Find what removes entries | Proofline"
description: "An unbounded-growth claim holds when entries are added on a path that keeps running and nothing removes them on a schedule tied to that path."
url: https://proofline.sh/learn/ai-review-memory-growth-comment/
---

[Learning center](/learn/)

# Will this collection really grow without limit? Find what removes entries

An unbounded-growth claim holds when entries are added on a path that keeps running and nothing removes them on a schedule tied to that path. Find the code that removes entries, work out the largest size it allows at a realistic rate, and change the code only when that size grows with data or uptime. When the claim is that code loads a whole file or result set into memory, measure peak memory at the largest input you expect.

Code review The Proofline team November 22, 2026 5 min read

## Two kinds of memory claim

Automated reviewers raise two different claims under this heading. The first is about state that lives as long as the process: a dictionary keyed by request, a list of timestamps, a cache with no eviction. Its size depends on how often entries arrive and what removes them. Python's own [`deque`](https://docs.python.org/3/library/collections.html#collections.deque) documentation draws the line: "If _maxlen_ is not specified or is `None`, deques may grow to an arbitrary length." That says what the type allows. Whether your deque grows depends on the code that removes entries.

The second is about one operation that holds its whole input at once: reading a file with `read()`, building a list from a query or materializing every object a delete touches. Its size depends on the largest input, not on uptime. The two need different evidence.

## A whole-input claim, fixed and then measured

In August 2026, a NetBox pull request fixed an issue where deleting scripts with many recorded jobs caused memory use high enough to exhaust the server. The description explained the cause: "Django's deletion collector loads every related `Job` into memory". [Pull request](https://github.com/netbox-community/netbox/pull/22867) Django's documentation confirms the mechanism: "Django needs to fetch objects into memory to send signals and handle cascades", and only when there are no cascades and no signals can it "take a fast-path and delete objects without fetching into memory." [QuerySet.delete()](https://docs.djangoproject.com/en/5.2/ref/models/querysets/#delete)

The first version deleted jobs in batches. Its automated Claude review pointed out that each batch still loaded full job rows, including their output and logs: "With multi-MB script output that's still hundreds of MB per batch." [Claude review](https://github.com/netbox-community/netbox/pull/22867#issuecomment-5185227774) The author changed the batch to load only primary keys, and checked the result: "Confirmed via captured SQL that the delete's initial `SELECT` pulls only `id`". [Author's reply](https://github.com/netbox-community/netbox/pull/22867#issuecomment-5196495299)

The next review noted that the batch size had been chosen for full rows and no longer fit: "A batch is now 100 integers." [Claude review](https://github.com/netbox-community/netbox/pull/22867#issuecomment-5196501163) Here the author measured instead of guessing: "I benchmarked a 200k-job deletion across 100/1000/5000; 1000 was fastest and peak memory was flat across all three." They added that "a single unbatched delete was the only one that spiked". [Author's reply](https://github.com/netbox-community/netbox/pull/22867#issuecomment-5241810396) The merged helper, from the pull request merged on 11 August 2026:

```
job_pks = job_queryset.order_by('pk').values_list('pk', flat=True)
# Re-slice the queryset each iteration: it re-queries after each batch delete, so the
# remaining set shrinks and the loop terminates (do not hoist this into a cursor).
while pks := list(job_pks[:JOB_DELETE_BATCH_SIZE]):
    # only('pk'): the batch still can't fast-delete, so each Job in the batch is instantiated;
    # loading just the PK avoids pulling the large data/log_entries payloads into memory.
    jobs.filter(pk__in=pks).only('pk').delete()
```

[Diff](https://github.com/netbox-community/netbox/pull/22867/files)

The first comment named a real cost and the second an assumption the fix had made obsolete. Captured SQL settled the first, and a measurement at 200,000 jobs settled the second.

## A long-lived collection that already had a bound

A Home Assistant pull request, opened in August 2026, made the Netatmo integration count API calls over a trailing hour with a deque of timestamps. Copilot said: "Pruning of `self._calls` only happens when `_calls_in_the_last_hour()` is invoked. Since timestamps are appended in `async_fetch_data()`, it's possible for the deque to grow without being pruned if fetches occur without passing through `async_update()` often enough (e.g., via force-update paths)." [Copilot review](https://github.com/home-assistant/core/pull/179190#pullrequestreview-4941527157)

The claim is true about the call sites and wrong about the bound. At the merge commit, `async_update()` is not only reached through fetches. `async_setup()` schedules it with `async_track_time_interval` every `SCAN_INTERVAL`, which is 60 seconds, and every run calls `_calls_in_the_last_hour()`, which drops timestamps older than an hour. [Merged coordinator](https://github.com/home-assistant/core/blob/6883a0c/homeassistant/components/netatmo/coordinator.py) Whatever path adds calls, while the integration is loaded the deque never holds more than the calls of about the last 61 minutes. A short simulation of that schedule confirms the bound:

```
from collections import deque

def peak(gap, hours=6):
    calls, peak_len = deque(), 0
    for t in range(0, hours * 3600):
        if t % gap == 0:
            calls.append(t)
        if t % 60 == 0:
            while calls and calls[0] < t - 3600:
                calls.popleft()
        peak_len = max(peak_len, len(calls))
    return peak_len

for gap in (9, 1):  # 400 calls an hour, and one call every second
    print(f"one call every {gap}s: peak {peak(gap)} entries")
```

It prints a peak of 407 entries at 400 calls an hour, the integration's development rate limit, and 3,660 at one call every second. The maintainer who later approved wrote: "Your replies to the Copilot threads hold up." [Maintainer's review](https://github.com/home-assistant/core/pull/179190#pullrequestreview-5562477650) The author's replies sit in resolved threads that the page does not show, so I have not read them. The pull request merged on 19 October 2026 without a change to the pruning.

## Decide by what removes entries

| What you find | Decision |
| --- | --- |
| Nothing ever removes entries, and keys come from requests, users or time | Change. Add `maxlen`, an LRU or a time-to-live, and test the size after many inserts |
| Removal runs on a timer or on every pass of a loop that always runs | Dismiss. Give the bound as rate times window |
| Removal runs only on a path that may stop running | Change, or add a test that shows the bound without that path |
| One operation loads a whole file, response or result set | Measure peak memory at the largest expected input, with [`tracemalloc`](https://docs.python.org/3/library/tracemalloc.html) or the process's resident size. Stream or batch it if that peak grows with data |

For a timed removal, the arithmetic is the reply: the window, the removal interval and the highest realistic rate. For a whole-input load, the measurement is the reply, as in the NetBox thread, and a [query with no limit](/learn/ai-review-unbounded-query-comment/) is often where that input comes from.

## One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. [Eligibility and fair-use terms](/free-code-review/).

[Start free with GitHub](https://proofline.sh/orgs/plan?offer=free_month)

## Related reading

-   ### [A query with no limit: measure what it reads and how fast that grows](/learn/ai-review-unbounded-query-comment/)
    
    Code review The Proofline team November 22, 2026 4 min read
    
-   ### [Trace an accepted code review finding to the lines it changed](/learn/reconstruct-accepted-review-finding/)
    
    Code review The Proofline team November 20, 2026 3 min read
