Writing · Research Computing

Why your analysis runs out of memory, and what to try first

← All writing

The script ran fine on the sample file. Then you pointed it at the full data and it died with a message like "MemoryError," "cannot allocate vector of size," or just "Killed." It feels like the computer is too small. Often the code is asking for far more than it needs.

What's going on

Working memory (RAM) is the desk where your program spreads out its papers. Disk storage is the filing cabinet. A file can sit happily in the cabinet and still be too big to spread out on the desk, especially once your code makes a few copies of it. That's the whole story behind most memory errors.

Try these first, in this order

Load less. Read only the columns you'll use, and drop the rest at the start. Both pandas and R's data tools let you choose columns when you read a file. It's the cheapest fix and often the biggest.

Read in pieces. Process a large file a chunk at a time, keep the summary from each chunk, and combine them at the end. This works well for counts, sums, and filtering rows.

Use smaller data types. A column of small whole numbers or a short list of repeated labels doesn't need the default storage size. Converting text labels to a category type can shrink a column a lot.

Stop making copies. Every time you save an intermediate result under a new name, you may be holding two versions at once. Remove objects you're done with, and chain steps so the big table isn't duplicated.

Change the file format. Plain CSV is bulky and slow to read. Formats like Parquet store columns compactly and let you read just what you need. The research data file formats article covers when that switch is worth it.

Sample while you build. Develop on a slice of the data, then run the full thing once the code works. You'll find mistakes faster and waste fewer long runs.

When you do need more memory

If the data truly doesn't fit even after those steps, ask for a bigger machine. On a shared system, request more memory in your job script and say how much you measured, rather than a round number you made up. The first-job guide shows where that request goes, and the where to run your research article helps you decide where a larger job should live. If your language choice is part of the problem, Python or R has a short comparison.

One habit pays off either way: write down how much memory a run used, in your README or notes, so the next person (or you, next semester) knows what to request. For the wider picture, the research computing guide collects these articles in one place.

Common questions

What does an out-of-memory error actually mean? Your program asked the computer for more working space (RAM) than was free, so the operating system stopped it. RAM is different from disk storage. You can have a mostly empty hard drive and still run out of memory.

How much RAM does my data need? A rough rule is that a table takes two to five times its file size once it's loaded and you start transforming it, because copies get made along the way. A 2 GB file can need 6 to 10 GB of memory in practice. Check the real number with a small test run instead of guessing.

Should I just ask for a bigger machine? Sometimes, and on a cluster that's a normal request. But a few minutes spent loading only the columns you need or reading the file in pieces often solves it for free, and it makes your code friendlier to run anywhere.

Why does it work on my laptop but fail on the cluster? Clusters often give each job a fixed memory limit that you set in the job script. If you don't ask for enough, the scheduler ends the job even though the node has plenty. Look at the job's error file and the memory you requested.


Samah Alshrief, Ph.D.

AI data scientist and research-computing specialist. I help researchers and universities with research data management, data services, and private AI. Get in touch →