1

I know this sounds like a simple question, but I have searched everywhere and they just say " store data to be used more quickly". I know ram gets used to load programs into, but is the whole program loaded into it or just part?

1
  • 1
    A point left out by other answers: processor cache and virtual memory (swapping to disk) are optional performance enhancers. These features require additional hardware that does not necessarily exist in all computers/processors. A modern PC will have these features, but not every processor will. Basic computer architecture does expect code and data to reside in (main) memory for the CPU.
    – sawdust
    Jan 6, 2013 at 23:34

3 Answers 3

4

There is an analogy I use when describing how the whole CPU/RAM/Hard Drive system interacts, I will do the whole analogy and it covers more than just ram, but I think it will help out.


Imagine your computer is like a kitchen, and running a program is like making a meal.

If you had two chefs (multi-core processor) you could make some meals faster, one person could cut the carrots while the other person peels the potatoes. However some points of the recipe there are things only one chef can do, for example mixing everything in the stand mixer. It does not matter if you have 1 chef or 4 chefs it still takes the same amount of time to mix the ingredients. This is why you just can't multi-thread everything, some steps take time and it can't be split across processors.

Now on to Memory:
The fastest kind of memory are CPU registers, they are like your cutting board. It is very small and can only hold what you are currently working on.

If something is not right in front of you, where do you go? You go to the pantry and get it, this is the CPU Cache. You have not left the kitchen but you did have to stop what you where doing for a few seconds and take some time to walk to the pantry and get you want.

But what do you do if the thing you need is not in the pantry? Well, you go to the corner store down the street, this is your RAM. Now it takes a few minutes to leave the kitchen (CPU) and drive to the corner store (RAM) and is 100's of times slower than walking to the pantry inside the kitchen, but your pantry is a limited size and you can't store everything in there. (Dual-Channel memory is like driving two cars to the store, you can take twice as much back home with you so you don't need to take as many trips)

But the corner store is just that, a small corner store. What do you do if you need something the corner store does not stock? Well you go drive in to town and go to the big Mega-Mart, this is your Hard Drive. Now the Mega-Mart is huge, you could get almost anything there, but there are drawbacks. The Mega-Mart is miles away compared to the corner store, and takes an hour to drive there and the employees there don't care and are very slow to check you out, it is 100's of times longer than going to the corner store (RAM) and 100,000's of times longer than just walking to the pantry (CPU Cache). And all this time your recipe (Program) is sitting there waiting for you to get the necessary ingredient to continue.

But what about those things that are super special order that even the Mega-Mart does not have it? Well you go and have it mail ordered and wait 1-3 days for it to show up, this is getting something from the internet. compared to waiting the few seconds to go to the pantry, that is an eternity.

5
  • very helpful!, got a question: but the stuff that is being stored in the corner store, is that the actual program or is it just bits and pieces of it?
    – agz
    Jan 6, 2013 at 21:09
  • Ustually when you first access it the whole program gets copied "to the corner store" and bits of the program gets moved "to the pantry" but if you don't access parts of the program for a long time it could get moved to a slower, but bigger, storage layer (the hard drive) to make room for something else that is needed right now, that is what a swap file is doing. Jan 6, 2013 at 21:30
  • All the layers do the exact same thing from the internet down to the cpu registers, its just a size/speed tradeoff. When you need to use something you copy it to a faster layer (you always leave the original on the harddrive/ram/cache), when you are not using it you either move a working copy down to a slower layer or apply the changes back to the original. Jan 6, 2013 at 21:34
  • I love your explanation!
    – ChrisN
    Jan 6, 2013 at 21:36
  • One of the best explanations I have seen, Thanks!
    – agz
    Jan 6, 2013 at 21:38
4

RAM is used to store everything that is being "currently used" by the computer. For example, the parts of Windows (Or Mac OS, or Linux) that give you the desktop, file viewing and so on. The browser you are using now is loaded into memory. Ideally when you quit a program it is completely removed from memory, but some times this does not occur and causes a memory leak.

To answer your specific question, small programs are generally loaded completely into memory, but in some cases, especially with large programs, only the main parts and some important subparts are loaded always, if you call on a little used part of the program, it might have to read that into memory separately.

Also, sometime programs will get "pre-loaded" by the operating system (Windows, Mac OS, Linux, etc.). This means that the program is sitting in RAM, waiting, so when you open it, it's right there and doesn't have to be read into memory. The OS does this if it thinks that you will soon want to open that program.

Confusing things even further, if you need more memory than you actually have, the operating system will take some of your hard drive and make it act like RAM. This is called "swapping" or "paging", and the part of the drive that is used for this is called "swap space" or a "swap file" or "paging file".

(Credit for the last two paragraphs goes to Darael for suggesting them)

Someone correct me if this isn't accurate and I'll revise this answer.

1
  • Swap (/pagefiles) complicates this, as things in RAM that haven't been used very recently may be moved into slower (but still temporary) storage, usually on the hard disk. It will then have to be read back in to RAM if it's to be used. Furthermore, the OS may cache frequently-used things in RAM even if they're not in use right now, to improve response times. Otherwise, I think that's pretty much accurate.
    – Darael
    Jan 6, 2013 at 19:13
0

Your computer's RAM is used to store data required for the operating system and programs to run. The CPU interacts directly with the RAM, so that it can be used directly as working memory, or "scratch space", for programs that are running. Memory not allocated to running programs is typically used by the operating system to act as a cache, storing frequently or recently accessed data to enable high-speed access to that data, since RAM is far faster than even SSDs, let alone hard drives. (Note that your CPU has its own internal cache memory and registers, which are extremely small but even faster than system RAM, but this is beyond the scope of this discussion.)

If the amount of RAM installed on your computer is insufficient for all of the software running on your system, the operating system will swap out data not currently in use from RAM onto the hard drive, keeping what's currently needed on in RAM. When the data swapped out from RAM is needed, the operating system moves it back from the hard drive and onto the RAM. This process is called paging and can be extremely slow as RAM is several orders of magnitude faster for random data accesses than hard drives, so if you're running out of physical memory, the system will be significantly slower than normal, with lots of disk activity.

This is why adding RAM to your computer can increase performance, especially if your system has 2 GB or less memory. Very large amounts of RAM (> 8 GB) generally has limited benefit, unless you are running several large programs or virtual machines at a time or have disk-heavy workloads where the extra memory can be used to speed up accesses to larger amounts of data at a time.

To learn more, see the Wikipedia article on the "memory hierarchy".

Not the answer you're looking for? Browse other questions tagged .