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:
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.
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.
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.3473What 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.947Performance 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