Day 21 — CPU, RAM, disk, and estimating data size
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:
file.size() and object.size();Weeks 1–4 used small tables. From now on data size matters: tomorrow (Day 22) we move to a bigger, remote computer.
rsession or R: that is where your data are..Rmd, outputs) stay on disk.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.
RAM ≈ rows × numeric columns × 8 bytes. Plan for 2–3× that: R makes copies while it works.
> 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.> 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
19.3.19.3,), so ≈ 5 MB; the file is 4.79 MB.write_csv() prints nothing, so file.size() shows what it wrote.One character = one byte. Rounded values make a CSV smaller than its R object; long decimals make it larger.
S000001, …): CSV only 5.6 MB, in R 14.4 MB. Each different text value costs about 64 bytes.treated / control) stay cheap: R stores each different text once.> 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.data/pixels.csv: 8 × 8 = 64 pixels; 24 are above 100.Single-cell RNA-seq: 30,000 genes × 100,000 cells × 8 bytes = 24 GB, more than a 16 GB laptop, before any working space.
Error: cannot allocate vector of size …, or the session crashes and the Environment is lost.| 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.