Why does partially full RAM cause lag?

53

13

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?

Mohammed Noureldin

Posted 2017-08-28T09:56:50.023

Reputation: 1 123

28I 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 – 2017-08-28T11:37:26.167

@DavidSchwartz, Nice info, but why to use that available RAM if that will cause lag? and as you said, if we have 1 gb free (with the fully word meaning), why there is still lags? – Mohammed Noureldin – 2017-08-28T12:32:10.087

You are sure that you are not confusing free space available for storage with free memory? – Thorbjørn Ravn Andersen – 2017-08-28T13:03:17.103

2You mean my HDD free space with my RAM? surly not. – Mohammed Noureldin – 2017-08-28T13:05:23.743

2@MohammedNoureldin I don't think you really do have 1GB free. What tool are you using? And what precisely is it telling you? Likely you have only a very tiny fraction of your RAM free, and that's normal and has nothing to do with your performance issues which have to do with the working set not fitting in RAM. – David Schwartz – 2017-08-28T13:07:07.900

Actually the system shows that to me, when I open android opened apps list (with that touch key), it shows that there is ~900 mb available. – Mohammed Noureldin – 2017-08-28T13:26:46.990

2As I said, "RAM can be both used and available". You have 900 MB available that is probably also mostly in use. (For example, memory that holds the actual programs that you're running is available, since the programs can be read from disk again if needed. But ejecting that from RAM can cause a huge performance drop. Available, as its name suggests, means that it can be used if needed, not that there is no cost associated with using it for a different purpose.) – David Schwartz – 2017-08-28T13:45:31.920

I understand that, but even if that 900MB is used (and available), that shoudn't be a problem as they maybe replaced, correct? otherwise we do not need that process (the whole ram is used) when that will cause lags, right? – Mohammed Noureldin – 2017-08-28T13:48:32.827

3They 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 – 2017-08-28T13:49:04.733

Why does it need more time to overwrite the data in RAM? Shouldn't it be just as normal as any normal RAM writing process? – Mohammed Noureldin – 2017-08-28T13:58:40.837

Let us continue this discussion in chat.

– David Schwartz – 2017-08-28T14:20:38.347

3What makes you think it is the RAM percentage that makes the device lag? – enkryptor – 2017-08-28T18:16:13.940

@enkryptor, It is obvious, when I have 1200 gb free, the performance is ok, when I have 600 mb, it lags like hell. – Mohammed Noureldin – 2017-08-28T20:58:08.353

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 – 2017-08-29T09:29:21.720

@enkryptor, my mobile is relatively old, it does not matter for me actually why my RAM is so full, I just wanted to understand what is going on behind the stages, and why does it lag although there is available memory. – Mohammed Noureldin – 2017-08-29T10:04:26.033

first you need to define lag? – None – 2017-08-29T14:53:52.947

Performance drop – Mohammed Noureldin – 2017-08-29T22:24:00.917

Which OS? How do you measure "lag"? – Reinstate Monica - M. Schröder – 2017-08-31T07:42:11.160

Android and Windows (for mobile and PC), nothing Special, just the performance drops, and I check RAM status by the any RAM status app. – Mohammed Noureldin – 2017-08-31T08:21:33.037

Answers

70

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.

LMiller7

Posted 2017-08-28T09:56:50.023

Reputation: 1 849

1Your 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 – 2017-08-28T15:45:19.573

7The 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 – 2017-08-28T15:51:48.817

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 – 2017-08-28T17:56:15.393

6It 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 – 2017-08-28T19:03:06.810

3We 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 – 2017-08-28T23:45:23.593

1"if I was dealing with pointers and these very low level things, will it still be able to do paging" - if you really want to understand, you need to read a good os book – Alex C – 2017-08-29T08:32:56.737

1@LMiller7 to most people "paging" means swap, which is certainly not essential and sometimes recommended to be disabled. – OrangeDog – 2017-08-29T17:04:04.163

From my experience, I think that memory manager of Windows or the part which decides when to use virtual memory (memory area which is on the swap file not in RAM) does not work correctly. That was until Windows XP, for Windows 7 not sure but maybe it is the same. For example, you have 2GB RAM, the OS and 1-2 applications occupy 900MB. You have around 1100MB free but the swap file is accessed... why? Now I put 8 or 16GB RAM and set swap file to 0. With 2GB RAM and 0 swap on XP frequently is shown warning for insufficient memory. Maybe Microsoft owns factories for production of RAM :) – i486 – 2017-08-29T19:21:05.877

Low memory warnings have nothing to do with a shortage of available RAM. Windows doesn't even have a warning or error for this. It means the commit charge is approaching the commit limit. Also, the "Pagefile Usage" graph in XP is showing the commit charge, not actual pagefile usage. – LMiller7 – 2017-08-29T20:55:42.637

@OlivierDulac When it comes to random access, RAM is not a thousand times faster than a hard drive, it's a million times faster. Nanoseconds vs. milliseconds. SSDs are much better, of course. When it comes to sequential bandwidth, it's 100–300× faster. (SSDs are slightly better). – derobert – 2017-08-30T23:12:05.043

29

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.

Mike Scott

Posted 2017-08-28T09:56:50.023

Reputation: 4 220

11Actually, 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 – 2017-08-28T13:47:05.500

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 – 2017-08-28T14:30:46.970

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 – 2017-08-28T15:47:19.083

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.

Will

Posted 2017-08-28T09:56:50.023

Reputation: 37

3It’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 – 2017-08-28T11:23:08.410

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 – 2017-08-28T11:27:51.757

8No, you are confusing trashing and paging. Trashing is just paging becoming pathologic. – xenoid – 2017-08-28T12:06:53.223

@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 – 2017-08-28T12:22:02.080

I am confused now, is the answer is correct? or should it get edited? – Mohammed Noureldin – 2017-08-28T12:22:12.717

I think it covers all bases now c: – Will – 2017-08-28T12:25:21.660

Would you mention that argument about paging in the answer? I don't know exactly what is happening in the background and how does paging affect all that process. It would be great to have it in your answer to explain the whole situation with an example. Thank you – Mohammed Noureldin – 2017-08-28T12:26:56.543

Already added. But I'll add a reference. – Will – 2017-08-28T12:32:35.870

1Comments incorporated, made into wiki answer, feel free to edit. – Will – 2017-08-28T13:41:01.210

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.

Gilles Lesire

Posted 2017-08-28T09:56:50.023

Reputation: 111

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 – 2017-08-29T14:32:16.553

1Sure, 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 – 2017-08-29T14:43:14.873

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).

Machavity

Posted 2017-08-28T09:56:50.023

Reputation: 467

1Yes, 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 – 2017-08-30T20:35:59.147

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 – 2017-08-31T09:38:22.907

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.

codykochmann

Posted 2017-08-28T09:56:50.023

Reputation: 200