54

Why does partially full RAM (let's say 80%), makes the device lag a lot, even though there is some space available to store more data? What I expect is that it keeps working well till 99%.

I notice that on the mobile more than on the PC, as it has smaller RAM, e.g. my mobile has 2 GB RAM; when I have less than 1 GB of RAM free, it lags a lot! Why does that happen, even though it still has ~ 1 GB to use?

  • 28
    I suspected that you are assuming that RAM that is available is not being used. So you see 20% RAM available and think that only 80% of the RAM is being used. This is incorrect, RAM can be both used and available. (For example, the machine I'm using now has 32GB of RAM and 21.1 GB are available, but only 1.8 GB are free.) – David Schwartz Aug 28 '17 at 11:37
  • 2
    You mean my HDD free space with my RAM? surly not. – Mohammed Noureldin Aug 28 '17 at 13:05
  • 3
    They can be replaced, sure. But that will have a huge negative effect on performance -- exactly as you're seeing. If the working set exceeds RAM, performance will be terrible, no matter how much RAM is available. – David Schwartz Aug 28 '17 at 13:49
  • 3
    What makes you think it is the RAM percentage that makes the device lag? – enkryptor Aug 28 '17 at 18:16
  • 4
    @MohammedNoureldin I guess there must be a reason why your RAM is busy (a number of processes are working and doing something). This might be the same reason why the device is lagging. The RAM loading might be just a symptom, not the reason itself. – enkryptor Aug 29 '17 at 9:29
71

There is much involved here but I will try to explain it as simply as I can and in a way applicable to just about any OS.

There are 2 basic principles here:

  1. The sum total of everything that needs to be in RAM and those things that would benefit from being in RAM is almost always greater than the size of RAM. Things that would benefit from being in RAM include process working sets and the standby list. The latter contains data and code that was once in active use but has since lapsed into inactivity. Much of this will be used again, some of it quite soon, so it is beneficial to keep this in RAM. This memory acts as a kind of cache but is not really essential so is in the category of available memory. Like free memory it can be quickly given to any program that needs it. In the interests of performance standby memory should be large.

  2. The frequency of use of memory blocks is far from random but can be predicted with considerable accuracy. Memory is divided into blocks, often 4K bytes. Some blocks are accessed many times per second while others have not been accessed for many minutes, hours, days, or even weeks if the system has been up long enough. There is a wide range of usage between these 2 extremes. The memory manager knows which blocks have been accessed recently and those that have not. It is a reasonable assumption that a memory block that has been accessed recently will be needed again soon. Memory that has not been accessed recently probably won't be needed anytime soon. Long experience has proven this to be a valid principle.

The memory manager takes advantage of the second principle to largely mitigate the undesirable consequences of the first. To do this it does a balancing act of keeping recently accessed data in RAM while that keeping rarely used data in the original files or the pagefile.

When RAM is plentiful this balancing act is easy. Much of the not so recently used data can be kept in RAM. This is a good situation.

Things get more complicated when the workload increases. The the sum total of data and code in use is larger but the size of RAM remains the same. This means that a smaller subset of this can be kept in RAM. Some of the less recently used data can no longer be in RAM but must be left on disk. The memory manager tries very hard to maintain a good balance between memory in active use and available memory. But as the workload increases the memory manager will be forced to give more available memory to running processes. This is not a good situation but the memory manager has no choice.

The problem is that moving data to and from RAM as programs run takes time. When RAM is plentiful it won't happen very often and won't even be noticed. But when RAM usage reaches high levels it will happen much more often. The situation can become so bad that more time is spent moving data to and from RAM than is spent in actually using it. This is thrashing, a thing the memory manager tries very hard to avoid but with a high workload it often cannot be avoided.

The memory manger is on your side, always trying it's best to maintain optimum performance even under adverse conditions. But when the workload is great and available memory runs short it must do bad things in order to keep functioning. That is in fact the most important thing. The priority is first to keep things running then make then as fast as possible.

| improve this answer | |
  • 1
    Your answer helped me a lot, thank you! That memory manger is a part of my OS right? so if I was dealing with pointers and these very low level things, will it still be able to do paging? – Mohammed Noureldin Aug 28 '17 at 15:45
  • 7
    The memory manager is part of the OS. Paging is a basic function of the memory manager and is as essential to operation as breathing is to you and I. It cannot be stopped. – LMiller7 Aug 28 '17 at 15:51
  • 2
    @MohammedNoureldin: When you're writing user-space applications (whose memory views are isolated from that of the kernel and other processes) you can’t access physical memory directly. All you can access is a view on memory that is managed by the – you guessed it – memory manager with all its features (probably) including paging. Memory pointers only refer to points in the address spaces of a memory view. They are a language concept used in both kernel and user space code but that doesn't mean the two memory views are identical. Don't confuse process memory management with kernel MM! – David Foerster Aug 28 '17 at 17:56
  • 6
    It is 'obvious' or 'well known', but maybe still should added in this answer: memory (ram) is in the order of 1000x faster than a hard drive (well, depensd on a lot of things, for example ssd/ide/sata , etc). The OS execute code from ram (+cache), not from disk. When memory gets low, and the OS needs to "swap" portions of ("unused") code from memory to the disk, to free ram to execute something else, this will take a 'lot' of time (the swapping), and if this happens often (ex: you have many programs, each needed a swap to have their code be loaded into memory), it will slow things down a lot. – Olivier Dulac Aug 28 '17 at 19:03
  • 3
    We usually don't use the term "swap" to refer to loading code into memory. That's usually called a "fault". Usually, the term "swap" is only used for information that has to be written to disk before it can be ejected from RAM. – David Schwartz Aug 28 '17 at 23:45
28

All modern operating systems use otherwise unused memory for caching data so that it can be accessed from fast RAM instead of slower storage. They will generally report this as free memory, since applications can clear the cache and use it if they need to, but it's still actually being used. The less of it there is, the less data can be cached, and the slower the computer will be.

| improve this answer | |
  • 11
    Actually, applications don't need to "clear" the cache. They just request RAM from the OS. The OS, seeing that it has no unused RAM, will clear part of the file cache and give the now zeroed-out RAM to the application. The application can't tell where that RAM came from. – MSalters Aug 28 '17 at 13:47
  • 5
    @MSalters Yes, I agree, the application asks for RAM and the OS clears cache if necessary. I was trying to keep it simple. – Mike Scott Aug 28 '17 at 14:30
  • That was some how clear, but I am a little bit more aware about details, therefore I have the feeling that something is missing and I am in need for more details @LMiller answer helped me actually, that will be great if you could revise the information in it. – Mohammed Noureldin Aug 28 '17 at 15:47
4

This answer has been mostly rewritten to reorganise the structure and make the message clearer. I have also opened it as a community wiki answer; Feel free to edit.

Paging is a memory management scheme through which fixed-size blocks of memory have processes assigned to them. When memory usage rises to a high level (i.e. 80% capacity), paging begins to extend from RAM into vRAM (virtual RAM).

vRAM is located in system storage, usually within a hard drive, or other sizable storage locations.

Processes are assigned part of your hard drive to run as memory and will treat their section as RAM. This is a perfectly normal process, however, when the time spent to transfer data to and from the vRAM increases, system performance decreases.

While dedicated RAM is accessed directly through the motherboard from the CPU, which provides a fast connection, virtual RAM must transverse cabling between the board and the location of the vRAM.

This however, causes only slight performance impact. When the rate of which paging to vRAM takes place increases drastically (when dedicated RAM approaches capacity), thrashing takes place.

Thrashing is the practice of quickly and rapidly transferring pages of memory into your virtual memory. This takes a huge toll on performance as more time has to be spent fetching and addressing data.

Lets say, you want to write down a number 30 digits long. You could either sit next to your screen with your notepad and write it (using the dedicated memory), or you remember chunks of 5, run into the next room and write it down on your notepad in there (using virtual memory). Both get the job done, but which is going to be quicker?

Find out more about thashing here!

A big thanks to the contributors of this answer including Daniel B, xenoid and Jon Bentley.

  • 3
    It’s not like thrashing is intentional. It’s just a side-effect of paging and competition for physical memory. Also, virtual memory isn’t “created” by moving pages to disk. – Daniel B Aug 28 '17 at 11:23
  • Thrashing is a by-product, true, but its an immensely useful one in times where processes run over, which helps prevent gridlock. And yes, virtual memory is occupied rather than created. I will edit that in. – Will Aug 28 '17 at 11:27
  • 8
    No, you are confusing trashing and paging. Trashing is just paging becoming pathologic. – xenoid Aug 28 '17 at 12:06
  • @xenoid Paging is a memory management scheme; Its like saying that I'm confusing hitting a deer with my car and the highway system. Thrashing is the process which causes this dip in performance; I do, however, agree with you that this is a result of the paging system. – Will Aug 28 '17 at 12:22
  • 1
    Comments incorporated, made into wiki answer, feel free to edit. – Will Aug 28 '17 at 13:41
1

It's because the OS has to do a lot of paging (loading parts of active programs) and swapping (moving data in RAM to HD and vice versa) to keep your software running. When new pages need to be loaded which might need more space than the availble 20%, the OS will have to swap out existing pages in the RAM which it consideres less likely to be used soon. Especially at the start up of other programs. Swapping out and back in pages takes a lot of time and slows down your PCs performance drastically because you are now working at the speed of the HD, not the RAM.

It helps a little bit on an HDD to create a special partition on your HD and assign it as dedicated "swap" (don't use it for "real" files) space so the swapping is less affected by the HDs fragmentation.

| improve this answer | |
  • ROM is just nomenclature for the HD when talking about caching, paging, etc. Anything that is not the working memory. And ROM usually is slower than RAM, almost always have been. Old computers copied the data from the ROM to the RAM (ROM Shadowing) when booting because it worked faster. – Gilles Lesire Aug 29 '17 at 14:32
  • 1
    Sure, but ROM and RAM are also used as non-volatile vs volatile memory? BTW ROM usually is slower, ROM is copied to the RAM (ROM Shadowing) on bootup for performance gains. But sure, I'll change the one reference to fix your nitpicking. ;) – Gilles Lesire Aug 29 '17 at 14:43
1

Remember, hard drives are an order of magnitude slower than RAM, and RAM itself isn't all that speedy to begin with (in overall architecture). In order of speed of access (where each rung is an order of magnitude slower than the one above) you have

  1. Processor registers - In practical terms, registers are available within 1 processor cycle. Considering that processors do billions of cycles a second (3 GHz = 3 billion cycles per second), this is crazy fast.
  2. Processor cache - Depends on the level, but these are still pretty fast (3-5 cycle availability for L1 cache).
  3. Random Access Memory (RAM) - The random part means you don't know what state it's in when you access it. Think of a package delivery man who has to stop, pick the package up, walk to the door and ring the doorbell and wait for a response. Maybe you don't wait at all, or maybe you wait a minute or two for Mrs. Smith to shuffle to the door for the back of the house. In practical terms, we're talking anywhere from 14-27 cycles (depending on what state the RAM was when we accessed it).
  4. Hard Disk Drive - There's a physical process here now and while it happens as fast as possible you're waiting for heads to move and tracks to move under those heads. In practical terms, a 7,200 RPM HDD can complete a revolution in about 4 ms, or somewhere around 750,000 cycles for a 3 GHz processor. That's slow.

The Virtual Memory Manager is a gambler. It's betting you don't need all of your RAM all of the time, so it makes an educated guess and rolls the dice that your document program (which has been in the background for the last 10 minutes while you read this) isn't really important and it shoves it to the HDD.

But then you click back to the document! Now the VMM has to load all that data back from the HDD. Worse, if you're low on RAM, it now has to push other data (more gambling) to the HDD to free up usable space. Linux likes to live on the edge here. It will fill the vast majority of the RAM with frequently used data (great for servers with few processes).

| improve this answer | |
  • 1
    Yes, the memory manager is a gambler but that could very easily be misunderstood. If memory access was truly random then the memory manager would be wrong as often as right and the whole system would break down. But programs tend to exhibit a strong locality of reference. That is most memory accesses tend to be localized to a relatively small area of memory. They do move around but slow enough for the system to work. The odds are stacked in the memory managers favor and mistakes are quickly corrected. This has been proven over decades of experience – LMiller7 Aug 30 '17 at 20:35
  • The Random in Random Access Memory refers to the fact that you can access any of it randomly. (As opposed to having to wait for the data to pass underneath read heads as in a hard drive or tapes, and then access the data sequentially.). – Phil Aug 31 '17 at 9:38
0

The thrashing answers pretty much nail it. If you're able to, you can minimize how quickly this happens by reducing the swappiness (how much ram the system will allow to be used before moving things to swap space). I like to set systems to stay out of swap until ram reaches 99% for server applications since the default would effectively mean that I would need to code for things to only utilize 80% of the ram to take advantage of caching without being penalized for pushing the system into swap space.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.