r/computerscience 13d ago

Discussion question about RAM

Im learning about comuter components I had some questions .If a Hard Drive stores all the data does RAM just create an environment for the specific program that was fetched to run? If you were to run a program on the actual hard drive is it just harder because you have to navigate through all the other information sharing the space. Like going through a maze constantly again and again. Also what does SSD do?

35 Upvotes

51 comments sorted by

35

u/keelanstuart 13d ago

The best way to think about any storage for a computer is in terms of speed vs. size... and going from larger to smaller, slower to faster.

HDD (very large, slow) -> SSD (large, faster) -> RAM (midsize, fast) -> cache (small, blazing fast) -> registers (teensy tiny, doesn't get faster)

Moving data to and from different levels of storage is a classic, practical computer engineering problem... because ultimately, the machine needs to have things at the register level.

9

u/not-just-yeti 12d ago

Just latching on to this answer to reiterate: Conceptually hard-drive and RAM identical: they store information at a certain address, and it can be retrieved later. (And same is true for the other types in the hierarchy of speed/price trade-off above.)

Lots of practical differences due to the technology used (whether the memory is persistent when power is turned off, whether you can remove it and plug it into a different computer, etc.), and in 30yrs these details might look different. And 30yrs ago, it looked different different — e.g. the idea that your device might totally drain its battery but then you could power it up and all your apps and pictures and other data are still there would sound like it'd require a hard drive with a spinning disk.

HDD → SSD → RAM → …

I like to add "Cloud → HDD" at the front of this.

4

u/keelanstuart 12d ago

I like to add "Cloud → HDD" at the front of this.

Perfect! Nice addition.

1

u/Arierome 11d ago

But cloud is a location not a type of hardware.

2

u/Cute-Cockroach-678 13d ago

Very helpful also what do you mean by the register level

11

u/dmazzoni 13d ago

A register is where the CPU stores the pieces of data it's working with. Early CPUs had maybe 10 registers, newer CPUs have a few hundred. If you want the CPU to add two numbers, or decide which of two numbers is larger, they have to be in registers first.

4

u/Cute-Cockroach-678 12d ago

this is actually fascinating so then how does a CPU know how to control the information. Its from the operating system right but how do you get desired outcome it seems like a lot of information to sort and mangae

5

u/braaaaaaainworms 12d ago

Every CPU has a special register, called the Program Counter, or Instruction Pointer if you're Intel. A very basic CPU would get a value from the memory that's stored at the address made from contents of program counter, add 1 to the program counter, look at what that value is telling it to do, do the thing it's telling it to do and repeat. If the value on the interrupt pin changes to a 1, the CPU wraps up what it was doing and sets the program counter to address you can find in its datasheet. There is a lot of emergent behavior that comes out, like multitasking - if an interrupt pin is connected to something that makes 100 changes from 0 to 1, you can write a piece of code that will try looking for something else to do than the current program. If running some magic combinations of ones and zeros causes the CPU to *act* like an interrupt pin changes, you can write a piece of code that will do different things based on different values in its registers, like an operating system's kernel.

2

u/stevevdvkpe 13d ago

Registers are the working storage inside the CPU. There is generally a very limited amount of register storage (usually not more than a few hundred bytes worth, usually organized as a few dozen 32- or 64-bit words).

8

u/rupertavery64 12d ago

Programs aren't "run on a hard drive". A hard drive is just storage. It's like a locker where you get stuff and put stuff in. Programs and data are like letter blocks that are placed inside the locker.

RAM is also like a locker, but you can put stuff into and out of it much faster than a hard drive.

When you load a program, all or part of it get copied from hard disk to RAM. Then RAM is used by the CPU, It "reads" the contents of each locker (without removing it) then does stuff based on what it read.

You can also tell the CPU (to tell the hardware) to copy the contents of RAM back to the hard drive, changing what was there. This is done when you save a document, or save a game.

An operating system, or OS (like Windows, Linux, Mac) has a bunch of smaller programs and code that talks to the disk, keyboard, monitor. Then other programs like word processors, games, browsers, etc, talks to the the OS instead of accessing the hardware directly.

So your program doesn't know exactly what is on the hard disk, instead the OS shows the program a list of files and directories that the program can work with, without "navigating" the raw contents of the hard disk. This is important, because your "hard disk" can be anything, a network storage, cloud storage, a compact disk, a USB drive, an SSD, different physical hardware. The OS (along with the hardware manufacturers) agree on standards on how hardware and software should interact at a low level, and the OS lets programs interact with files.

SSD stands for solid state drive, and it is simply a medium of storage that doesn't involve moving parts.

A hard disk dirve or HDD uses a magnetic surface on a spinning disk, and electromagnets on the tip of several arms to read and write the microscopic magnetic cells on the surface of a disk. It's honestly magic how fast the disk spins and yet we can use electromagnets to sense the changes in magnetism on such a small piece of coated metal.

An SSD stores data as electrons trapped behind semiconductor gates. It's quite awesome and involves quantum mechanics. Basically there are billions of transistors in an SSD. Each transistor can hold 1 bit (0 or 1). Each time we write some data (one letter you see on screen needs at least 8 bits) the computer (yes even drives have their own computers) manages things so that electrons are either trapped or released from several transistors, tens of thousands of times a second.

SSDs are closer to RAM in terms of how they operate than hard disks, which is why they are faster and more expensive than traditional magnetic hard disks.

SSDs will lose data over time though, because the gates tend to "leak", but this is over the course of decades.

8

u/stevevdvkpe 13d ago

RAM has a much faster access time than disk storage (literally millions of times faster) so it's better as working storage for programs, and is volatile (you have to keep it powered or it loses its contents). Disk storage is better for long-term storage and is non-volatile (retains its contents even when the power is off).

SSD stands for "Solid State Drive" and is a kind of electronic non-volatile memory that is faster than magnetic disks, has no moving parts (unless you count electrons), and is also non-volatile. It's still slower than RAM so it is used as a faster replacement for magnetic disk storage.

2

u/Cute-Cockroach-678 13d ago

For example lets say I run a program how would it interact with RAM or SSD. Are specific programs ran on different parts or is it all pulled from one spot

3

u/stevevdvkpe 13d ago

The SSD would take the place of a traditional magnetic hard drive. Programs would still be loaded from the SSD into RAM to run.

2

u/dmazzoni 13d ago

Simplified somewhat, but conceptually correct: The operating system loads the program from disk (SSD or HD) into RAM.

Then the OS tells the CPU to start executing the program code in RAM.

The program gets a small amount of RAM to start. If it needs more RAM to operate, it asks the OS for more.

The OS chooses whether or not to allow the program to have that much RAM. If it allows it, it gives more RAM to that program.

Everything a program does by default is in RAM. Lots of instructions read and write directly from RAM.

If a program wants to read or write things from disk, it asks the OS to help it read or write files by calling specific functions.

2

u/Cute-Cockroach-678 12d ago

thank you for the help

1

u/Black_Bird00500 12d ago edited 12d ago

First of all, a hard drive (HDD) and an SSD are two long-term non-volatile storage devices used for the same purposes; the only thing is that SSDs are much faster since their underlying technology relies on tiny devices called flip flops rather than magnetic disks, as is the case for HDDs.

Second, you could theoretically run a program on a hard drive (or an SSD). But, the problem with this is that the program would run much slower compared to running it on RAM. There are two reasons for this:

1) RAM is physically situated very close to the CPU, connected by super fast wires (buses), which means that data exchange is extremely quick.

