ponedjeljak, 27. veljače 2012.

OS Memory management

After a while, I'm back! I'm crazy and I'm gonna swear a lot. Lets start politely not to scare you away. IDK who's reading this crap anyway.


Anyway, it goes like this


Memory - a capability of a human to remember stuff. There are many kinds of memory - long term, shot term? Perhaps many other, but I cannot remember correctly. Because I haven't been there for a while. Learning about human memory.


However, I've been learning about (*DRUMROLL*) computer memory. Yes, all the volatile and non-volatile, capacitive and magnetic and flash and flip-flops and whatever. YES, these are just memory types and methods of storing data.


A volatile memory is the one that gets erased whenever the power runs out. So what does the memory store? Since we're dealing with computers, it's bits. Bits, or actually two-state "numbers" or "elements" or whatever that can only have two states. Ok, that doesn't mean "ON" and "OFF", although one can understand with less effort if it visualizes it like being ON and OFF.


But a computer cannot understand ON and OFF. It understands voltage. So let's say 0.8 V is "OFF" and 5 V is "ON". (It can go either way, but the inverted version is called reverse-logic). NEXT - Why are we doing this?


Because the computer uses something called the Boolean Algebra. It's the mathematical formulation of truth values of statements, set theory and few other stuff using only two values - TRUE and FALSE. If 0.8 V is OFF, that 0.8 V is FALSE - and 5 V the truth. To easily create mathematical formulations and statements, finally, we use binary numbers. That means we only have two number - a 'zero' and 'one'. 0 and 1.


The story is: Computers use mathematical formulations of Boolean algebra encoded as voltage to calculate results of mathematical operations with binary numbers.


To make these calculations computers need to store binary numbers encoded as voltages, and put them through something called "logical gates", aligned and ordered specifically to give results of mathematical operations - for example, adding. So, how to store numbers? There is an entire field of study that handles that - but that's currently useless stuff to bore you about.   
WOW,  Is it really that simple?
But damn, I'm going too deep into developer's point of view, and this is a practical blog so I must quit doing this shit :). So anyway, let's talk about something that might interest you. How does having a lots of memory helps YOU in YOUR everyday work? Let's start with an answer.


YOU: "So, I have 500 GB of memory. PC, Y U NO WORK FAST?"


What? What are you saying? Let me explain:


First, let's make sure that both of us know the different kinds of memory. That means you must learn what I tell you, and I'll tell you what someone else told me :). If you make the same mistake after reading this you weren't paying attention so GET A GRIP. So,


Let me ask you a question. ME: "How much memory your computer has?"


You answering my question with the answer in the previous title is VERY WRONG. Since I'm quite smart, and believe you don't have a clue about computers, I understand what you are telling me, but this is not what I asked.


You told me about your hard drive space. Hard drives are a permanent storage class memory that every computer has. It could be a hard disk, a solid state drive or flash memory or whatever your computer has. Specific thing about this kind of memory is that is doesn't lose data when the power runs out. So when you install a program, download stuff from the Internet, copy pictures from your camera, it all stays in the same place you put it after you shut your device down. And, your camera has some permanent storage too. It's the memory card (a kind of flash memory, for those who are asking) pictures go to when they are taken. It would be very stupid if they go away when you push the OFF switch, right?


