How a computer works, and how big is your data?

Day 21 — CPU, RAM, disk, and estimating data size

Data Analysis in Life Sciences

About this lecture

A colleague sends you a single-cell RNA-seq table: 30,000 genes measured in 100,000 cells. You type read_csv() and your laptop freezes, or R stops with “cannot allocate vector”. Before you load a dataset, how can you tell whether it will fit?

Today you will:

  • learn what the CPU, RAM, disk and GPU each do, and where R keeps its objects;
  • estimate the RAM a table needs: rows × columns × 8 bytes;
  • check estimates in R with file.size() and object.size();
  • size up DNA, images and single-cell data.

Weeks 1–4 used small tables. From now on data size matters: tomorrow (Day 22) we move to a bigger, remote computer.

The parts of a computer

  • Disk (SSD): slow but permanent. Saved CSVs, scripts and reports live here.
  • RAM: fast, temporary working space. Everything in R’s Environment pane lives here.
  • CPU: carries out the calculations, using data that are in RAM.
  • A file is read from disk into RAM before R can work with it.

Saved data are read from storage into working memory and processed by the CPU.

CPU cores, and what a GPU is

  • A CPU has a few cores (typically 4–16 in a laptop); each can work on a separate task. In the practical you look up how many yours has.
  • A GPU has thousands of simple cores doing the same small calculation at once.
  • GPUs are used for images, video and deep learning (for example protein-structure prediction).
  • This course runs on the CPU. You will hear about GPUs later; you do not need one here.

A CPU drawn with eight large cores beside a GPU drawn with hundreds of tiny cores.

Programs and processes

  • A program is an installed file on disk, such as RStudio.
  • A process is a running copy of it, using CPU and RAM now.
  • Task Manager (Windows) or Activity Monitor (Mac) lists processes and the RAM each uses.
  • Look for rsession or R: that is where your data are.

One application saved on storage can be launched as running processes using memory and CPU.

Closing R empties its RAM

  • Objects in the Environment pane are in RAM. Restart R and they are gone.
  • Saved files (data, scripts, .Rmd, outputs) stay on disk.
  • That is why we restart R and render from saved files: a result only in RAM cannot be checked or repeated.

Closing a running process clears its working session while the saved note persists on storage.

How many bytes?

One DNA letter in a text file takes 1 byte, one grey pixel takes 1 byte, and one number in R takes 8 bytes.

A bit is a single 0 or 1. A byte is 8 bits: 256 possible patterns, enough for one letter or a whole number from 0 to 255. 1 kB = 1,000 bytes; 1 MB = 1,000,000; 1 GB = 1,000,000,000.

Estimate a table before you load it

A grid of 10 columns and one million rows needs 1,000,000 times 10 times 8 bytes, which is 80 MB, plus working space.

RAM ≈ rows × numeric columns × 8 bytes. Plan for 2–3× that: R makes copies while it works.

Check the rule in R: one million numbers

> x <- rnorm(1000000)
> object.size(x)
8000048 bytes
> format(object.size(x), units = "MB", standard = "SI")
[1] "8 MB"
> seedlings <- read_csv("data/seedlings.csv",
+                       show_col_types = FALSE)
> file.size("data/seedlings.csv")
[1] 592
> object.size(seedlings)
6968 bytes
  • rnorm(1000000) makes one million random numbers. Prediction: 1,000,000 × 8 = 8,000,000 bytes. R says 8,000,048: the rule works, plus 48 bytes of bookkeeping.
  • object.size() = bytes in RAM; file.size() = bytes on disk.
  • The 24-plant table is 592 bytes on disk but 6,968 in R: for a tiny table R’s bookkeeping (names, types) outweighs the 96 numbers. The rule is for large tables.

A larger table, in RAM and on disk