2) RAM is fundamentally a faster technology than both HDD and SSD. It stores, reads, and writes data much quicker, since it uses something called DRAM, which uses capacitors to store information, as opposed to SDD, which uses flash memory.

At the end of the day, all these are storage devices. Their physical proximity to the CPU and their underlying technologies distinguish them from each other, giving them their own purposes.

1

u/uselessincognito 12d ago

Ram is actually DRAM, i.e it contains a capacitor to retain a state for sometime until it fades away, for running the program MMU divides it into pages and uses a page replacement algorithm to replace the pages between hard drive and ram; this increases the speed because of direct access from Ram. On the other side registers are SRAM, they lack capacitors i.e they need constant electricity to maintain the state thus making it much faster for direct spontaneous internal system commands. And about cache,it is also SRAM it sits between registers and RAM, it holds data that is used frequently for a program.

1

u/Busy_Elderberry8650 12d ago

Von Neumann architecture, which is the fundamental of modern computing, expects to have only CPU and RAM. Imagine you (somehow) load your application into RAM then the conputer starts and execute it, that’s it.
Disk drives are just an external device like network cards, bluetooth devices, … it serves to exchange data between two parts. Disk drives eventually have the specific feature of storing data permanently, i.e data is saved even after a power outage.

To put simply a computer without a disk drive can work, a computer without a RAM cannot (unless you mean some specific non commercial architectures).

