Process large datasets without running out of memory

Table of Contents

Lacking CPU, your program runs slower; lacking memory, your program crashes. But you can process larger-than-RAM datasets in Python, as you’ll learn in the following series of articles.

Code structure

  1. Copying data is wasteful, mutating data is dangerous
    Copying data wastes memory, and modifying or mutating data in-place can lead to bugs. A compromise between the two is “hidden mutability”.

  2. Clinging to memory: how Python function calls can increase your memory usage
    Python will automatically release memory for objects that aren’t being used. But sometimes function calls can unexpectedly keep objects in memory. Learn about Python memory management, how it interacts with function calls, and what you can do about it.

  3. Massive memory overhead: Numbers in Python and how NumPy helps
    Storing integers or floats in Python has a huge overhead in memory. Learn why, and how NumPy makes things better.

  4. Too many objects: Reducing memory overhead from Python instances
    Objects in Python have large memory overhead; create too many objects, and you’ll use far more memory than you expect. Learn why, and what do about it.

Data management techniques

  1. Estimating and modeling memory requirements for data processing
    How much memory does your process actually need? How much will it need for different inputs? Learn to how measure and model memory usage for data processing batch jobs.

  2. When your data doesn’t fit in memory: the basic techniques
    You can still process data that doesn’t fit in memory by using four basic techniques: spending money, compression, chunking, and indexing.


  1. Reducing Pandas memory usage #1: lossless compression
    How do you load a large CSV into Pandas without using as much memory? Learn the basic techniques: dropping columns, lower-range numeric dtypes, categoricals, and sparse columns.

  2. Reducing Pandas memory usage #2: lossy compression
    In this article you’ll learn techniques that lose some details in return for reducing memory usage.

  3. Reducing Pandas memory usage #3: Reading in chunks
    By loading and then processing a file into Pandas in chunks, you can load only part of the file into memory at any given time.

  4. Fast subsets of large datasets with Pandas and SQLite
    You have a large amount of data, and you want to load only part into memory as a Pandas dataframe. CSVs won’t cut it: you need a database, and the easiest way to do that is with SQLite.

  5. From chunking to parallelism: faster Pandas with Dask
    Processing your data in chunks lets you reduce memory usage, but it can also speed up your code. Because each chunk can be processed independently, you can process them in parallel, utilizing multiple CPUs. For Pandas (and NumPy), Dask is a great way to do this.


  1. Reducing NumPy memory usage with lossless compression
    By changing how you represent your NumPy arrays, you can significantly reduce memory usage: by choosing smaller dtypes, and using sparse arrays. You’ll also learn about cases where this won’t help.

  2. Loading NumPy arrays from disk: mmap() vs. Zarr/HDF5
    If your NumPy array doesn’t fit in memory, you can load it transparently from disk using either mmap() or the very similar Zarr and HDF5 file formats. Here’s what they do, and why you’d choose one over the other.

  3. The mmap() copy-on-write trick: reducing memory usage of array copies
    Usually, copying an array and modifying it doubles the memory usage. But by utilizing the operating system’s mmap() call, you only pay the cost for the parts of the copy that you changed.

Measuring memory usage

  1. Fil: a new Python memory profiler for data scientists and scientists
    Fil is a new memory profiler which shows you peak memory usage, and where that memory was allocated. It’s designed specifically for the needs of data scientists and scientists running data processing pipelines.

  2. Dying, fast and slow: out-of-memory crashes in Python
    There are many ways out-of-memory problems can manifest. Learn how to identify them, as a first step to fixing the problem.

  3. Debugging Python out-of-memory crashes with the Fil profiler
    Debugging out-of-memory crashes can be tricky. Learn how the Fil memory profiler can help you find where your memory use is happening.

  4. Debugging Python server memory leaks with the Fil profiler
    When your server is leaking memory, the Fil memory profiler can help you spot the buggy code.

How do you process large datasets with limited memory?

Get a free cheatsheet summarizing how to process large amounts of data with limited memory using Python, NumPy, and Pandas.

Plus, every week or so you’ll get new articles showing you how to process large data, and more generally improve you software engineering skills, from testing to packaging to performance: