Eren Ünalsan

BlogArticle

  • data
  • python
  • performance

Opening a 112 GB file without loading it into memory

5 min readEren Ünalsan

DataInspector started from a single question: how do I open a data file with hundreds of millions of records without loading it into memory, without writing a single byte to disk, and without making the user wait? In this post I walk through how the answer took shape.

The problem: the file does not fit in memory

The usual ways of opening a very large data file fall into three groups. General-purpose editors freeze while trying to read the file from start to finish. Most data tools load the file into memory. Some first convert the file to another format, which means a second copy on disk. For a 112 GB file, none of the three works.

I wanted something different: leave the file where it is and read only the part I want to look at. To get there I set three rules for myself, and I come back to them at the end.

The target files are clear: CSV, JSON Lines, XML and YAML files with hundreds of millions of records. At this size the “load the file, then look at it” approach hits a wall on both memory and time. The place to look is inside the file, a byte position; the real job is getting to that position quickly.

One record, one line

One observation shaped the whole design: if every record is a single line, finding record N means finding the Nth \n byte. There is no need to parse the file, build a tree or infer types; counting bytes is enough.

DataInspector opens CSV/TSV, JSON/JSONL/NDJSON, XML and YAML files, and all four run on the same indexing and search code. What makes that possible is that the same assumption holds for all of them.

This assumption has a cost: I reject files whose records do not fit on one line. I preferred refusing a file to silently showing a wrong result.

I kept the core small, and two interfaces use it: on the desktop, a Tkinter interface with an Office-style ribbon and Windows 11 Fluent light/dark themes; for the local network, a Django web interface. Both connect to the same indexing, search and reading code in the livedata/ package, so fixing a bug once fixes both interfaces.

If I stored an offset for every record, the index would grow large enough to compete with the file itself. Instead I store one offset every 1,000 records. For 205 million records that is about 205 thousand offsets; at 8 bytes each, 205,000 × 8 ≈ 1.6 MB. The memory cost of the index grows with the number of records rather than the file size, and it stays very small.

To go to any line, I find the nearest offset, seek to it in the file and scan forward at most 999 lines. The time I measured is 0.4 ms. For example, to reach record 150,000,000 I use offset 150,000 and scan no lines at all; for record 150,000,437 I go 437 lines forward from the same offset. Even in the worst case the scan is capped at 999 lines.

sparse-indexpseudocode
INTERVAL = 1000

# index: byte offset of every 1,000th line
offsets = [line_start for i, line_start in lines(file)
           if i % INTERVAL == 0]

def record(n):
    file.seek(offsets[n // INTERVAL])   # jump to the nearest offset
    for _ in range(n % INTERVAL):       # scan forward at most 999 lines
        file.readline()
    return file.readline()
FIG. 01 — Sparse index: one offset every 1,000 recordsSchematic

Text and regular-expression search does not need the index: I read the file as a stream and scan it at about 800 MB/s. Sorting also happens without writing to disk, within a page and over the first N results. All reading and searching runs on background threads; the UI thread only draws. That is how the first page of a 112 GB file arrives in under 0.5 seconds, while format detection takes about 10 ms.

I can export the records I select to Excel, Word and PDF using only the standard library, so I added no external dependency to the project. I can decode Base64 fields across columns. If the source file changes while it is open, the program warns me, because the old offsets may no longer point to the right place. The app is packaged with PyInstaller as a single .exe of about 12 MB.

Three rules and what comes next

The three rules I promised at the start are:

  • Nothing is written to disk. The status bar says “Written to disk: 0 bytes”.
  • The file is never loaded into memory. The memory ceiling is independent of file size; a 112 GB file uses the same memory as a 600 MB one.
  • The UI never freezes. All the heavy work runs in the background.

The three support each other: because I do not load the file, the memory ceiling is fixed; because I do not write to disk, I never touch the source file; and because the work runs in the background, the user does not pay for those two constraints by waiting.

These rules are less a wish list than a yardstick: if a feature breaks one of them, it is the design that needs rethinking, not the feature. The project passes 135 tests today.

As a next step, I am considering a separate path for files whose records span multiple lines; for now I reject them. I also want to measure the index interval (1,000) across different file types and re-evaluate the trade-off between memory and jump time.