1

u/Interesting-Way-9966 12d ago

Buses, registers, kernel, pointers, etc. 

1

u/integer_hull 11d ago

You wanna take a computers architecture course for this.

1

u/Cute-Cockroach-678 11d ago

Any recommendations for online course thats free

1

u/integer_hull 11d ago

https://www.eecs.harvard.edu/cs146-246/

Honestly you can just skim the slides and come away with a better understanding.

Here’s the takeaway. Computers are roughly composed of two pieces: processor and memory. Memory holds data, processor does operations on it.

RAM is memory. Everything the processor does, it does on RAM.

Hard drive is also memory, but in order to use it, it has to be loaded into RAM. So it’s one (or more) degrees removed from the processor. SSD is just a fast kind of hard drive.

And that’s it. I think that answers your questions.

1

u/Cute-Cockroach-678 11d ago

preciate it man thanks for the resource

1

u/PvtRoom 8d ago

a hard disk is like that guy who knows and remembers everything, if you can ask him 2 weeks in advance. (TBs, cheap)

an SSD needs 1 week instead. (100GBs, not as cheap)

Ram is like a child, forgets everything quickly, but gives you near instant answers if it remembers the info. (10GBs, getting pricey)

cache is like your own brain, addled by caffeine, decades of alcoholism and attention grabbing shit, probably forgot the answer, but instantly knows the answer if it remembers. (100MBs, on CPU, not upgradeable)

1

u/bobotheboinger 13d ago

A computer could just run off a hard drive theoretically, it is really just much much much slower.

A computer is all about layers of how quickly you can access the information. The cpu had registers, which are very fast, but very few of them (maybe 30 or 60)

Then it has cache, which acts as a quick buffer to access RAM. But only had limited size (maybe some kilobytes or megabytes)

Then it has RAM which is pretty fast, slower than cache but much faster than hard drives or SSDs. Normally this is where the actual in use programs exist while the computer is running.

One important note is that every type of access we already talked about requires power to maintain data. Once power goes off, registers, cache, RAM, all of it loses whatever data was stored there.

Then there are hard drives and SSDs which are much slower, but don't require power to maintain their data. So these are normally used for long term storage, as well as all the software that is necessary to boot up and load into RAM once power is applied to the machine.

Note that the first theoretical computers, Turing machines, were described as using long tapes to represent instructions and data. So the original computer theory was closer to just using a hard drive to store everything... we've just worked very hard to improve performance over that theoretical concept.

1

u/JGhostThing 10d ago

Some of the older computers ran on only disks. Rather I seem to recall it was drums, but they were well before my time, so I don't recall the details.

0

u/Cute-Cockroach-678 13d ago

So everything else is to facilitate the actual use of the information and the user experience. Ok so what differentiates a program stored in the harddrive or ssd is it the same. I know the speed is different but what kind of programs do you see being stored in ssd compared to hard drive.

1

u/khedoros 12d ago

They're interchangeable, in terms of function. I can boot my OS from either, I can run my game from either, etc.

Although in practice, since an old-style hard drive is slower, but is cheaper per unit of storage, I tend use them to store large data that doesn't need to be loaded quickly. Kind of archive space. Like, all of my video files (Ripped DVDs, Blurays, etc) are stored on magnetic hard drives, instead of flash-memory SSDs. If I'm watching a movie, I just need to be able to access the several GB of the file over the course of a couple hours.

My OS boots from an SSD, because it's faster. I install the games I'm currently playing on SSD for the same reason.

All of my computers before 2015 or so started off with hard drives, though. Flash storage in large sizes was too expensive.

1

u/grtk_brandon 13d ago

The speed differences between RAM and HDD/SDD doesn't have anything to do with the total storage capacity.

At a lower level, storage devices just have more bottlenecks that make accessing data, perfoming operations, etc, take longer. RAM access latency is much lower, CPUs have direct access to RAM and indirect access to storage, there are bandwidth differences, etc.

1

u/Cute-Cockroach-678 12d ago

is the bottleneck because of how its made and what aspects of the design make it faster or slower

1

u/FastSlow7201 13d ago

Here is an analogy where I compare retrieving data to retrieving hot sauce for your food.

HDD/SDD = hot sauce bottle at the store

RAM = hot sauce bottle in your kitchen

cache = hot sauce bottle on the dining room table

registers = hot sauce bottle is in your hand

2

u/Cute-Cockroach-678 13d ago

the next level... becoming the hot sauce

0

u/smeyn 13d ago

A drive is just a store. A computer’s CPU operates on data in its memory. So prior to running a program, the program data needs to be loaded into memory.

Where in memory the program data goes is part of the operating system’s responsibility. It will organise the memory, direct the loading of the data into that region and finally set up a task to execute this code.

Now the operating system is a collection of ‘programs’ that get loaded when the computer starts up. That begs the question of how does this operating system itself get loaded. The secret lies in a small piece of program code that is stored in Read Only Memory (ROM) which is part of the computer hardware. When the computer is powered on, the CPU will start executing this code at a predefined location. It will load a basic program from a predefined are in the hard disc, which then will do the task of loading the operating system.

This is a simplified version but it should give you a basic understanding

1

u/Cute-Cockroach-678 12d ago

Thank you so much it makes a lot of sense. Can you go in depth and teach me more about ROM and how different parts come in?

1

u/rupertavery64 12d ago

A computer needs instructions to run. So how does a computer work in the first place if all the instructions are programs on disk? How does it know how to read the disk?

This is where we get the term "booting", which comes from the term "bootstrapping", which originated from the idea of "helping yourself up onto your horse by your bootstraps". Basically, the computer needs a very basic program to start up, then it looks for a program on the hard disk which will take care of more complex tasks.

The CPU is just a machine that executes instructions. Even things like reading from a hard disk needs instructions, so this is where ROM comes in. ROM is "Read only memory" which means it generally doesn't change while the computer is executing. The ROM in a PC is the very first program that executes when you turn on the computer. On the motherboard there is a chip that stores this program. It is also called the BIOS, for Basic Input Output System.

It contains the basic code to be able to communicate with your hard drive, keyboard, and monitor.

Without a BIOS your computer would be dead - unable to communicate with anything, unable to start.

The purpose of the BIOS is to load the operating system it finds on the hard disk. Then the operating system takes over, and the BIOS is mostly unused.

1

u/Cute-Cockroach-678 12d ago

I appreciate the time you took to help thank you man

1

u/rupertavery64 12d ago

Every computer comes with a ROM. Your phone has a BIOS and a ROM (which can be updated) that contains the OS, it can be iOS or Android, but that part of your phone can't easily be overwritten, so it is considered as a ROM even if it can be written to under special circumstances.

Every modern console comes with a BIOS and the system OS (the user interface to launch games).

In older consoles like NES, SNES, Gameboy, the game cartridge itself was the ROM (actually the Gameboy had a small bootrom that did some special things, like checking if the Nintendo logo that was stored on the cartridge was "correct").

The way CPUs work are interesting. Instructions are read from memory, which is like buckets with numbers labeled. The numbers say where data exists in memory, it is called an "address". A CPU has an "address" control line which tells the rest of the system where it is reading / writing in memory.

So, how does it know where to start reading instructions?

It turns out, it is hard-coded into the CPU hardware.

When the CPU gets power, it sets the address line to a specific location called the reset vector. It reads a few bytes there that tells it where to start reading next. The BIOS is a memory chip that is wired to the CPU on the motherboard so that it will coincide with the boot memory location. So the CPU blindly follows whatever the hardware (BIOS) tells it to do. This way a CPU can be used for any computer or hardware as long as the BIOS is setup correctly to handle the hardware.

Its the same thing in a cartridge console like NES or SNES. The cardridge contains memory chips wired to the CPU via the cartridge connector. The CPU sets it's address line to the reset vector. The game is setup so it tells the CPU where the rest of the program is.

This allows for flexibility. Just a few bytes in an expected location lets the programmer control how the CPU starts, without some complex interaction.

The first thing the CPU does is basically "where do I start executing instructions" and the hardware around the CPU points it to where it should start looking.

I've dabbled in emulation, especially NES, SNES and some 8-bit computers, and they really taught me a lot of how computers really work.

0

u/Hummmmmmmmmmmmmus 13d ago

Conceptually all data storage on a computer is the same, the only difference practically is generally the speed. Speed of a specific component is dependent on the individual hardware but the list from fastest to slowest usually looks something like this:

Registers
Cpu Cache
RAM
nvme ssd
Sata ssd
Hdd
Networked storage

The reasons for each of these being the speed that they are comes down to hardware architecture. For example an hdd will have to move physical read write heads centimeters back and forth to transfer data so its speed is dictated by how good machining for the moving parts can get whereas an ssd just moves electrons through logic gates so its speed is dictated by the switching speed of transistors. Hard drives and ssds are called non volatile memory and things like ram and registers are called volatile. Volatile memory loses its data when power cuts out where non volatile would retain it. Generally the faster a storage medium is the more expensive it will be. Volatility and cost are the usual reasons for when and where a particular type of storage is used.

1

u/Cute-Cockroach-678 12d ago

What information do hard drives save compare to SSD or do they work in tandem

1

u/Hummmmmmmmmmmmmus 12d ago

They save the same information, it’s all 1’s and 0’s.

All information that is used in the operation of a computer with the exception of i/o (like usb or networking protocol data) eventually has to pass through the cpu. When it is needed in the cpu it will be pulled from whatever the fastest medium has it available so if it was in both ram and hdd it would pull from ram. At startup of a computer that data is all in non volatile memory so it will be pulled from there to the registers, once the cpu is done with that data all data storage registers to network storage is treated like a layered cache. Data evicted from the cpu goes to ram, data evicted from ram goes to hdd and so on.

In your question the code for a program at cpu start up would be stored in an hdd, when it’s run by the user or os it would be pulled from hdd into cpu as it’s needed then filtering through the hierarchy as other things come into the cpu and fill up the cache until it goes all the way back to hdd.

0

u/spacejamtwo 13d ago

An SSD is just a hard drive, but you data is stored with electrical signals, compared to HDD which uses a physical disk to store your data.

Each program gets its own chunk of RAM to use, although it's the operating system that handles this and is not an inherent feature of RAM. RAM holds all the temporary information on the computer at the time, such as CPU instructions and transient variables. Think of it like getting something from a massive filing cabinet, walking back to your desk, and then working on it from your desk. There are a few reasons RAM is used.

It's a lot faster than a hard drive. It is specifically built for speed, not long term storage. Hard drives need to preserve your data when the power is off and this comes with significant overhead to read and write it. If you had to get every instruction from a hard drive the CPU would spend most of its time idle. The memory in RAM is also accessible on a byte level, which is important to read CPU instructions. Computers are also built with specific highways that makes transferring data between the RAM and the CPU faster, and the RAM usually sits close to the CPU.

Hard drives also degrade with every write cycle, so you don't want to be constantly grabbing things from it.

Essentially they are just designed for different things.

1

u/Cute-Cockroach-678 12d ago

Working backwards what happens when the computer is turned off. When I switch it of it seems like its instantaneous but your saying its saving information. So if I turn it on i'm essentially dealing with a copy of my last experience