> set.seed(21)
> n <- 100000
> big <- data.frame(matrix(round(rnorm(n * 10, mean = 20,
+                                      sd = 4), 1), ncol = 10))
> dim(big)
[1] 100000     10
> format(object.size(big), units = "MB", standard = "SI")
[1] "8 MB"
> write_csv(big, "output/big-measurements.csv")
> file.size("output/big-measurements.csv") / 1e6
[1] 4.793725
  • 100,000 rows × 10 columns of measurements such as 19.3.
  • In R: predicted 100,000 × 10 × 8 = 8 MB; R says 8 MB.
  • As CSV: about 5 characters per value (19.3,), so ≈ 5 MB; the file is 4.79 MB.
  • write_csv() prints nothing, so file.size() shows what it wrote.
  • Same data, two sizes: here the file is smaller than the object.

A CSV stores numbers as text

The text 25.7 with a comma is 5 bytes, the same number in R is 8 bytes, and a long decimal written as text is 19 bytes.

One character = one byte. Rounded values make a CSV smaller than its R object; long decimals make it larger.

File size is not RAM size

  • Rounded numbers: CSV 4.8 MB, in R 8.0 MB.
  • Add a unique ID per row (S000001, …): CSV only 5.6 MB, in R 14.4 MB. Each different text value costs about 64 bytes.
  • One million unrounded numbers: CSV 18.6 MB, in R 8.0 MB.
  • Repeated labels (treated / control) stay cheap: R stores each different text once.

Measured file sizes on disk and object sizes in R for three generated tables: 4.8 versus 8.0 MB, 5.6 versus 14.4 MB, and 18.6 versus 8.0 MB.

DNA is text: one byte per letter

> dna <- readLines("data/dna.txt")
> dna
[1] "ACGTACGTTTAGCCGATATACCGGTTAAACGTACGCGTAT"
> nchar(dna)
[1] 40
> file.size("data/dna.txt")
[1] 41
> bases <- strsplit(dna, split = "")[[1]]
> table(bases)
bases
 A  C  G  T
11  9  9 11
  • nchar() counts letters: 40. The file has 41 bytes: one per letter plus the line ending.
  • strsplit() cuts the text into single letters; table() counts each base.

The 40-letter course file takes 41 bytes; the human genome as text takes about 3.1 GB, and about 25 GB as separate letters in R.
  • Human genome: 3.1 billion letters ≈ 3.1 GB as text.
  • As separate letters in R, 8 bytes each: ≈ 25 GB.

An image is a grid of numbers

  • Each pixel is a brightness, 0 (black) to 255 (white): 1 byte in an 8-bit file.
  • data/pixels.csv: 8 × 8 = 64 pixels; 24 are above 100.
  • A camera image of 2048 × 2048 = 4,194,304 pixels:
    • file: 4.2 MB;
    • in R: × 8 bytes = 33.6 MB;
    • 100 time-lapse images in R: 3.4 GB.

The 8 by 8 table of brightness values in pixels.csv and the same values displayed as a grey image.

How big is biology data?

Sizes on a log scale from 592 bytes for seedlings.csv to 24 GB for a single-cell table, with a dashed line at 16 GB laptop RAM.

Single-cell RNA-seq: 30,000 genes × 100,000 cells × 8 bytes = 24 GB, more than a 16 GB laptop, before any working space.

When RAM runs out

  • Swapping: the computer moves parts of RAM to the much slower disk. Everything crawls, even the mouse.
  • R stops: Error: cannot allocate vector of size …, or the session crashes and the Environment is lost.
  • Estimate first. If 2–3× the estimate is near your RAM: use fewer rows or columns, a compact format, or a bigger computer.

A 24 GB object does not fit in 16 GB of RAM; the computer swaps to disk or R stops with an error.

Summary

Idea Key fact
CPU does the calculations; a few cores
RAM fast, temporary; R objects live here; emptied when R closes
Disk slow, permanent; files live here
GPU thousands of simple cores; images, deep learning; not needed here
RAM for a table rows × numeric columns × 8 bytes; plan 2–3×
File vs object CSV text: 1 byte per character; can be smaller or larger than in R
Scale genome ≈ 3.1 GB as text; 2048² image ≈ 33.6 MB in R; single-cell table ≈ 24 GB

Next (Day 22, remote computer): log in to a bigger computer, move a project there, render, and bring the result back.

Now: Open exercises · Quiz. Leave with data-size.R, output/big-measurements.csv and reports/data-size.txt.