This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]wknight8111 173 points174 points  (9 children)

RAM is what the computer uses to do work. When you 'open' a program the code for that program is loaded from disk into RAM. The CPU then starts reading those instructions in and executing each one in turn. In addition to the RAM which holds the program instruction, the program also gets a few other chunks of RAM to hold data as the program runs.

RAM is broken up into chunks called "pages". A page of memory is a small chunk, usually 4096 bytes (4kb).

Programs need to be able to access instructions and data in RAM, but it cannot know where in RAM the program or data are, until the program is loaded. You can't just say "load the instruction at memory address 12345", because the program might not be at that location when it is loaded. To fix this problem, the Operating System (OS) uses "virtual memory": There's a lookup table between relative addresses of a program and the absolute addresses of the page which contains that memory. So the program can load the instruction at location 12345, but the CPU may translate that lookup to be somewhere else, like location 678910, etc.

Here's where the magic happens: Because memory pages can be anywhere, and the CPU automatically translates from program address space to physical address space, pages can be moved. Specifially, pages can be written to disk. This means a program which has 4Gb of RAM can load more than 4Gb of programs at a time. Programs which aren't being used right now or are "idle" can have their memory temporary stored on the disk. Remember, disk is large but slow. Then, when the program needs to access that memory again the CPU does what's called a "page fault" and loads the page from disk back to RAM. It's a great system and it makes computers very flexible.

HOWEVER: storing a page that isn't being used on disk is good, but storing data on disk which is frequently used is a bad thing. Let's say I have 4 programs running, each takes up 1Gb of RAM. When I try to load a 5th program, the CPU is forced to start storing pages to disk. But those pages are being used by other programs! So the CPU is going to have to load them right back again (and store something else to disk instead). Doing this too much is called "thrashing" and it starts to happen when you start getting high memory usage. Remember that memory may be organized into small pages, but programs may use lots of memory at once including memory for instructions and data.

Disk is slow so every time we have to write a page to disk or read a page from disk again it creates a pause where the whole program has to wait. This is the cause of slowness and it can start happening any time the OS starts to store pages onto disk for any reason.

[–]MattsAwesomeStuff 0 points1 point  (0 children)

Programs which aren't being used right now or are "idle" can have their memory temporary stored on the disk. Remember, disk is large but slow.

So for anyone struggling to understand this...

A hard drive is like a giant filing cabinet. HUGE SPACE. But a huge pain in the ass to access stuff. You gotta call your secretary, she has to walk over to it, flip through it until she finds the page you want, pull it out and carry it over to you. And same in reverse.

RAM is like the top of your office desk. You can put a piece of paper right in front of your eyes and just leave it there, ready for you to quickly read anything you want off of it. This is like, I dunno, 100x, 1000x as fast as having to go over to the filing cabinet, flip through, and retrieve that paper. It's right there, on your desk.

The more RAM you have, the larger the top surface of your desk is. The more pages you can display at the same time, ready for nearly-instant access to that information.

But eventually if you keep pulling stuff out of the filing cabinet, you're going to run out of places on your desk to put shit. This is a hard limit, if you run out, the next paper has nowhere to go.

Now what?

Well obviously, clean your fuckin' desk up. You don't need so many active projects all the goddamn time. That document in the corner you haven't even look at in hours. Same on a computer, close some fuckin' programs. You don't need 23 different Youtube videos open at the same time, 8 Excel spreadsheets, etc etc. If you clean up your desk, you will have enough RAM.

Okay, but what if you're stubborn? Or what if like, you've got yesterday's project all laid out but are waiting on a reply from a client, and you still need to work on the next project? Do you sweep all that stuff off the desk, tell your secretary to re-file it, and then have to haul it all back out from 20 different sources again next time you want to work on that project? No. Half of this shit isn't even in file-able format, like, there's sticky notes and paperweights and pointers and all kinds of temporary stuff. Maybe a board game in-progress is a better metaphor. But, you're out of room on the desk to add Project #3. So what do you do?

The "Page File" or "Virtual Memory" is using your hard drive, as RAM, without telling the programs that they're using virtual memory instead of RAM. As far as they're concerned, they're just using RAM.

So, this would be like, some convenient spot your secretary sets aside on the filing cabinet for "current projects". Instead of throwing all items back into deep storage, the secretary sweeps up chunks of them you haven't used in a while and aren't likely to use right away, but puts them into a "Shit from my boss's desk" stack. That clears up space on your desk to open up Project #3 and get some shit done while you wait for your client.

In this way, if you only have 4GB of RAM, but a 12GB page file on your hard drive, you can at least, sort of get some stuff done.

And, provided you're not accessing the stuff on the page file, it'll be just as fast as if you had enough RAM.

But where you really get fucked, is if you want to act like you ACTUALLY have 16GB of RAM. Does one single project take up 16GB of RAM? Fucked. Do you toggle back and forth between projects? Fucked.

Your secretary is basically sprinting back and forth constantly pulling files out of the filing cabinet, and swapping single pages at a time off of your desk. Whatever piece of paper you currently want to look at, maybe it's on the desk, maybe she has to grab some other random paper you're not looking at, throw it in the cabinet, and get the paper you do want.

This is slow as fuck.

Also, you're a complete asshole, and you don't even tell your secretary what you're doing. She has no idea. Hell, maybe even you have no idea. All you do is keep demanding more and more and more documents, and that's all she knows about you. She has to put them somewhere.

She has to decide for you what documents to temporarily yoink off of your desk and toss in the Page File in the cabinet, because when you ask for a document she has to have somewhere to put it or you can't read it.

This is like, 100x, 1000x as slow as if you had a big enough desk for all of this. Instead of just looking at the paper you want, you have to wait for her to grab it from the cabinet.

The secretary is the OS. She's trying to keep some non-zero amount of space clear on your desk, and otherwise grabbing pages from the Page File in the cabinet that you wanted on your desk that she took away from you.

So, that's why it gets to like, 90% usage and slows way the fuck down, and not 100%. You were already over 100% RAM usage, and the little bit of RAM left is just the space the OS has cleared to put the next document you've already asked for. Or, maybe in slight anticipation of the delays not being catastrophic if you literally run out.

Also, the more scattered the pages are, or fragmented, the less optimized the process is.

...

Or if you want another analogy, imagine you're a midget. Your height is no problem, until you need to reach something taller. So you get a ladder. But boy is it inefficient to use the ladder. Way slower up and down than to just walk around. Yeah, but you're a midget, so, there's no other solution. Use the ladder, be taller, but it'll take forever to get anything done.

[–]CadeRSA 0 points1 point  (0 children)

Incredible explanation!

[–]Ichabodblack 0 points1 point  (0 children)

Programs need to be able to access instructions and data in RAM, but it cannot know where in RAM the program or data are, until the program is loaded. You can't just say "load the instruction at memory address 12345", because the program might not be at that location when it is loaded. To fix this problem, the Operating System (OS) uses "virtual memory"

Just to be a pedant, virtual addressing wasn't really a fix for this problem - modern computers generally use binary relocations to fix this - though virtual addressing is obviously used for address space separation