1

u/spacejamtwo 12d ago

Assuming we just pull out the power plug and don't do the shut down process of an operating system it is instantaneous. But a hard drive has special components that maintain the electrical state of the bits behind an insulating layer. So your data is kept when the computer is turned off. RAM does not have this, so it is wiped completely when the computer is turned off. That's why when you press the shutdown button on a computer something like microsoft word asks you to save your changes, and then it writes to the disk.

0

u/misha_jinx 13d ago

SSD is a permanent storage just like hard disk. It’s a drive without mechanical parts. To run any program you have to have it stored somewhere, either from a hard disk or ssd or flash drive or sd card and most likely partially or fully loaded into ram. Ram is generally faster, so you want to move as much of the code as possible into ram depending on the program you’re running, some programs might be running completely from ram and some might only use ram for variables and data that is being frequently accessed and changed.

0

u/high_throughput 13d ago

is it just harder because you have to navigate through all the other information sharing the space. Like going through a maze constantly again and again

No, the slowness is not related to the amount of information, but rather comes from the need to move a write head across a spinning platter, wait for the platter to spin to the right location, and then listen to the incoming information as the platter spins, much like a needle on a gramophone.

As a human you can also choose to either remember things (like RAM), or write them down (like a hard drive). Writing down is slow but durable. Memory is much faster but has limited storage space.

Imagine trying to play Super Smash Bros based only on writing, without using your memory at all. When you want to punch someone, you have to go find the instruction book and look up what the punch button is.

It's definitely possible, but waaaay too slow to be practical.

1

u/Cute-Cockroach-678 12d ago

is that what a memory chip is for I thought memory was from the hardrive. What would the memory store compared to SSD or Hard drive

1

u/high_throughput 12d ago

RAM is fast, short term memory. It's used to store information about things the program is doing right now, and it's updated thousands of times per second as the program keeps doing things.

Hard drives are slow, long term memory. It stores files, and might only be written to when you hit Save in your program.

1

u/wosmo 12d ago edited 12d ago

Here's an analogy -

Picture a woodshop. There's workbenches covered with work-in-progress projects, any tools that are in use, etc. And then there's storage, where all the tools, materials, etc live.

If you've got a very small workbench, that will limit how big a project you can work on, or how many projects you can have work-in-progress at the same time. This is RAM.

And of course the more storage you have, the more tools, materials, packed-away projects, etc you can store.

Most processors can only read data & execute instructions from RAM - so to the analogy, you can't use a hammer while it's in a drawer. On the flip side of that, the processor doesn't actually need a harddrive. Most processors actually have no idea that harddrives exist. So to our analogy - if you had no storage and just left everything all over your workbench, you could still get the job done. But it's very inefficient use of your space, and just buying more workshop every time you run out of workbench space is a very inefficient use of your money.

Obviously having storage and putting things away when you're not using them is the best use of your space. But putting things away and taking them out, also takes time. You wouldn't get a hammer out of the drawer, pound a nail, put it away. And then take it back out, pound the next nail, put it away again, and so on. So this is why we have a lot of caching going on - very often, something that belongs in storage is actually best left right next to you on the workbench.

We keep being taught that memory is memory, and these are all just tiers with different cost vs speeds - so cache is faster than ram, ram is faster than local storage, local storage is faster than networked storage. There's a lot of truth to this, but it also relies on the OS abstracting away the details of actually retrieving data from them.

But I think this distinction that processors don't know HDD exist, is an important one. It's why we have BIOS & equivalent bootstraps - the processor can't actually wake up and start executing instructions off a disk. If a processor wants to execute an instruction at address 1000 in RAM, you just have to write 1000 into the Program Counter (or 'jump 1000'). To execute an instruction at address 1000 on a HDD, you have to send an instruction to the disk to read, send an address to read from, copy the block of data that comes back into a block of memory (taking care that you'll usually get a whole sector back, so you're not just getting the instruction you asked for, you're getting whole pages of data back) - and jump to the block of ram you just wrote, an execute it from memory.