The failure
A report export reads rows from the database and writes a CSV file. Built the obvious way, it holds the whole result in memory, sometimes more than once: the database driver’s copy, the list of rows in the app, and the CSV text before it reaches the disk. Memory grows with the size of the export until the process hits its limit.
The simulator
Start at a yearly export with everything loaded at once: it runs out of memory. Try the other strategies, then grow the rows.
SimulatorExport a report to CSV
Out of memory while reading row 88,081 of 100,000
- Rows written as CSV
- 0
- File on disk
- 0 B
| Strategy | Peak memory | Result | Rows written as CSV | File on disk |
|---|---|---|---|---|
| Load everything (current) | over the limit (needs 2.3 GB) | Out of memory at row 88,081 (reading) | 0 | 0 B |
| Generator over a buffered query | 795 MB | Completed | 100,000 | 46 MB |
| Generator, CSV kept in memory | 78 MB | Completed | 100,000 | 46 MB |
| Stream each row to the file | 32 MB | Completed | 100,000 | 46 MB |
Model with illustrative numbers, not measurements from the real export.
What’s happening
Every mechanism here is general. None of it is a claim about one team’s code.
Load everything. The driver’s copy of the result, the list of row objects and the CSV string all grow with the row count. Each row is held three times.
A generator over a buffered query. The app sees one row at a time, so it looks like streaming. But a buffered query has already fetched every row into the driver before the app sees the first one. The generator only changes how the rows are handed over. It is a common trap.
A generator with the CSV kept in memory. Now the rows really do pass one at a time. The output piles up instead: every row is added to a string that is written to the file only at the end.
The fix. An unbuffered query, a generator, and each row written to the file as it comes. In this model, peak memory then depends on the number of columns, not on the number of rows. All three pieces were needed in the real export: take any one away and memory grows with the export again.
By the numbers above, a yearly export of about 100,000 rows with up to 30 to 40 columns is up to 3–4 million values.
Unbuffered reads have a cost. In many drivers the connection cannot run another query until the result has been read to the end. In this case it caused no problems.
Where I hit this in production
The job was a report export: read data from the database and write it to a CSV report on the server. It was started by a user or by a scheduled job.
How many rows depended on what the user selected. A yearly export was about 100,000 rows, with up to 30 to 40 columns.
What failed was memory. It grew as the exported date range grew. The limit was about 2 GB. Before the fix, even the yearly export exceeded it, and larger ones always did.
The first attempt read the buffered results in chunks, meant to avoid loading everything. The better solution was to stream the data directly to the file on disk.
The fix was unbuffered queries, a generator, and streaming the output. The team built a reusable way in the app to combine a generator with streaming. Unbuffered queries caused no side effects, but on their own they were not the best solution.
To stop it coming back, there is an architecture rule: new reports reuse the generator and streaming setup.