I asked you about the kind of memory computer currently uses to do work. Since permanent memory access is very slow (in context of today's computers), computer needs some active working space to load data from permanent memory, keep it here for quick access, to and to temporary store the results of data processing. Most stuff from memory is only kept for quick access (or is a copy of data from permanent storage for quick access) so when the computer isn't running, the contents of that memory don't need to be kept. So working memory is actually a scratchpad - you keep your active work in it, and throw it away when you're done with it. Let's compare - if you have a bigger desk, you can keep a lot of papers and documents for quick access, instead going for the file cabinet on the other side of the room to search for the particular thing you need. So you only have to go to the file cabinet once or twice and after you're done, walk once to the file cabinet to store done work. If you don't have a large desk, it's gonna get cluttered and productivity (in computer terms, processing performance) will drop. Most people don't care about performance, consider it bullshit, and me an idiot for particular optimization details I do with my stuff, so let me tell you this - performance is king. If you don't like it, get a hike.


So, let me correctly answer my question. I have 8 GB of memory on my MBP. The legacy name for that is RAM (Random Access Memory). It developed as a requirement because the applications were getting bigger, and CPU registers and caches aren't enough for fast and seamless performance. And now, the more memory you have, the computer's multitasking capabilities are higher and performance intensive applications are faster. What? What are registers and caches? Y U NO KISS?
Take a look, see what I mean?
What next? Let's see some software side of the story. Hardware is here, and it defines the speeds, throughputs, latencies, memory sizes, modules and other crazy shit. But how to harness all that POWAH and put it to good use? How will a computer manage all that hard grabbing memory shit? Smoke that shit?
Seriously crazy shit.


Could be scary shit


The culprits lie in very advanced algorithms and systems that manage, optimize, assign, protect, cache, manipulate, release, limit, transfer, copy, read, write, defragment and warn the user about the memory's current state. It's all contained in the main program. The mother of all running programs. The father, grand controller and king of your pc. All your bits are belong to him. 


The operating system. Windows. Unix. And other insignificant operating systems in miniscule amounts.


So, the OS actually manages memory through something called memory virtualization. It takes the physical memory (RAM) and pagefile or swap space (a very large file that's imitating memory storage) on the permanent storage and combines it together, abstracting the entire memory for things that need memory, and those things are called applications. An operating system takes a block available storage from the virtual memory and assigns it to a running application, but it has super-duper fucking advanced algorithm shit that assigns it to the best location it can currently get. So depends on the workload, memory capacity and other elusive factors, it stores it to either physical memory or page file. Completely transparent to the applications. If you ask me, that's fucking cool.


Generally performance goes like this: If you have more physical memory, you can load more applications and multitask efficiently. But computers work with limited values, and memory slots are also limited. So what's the cheapest way of getting my PC work fast? If it's a PC, buy and install Windows 7 with 4 GB of memory. If it's a Mac, buy Windows 7 and Bootcamp your ass to it. Windows 7 is the swiss knife of computers - it has the best applications, it has fucking cool memory management systems and it's too fucking simple to use that it's not even a challenge, it's boring me to sleep. Aim for the 64bit version. 3 reasons:


- You can have more memory (more than 3.2GB effective)
- Applications don't have memory limitations that are feasible in the next 100 years.
- Tis generally faster in 99.9% situations. Unless it was written by a developer fag.


If you have a 32 bit OS, you are limited, both mentally and practically. Y U NO explain why? OK, hold on.


Every block has 8 bits (0 or 1, remember?) and it has it's own address so the computer knows where you stored data. The computer, if it works with 32 bit numbers and have 32 bit wide registers - or to generalize, can process 32 bit numbers, it can know about 232 memory addresses. And that's 4,294,967,296 blocks. If everyone of this blocks is 8 bit wide - or 1 byte wide - its 4,294,967,296 bytes of memory. And that's 4 GiB (notice the 'i' - that means it's divided by 1024, and not 1000. It's pronounced gibibyte - it's a binary system instead of SI decimal system). So the effective limit is 4 GiB for 32 bit. If you have 8 GiB, and the picture above I said i have 8 GB of memory - it's possible. Mac OS X "Lion" is a 64 bit computer so it can address more than 4 GiB of memory. 64 bit OS makes your computer go faster and multitask better. GO FOR IT!


HOMEWORK: how much memory can a 64 bit computer address? It's a very long number to write :)


The end is near


This blog post is long shit. Now, when it's done, I guess you know a little more about how memory behaves and works to our advantage, with a practical advice to upgrade to 64 bit Operating Systems. Chances are that you have a 64 bit capable CPU, so I ignored that part in giving you the advice. If you have a 32 bit CPU, than you don't have to concern yourself with this. My advice is to buy a new computer. All CPUs that you can buy as a general consumer today are 64 bit and even have multiple cores! Pair that with 4 GiB+ memory and your computer will go crazy bat shit fast. And pair that with an SSD and it'll go inhumanly insane shit fast. So the end of 32 bit computers and 32 bit operating systems are near. Windows 8 Server is cutting 32 bit support, and all better retailers are bundling 64 bit OS installed (you paid for the licence, don't worry. Windows 7 has the same licence number for 32 and 64 bit version, no sweat)


Oh and BTW, the end has come for a user to know squat about his/her computer. Now when computers are becoming more complex, and Operating Systems more specific, you need to know something. Otherwise you'll make stupid mistakes and have less then you paid for a computer that you paid much. Fight with knowledge. Good knowledge can make a cheap computer fight the big ones that have assholes using them.


Fear not the stuff inside that cool aluminium shell or a boring matte plastic box.