Quiz: how a computer works, and data size
Try each question before opening its hint or answer.
Q1. Match each part to its job: CPU, RAM, disk, GPU.
- keeps saved files when the computer is off; (b) does the calculations, with a few cores; (c) fast, temporary working space for running programs; (d) thousands of simple cores for images and deep learning.
Ask which part loses its contents when R closes.
CPU (b), RAM (c), disk (a), GPU (d). R objects live in RAM; CSV files, scripts and reports live on disk.
Q2. You make growth <- final_mm - initial_mm in the console, then restart R. Is growth still there? What should you do so the result can be repeated?
Restarting R ends the process that held its RAM.
No: the object was in RAM and is gone. Save the code in a script or R Markdown report (and save outputs with write_csv() if needed), so the result can be recreated from files on disk.
Q3. Estimate the RAM needed for a table with 500,000 rows and 20 numeric columns. How much free RAM would you plan for?
rows × columns × 8 bytes, then 2–3 times that for working space.
500,000 × 20 × 8 = 80,000,000 bytes = 80 MB. Plan for roughly 160–240 MB free.
Q4. In a CSV, the value 25.7, takes 5 bytes. In R, the number takes 8 bytes. A CSV of short rounded numbers is 4.8 MB. Is the loaded object larger or smaller than the file? What if the values had 15 or more digits each?
A CSV uses one byte per character; R uses 8 bytes per number whatever its digits.
Larger: our 4.8 MB file became an 8.0 MB object. With long values (about 19 characters each, including the comma) the CSV is larger than the object: one million such numbers made an 18.6 MB file but an 8.0 MB object.
Q5. The human genome has about 3.1 billion letters. How large is it as a plain text file? Roughly how large would it be in R stored as separate letters at 8 bytes each?
One letter in a text file is one byte. 1 GB = 1,000,000,000 bytes.
About 3.1 GB as text, and about 3.1 × 8 ≈ 25 GB as separate letters in R: too big for most laptops.
Q6. A camera image has 2048 × 2048 grayscale pixels. How large is it as an 8-bit file (1 byte per pixel), and as numbers in R?
First count the pixels: 2048 * 2048.
4,194,304 pixels: about 4.2 MB as a file and 33.6 MB in R (8 bytes per pixel).
Q7. A single-cell RNA-seq table has 30,000 genes × 100,000 cells. Will it fit in the RAM of a 16 GB laptop? What happens if you try anyway?
30000 * 100000 * 8 / 1e9 gives GB.
No: it needs about 24 GB before working space. The computer starts swapping to disk and everything crawls, or R stops with “cannot allocate vector” or crashes. Use a subset, a more compact format, or a bigger computer (Day 22).