How computers use memory and storage
In this video from ITFreeTraining, I will look at how computers use memory and storage. Understanding how this works will allow you to determine how much memory and storage will meet your needs.
Memory and Storage Analogy
To understand how memory and storage work, or to explain it to someone who is having trouble understanding, consider that the main memory of the computer is like a desk. You have limited space on the table, but you can access things on the table very quickly.
The storage of the computer is like a filing cabinet. The filing cabinet can contain a lot more stuff than the desk does; however, in order to use anything from the filing cabinet you first need to retrieve it. Once you have something from the filing cabinet, you need to take it to the desk. If the desk becomes full, you need to remove something from the table and put it in the filing cabinet. Essentially the desk acts like main memory and the filing cabinet acts like secondary storage. Let’s have a closer look.
Main Memory
If you consider the table in our example, this holds data the computer can access quickly. This is often referred to as main memory or primary storage. The CPU requires this memory in order to operate. Since the CPU needs to access the data quickly, the memory will be connected to the computer using a high-speed connection.
Main memory nowadays uses highspeed read/write memory such as DDR. However, it is possible to have read only memory called ROM. For main memory, it will always be fast memory that can be accessed randomly. Randomly means you can access any data in the memory in any order. Thus, memory like this may be referred to as Random Access Memory or RAM.
Using the example of our table, we can reach out and grab anything on the table quickly. You could say that we could put things on top of each other, however for the sake of our analogy we are not going to do that. Essentially everything on our desk is laid out evenly. So essentially, everything is easy to get to. This means taking anything off our desk should take about the same amount of time. RAM works the same way, anything we access in the RAM takes roughly the same amount of time to access.
On modern computers, you will generally find that data found in a ROM will be copied to the computer’s RAM. The BIOS is a good example of this. When the computer starts up, the read only code in the BIOS is copied to RAM and executed there. This is a much faster way of executing the code then directly from the BIOS. Also, it is easier to design a computer to work this way rather than designing a CPU and motherboard to access the ROM directly. You may find in very old computers and some devices the code in the ROM is executed directly rather than being copied to RAM first.
Secondary Storage
Next, I will look at secondary storage. Secondary storage is much cheaper but slower than main memory. Secondary storage may be random access or sequential access. Examples of secondary storage are solid-state or hard disk drives, optical drives and tape drives.
Devices such as solid-state drives, hard disks and CDROMs are all examples of random-access devices. Tape drives are examples of sequential access. The reason that a tape drive is considered sequential access is because if you need to access data at the end of the tape, the tape drive needs to go through all the tape before it, in order to get to the end. This can take considerable time. In contrast, random-access devices can access any part of the device’s storage very quickly.
Nowadays, tape drives tend to be used for backups. This is because they can store a lot of data very cheaply. Since the data is only required rarely or in an emergency, it is an acceptable trade-off that it may take some time to get the data back versus the decreased cost.
Volatile and Non-Volatile
The next point to consider is the difference between volatile and non-volatile. Volatile storage requires constant power in order to keep its data. If the power is lost, so is all the stored data.
Volatile storage is usually used for main memory. Main memory needs to be fast. Having to keep main memory powered is a small price to pay for extra speed. The disadvantage of this, besides having to have it powered the whole time, is generally heat will be generated and thus the device will need to be kept cool or have some way of cooling itself.
By contrast, non-volatile memory does not require power in order to maintain its data. It is mainly used for secondary storage. For example, it is used for devices like solid-state and optical drives.
Generally, you will see particular devices used for main or secondary storage. For example, you won’t ever see an optical drive used for main memory. However, you may sometimes see some technology used that is a mixture of different technologies. For example, a hardware RAM disk will appear in the operating system as a storage device, however the stored data will be stored in memory modules. This makes it very fast, but if power is lost, the data on the RAM disk will also be lost. In some cases, you will see hybrid devices which have storage, such as a solid-state drive as well as the memory modules. If the power is lost, a battery is used to power the device until the contents of the memory modules can be transferred to the solid-state drive. This allows the device to retain data even if the power is lost.
You don’t tend to see hardware RAM devices on the market that often; this is because memory is quite cheap, so it is difficult to justify the extra cost for such a device. Also, devices such as solid-state drives have become quite fast as well, making it even harder to justify the cost.
How a computer uses RAM
Now that we understand the different types of storage, let’s look at how the computer uses them. To start with, consider that you have your application that you want to run. The application is stored on the solid-state drive. This allows the code and data for the application to be stored, even if the computer is not powered for long periods of time.
The next step is the application is transferred to memory. Being in memory allows the application to be quickly read. Any data associated with that application can be changed quickly. In theory you could do this with secondary storage; however, it would be very slow and inefficient.
The last step is to execute the instructions of the application on the CPU. This makes the process very quick; however, any changes made will need to be saved to storage, otherwise they will be lost. These are the basics of how computers use memory; however, this is just scratching the surface.
Virtual Memory
The next point I would like to cover is virtual memory. Put simply, virtual memory allows the computer memory to be expanded to secondary storage.
To understand how this works, consider that we have our software application. The application requests eight gigabytes of memory. The problem is that there are only six gigabytes of physical memory in the computer. In order to allow the application to run, two additional gigabytes are stored on secondary storage.
So, what the application sees is eight gigabytes of memory which is stored across physical memory and secondary storage. Virtual memory allows the computer to access more memory than what it physically has installed. Let’s have a closer look at how it achieves this.
Memory Mapping
The CPU in a computer maps an application memory to RAM or to storage. This way the application sees only a flat space of memory regardless of where the memory is being stored.
To understand how this works, consider our application has requested some memory. The application sees this memory as a contiguous block of data. That is, no breaks, spaces or gaps.
The memory maps to the memory management unit inside the CPU. Each block of memory is referred to as a page. The CPU this will determine what page sizes it supports. Generally, a 4k page size is very common. If the CPU support multiple page sizes, it is up to the operating system to decide which page size it wants to use.
The memory management unit keeps a table which records where the page is stored. The physical data for each page can be stored in physical memory or storage. So essentially, this table maps the data from the application to where it is physically located.
The application only sees a block of memory. The operating system is free to store each page of the block where it likes. If the page is stored on secondary storage, the operating system will read the page into memory. If the memory is full, the operating system will have to exchange one of the pages in memory with the one stored on storage. This is called swapping, because essentially the operating system is swapping the data between memory and storage.
Since the operating system is free to store the block wherever it likes, the memory can also be stored out of order. This becomes important when the computer has been running for a while and memory is allocated and deallocated. When this occurs over time, the memory will become more and more fragmented.
Computers need a system like this, otherwise your memory will start looking like a Tetris game you are losing. You will get to a stage where the computer will be asked to allocate a block of memory and there will be no free block of that size. Having a system where all the blocks are the same and can be divided up as required, prevents this problem from occurring.
If you don’t have enough memory in your computer, your computer will start paging and the performance of the computer will start to slow down. When this occurs, you should install more memory to improve the performance of your computer. However, if you have extra memory that your computer is not using, this can be utilized to improve performance. Let’s have a look how.
Memory Utilization
To understand how a computer will use memory, I will have a look at the task manager in Windows. The performance tab on the task manager will show how much memory is being used and what it is being used for.
First you can see in the task manager the amount of memory that is currently in use by software. The graphic gives you an indication of the total amount of memory that is used, but you can see below this is also the amount represented as a number.
When you start up an application it will require memory. The memory, shown here using our table and filing cabinet analogy, is the table. This memory is like the table, it is readily available and can be accessed quickly without delay.
The next section is ‘modified’. You may not see this on your system or it may be so small it is not that noticeable. Modified is data that is waiting to be written to the page file. What Windows will do, to make the best use of memory, is attempt to determine what is referred to as a working set.
A working set is a collection of memory that Windows thinks will most likely get used later. Windows will use a number of different measures to determine this. For example, if the memory has never been accessed after a certain period of time, there is a chance that it won’t be accessed while the application is running.
Memory that is in the modified section will be transferred to the page file. The idea behind this is that, rather than waiting for memory to become full, Windows will try and proactively move memory to the page file ahead of time. If Windows starts getting put under a lot of load, some of the work is already done ahead of time. Also, this frees up some memory which can be used for other purposes. Let’s have a look.
The next section is ‘standby’. This is memory that is used for file cache. When files are accessed, a copy of this file can be stored in memory. The idea behind this is that, if the computer accesses the same file again, rather than reading it from storage a second time, it can just read it from memory which is a lot faster.
To determine how much standby memory is in use, you can see a text figure, cached, which has the amount as 1.7 Gigabytes. This essentially means 1.7 Gigabytes is being used to cache files. The longer Windows is running, the larger this generally becomes. This make sense, because if you have free memory, you may as well be using it for something.
Next to this is the ‘free’ section. This is the amount of memory that is currently not being used for anything. A figure is showing the current free memory being 2.8 Gigabytes. You can see that Microsoft has made this the same color as the standby section. This is because standby will often consume all free memory. This is not a problem as standby is only used for cache and basically a copy of what is contained on storage. Since it is a copy, it does not matter if it is lost. If the computer needs more memory, it will simply release some standby memory and use that. So, all the memory that is available to the operating system can be used is comprised of standby or cached memory plus the free memory. You can see why Microsoft uses the same color since, essentially, it all can be used as required.
With both physical memory and a page file, it can be a bit confusing to determine how much memory is in use at one time. This is made more complicated because memory that has not been accessed may proactively be moved to the page file.
To make it easier, Microsoft has included a ‘committed’ figure. The committed figure is the amount of physical memory and page file that is currently in use. We can see this is 1.1 Gigabytes. This means that currently running software on this computer is using a combined 1.1 Gigabytes of physical memory and page file.
Notice that this figure is ‘slash’ 5.2 Gigabytes. So essentially, this figure is saying that the total memory of this computer, that is physical memory plus page file, is 5.2 Gigabytes with 1.1 Gigabytes currently in use. This gives us a better indication of how much memory is available to programs. Remember, however, if the computer starts doing a lot of paging, that is, storing a lot of memory in the page file, this will slow down the performance of the computer.
Some memory will be used by the operating system and some by applications. The paged pool figure will tell you how much memory is being used by the operating system but can be moved to the page file if required.
Some memory used by the operating system cannot be transferred to the page file. This is because it is critical to the operating system. For example, software that runs virtual memory, thus controlling data being moved in and out of the page file. This is referred to as non-paged pool.
You can see that the page and non-paged pool is quite small. This was more of a concern in the old days when memory was a lot smaller. Nowadays, computers have quite a lot of memory and thus the amount of memory used by the operating system none-pageable memory is small by comparison to the total memory.
Summary
To end this video, I will do a quick summary of the major points. Programs are stored in secondary storage but executed in main memory. This is because main memory is faster than secondary storage. Although technically it would be possible to run a program in secondary storage, it would be so slow it would not be worth it.
In order to get faster speed, there is a trade-off, which is that main memory is usually volatile. This means that if power is lost to the computer, the contents of the memory will also be lost.
In order to increase the amount of memory that is available to the operating system, the operating system can use secondary storage as memory. The process involves moving memory to storage or vice versa. This process is known as paging or on Linux-systems swapping.
Paging is slower than physical memory, but the advantage is it allows you to run programs or big jobs that you may not otherwise be able to run. It is important to remember that paging will always occur no matter how much free memory your system has.
This is for two reasons; firstly, extra memory will be used for caching. If memory is unlikely to be used by a program, it may as well be put in the page file and that memory used for caching. You will, however, find that the more memory that is being used, the less memory will be allocated for caching. If you are using all your memory for programs, you will find only a very small amount will be used for caching, if any at all. The operating system will attempt to make smart decisions about what to move out of memory into the page file, but if it makes bad decisions, this will slow the computer down. This is why caching is less of a priority over software, since it is difficult to predict with certainty which memory a program will need.
The operating system will proactively page a small amount of memory to the page file. The idea behind this is that if the operating system needs to perform a lot of paging later on, some of the work is already done. This has some risk since, if the operating system chooses the wrong memory to page, this will slow the operating system down, thus only a small amount of proactive paging is done. For this reason, even if your computer system has very large amount of memory installed with very little of it being used, don’t be alarmed if you see a small amount of paging. If the paging starts becoming significant, then you will need to increase the amount of memory installed in your computer.
End Screen
This concludes this video on how the computer uses memory. I hope you have found this video useful, and I look forward to seeing you in more videos from us. Until the next video, thanks for watching.
References
“Computer memory” https://en.wikipedia.org/wiki/Computer_memory
Credits
Trainer: Austin Mason http://ITFreeTraining.com
Voice Talent: HP Lewis http://hplewis.com
Quality Assurance: Brett Batson http://www.pbb-proofreading.uk