Oops
@elorimer , post this just as your posted the above….Sorry
Not to beat this swap discussion to death - but knowing what ”swap” means/implies is important to understand.
As
@ColinTaylor pointed out, the Linux kernel thinks in terms of virtual memory. Virtual memory = real memory (i.e. 2GB in some routers) and some pre-allocated storage space it calls “swap”.
A very “general” rule of thumb (used by many of the major Linux Distrubutions) create a swap space on a drive that is 2X the real memory. Some do 1X. So in our 2GB memory based router a swap of 2GB to 4GB would likely be appropriate.
So, now the kernel has 2GB real memor and 2GB of swap space = 4GB virtual memory.
Here is the key point, and why it’s called “swap”.
The processor(s) can’t execute or even access the code or data in the swap area. If the system runs out of real memory (2GB) it selects a chunk of memory (often around 4KB - pagesize) and copies it out to the swap device/disk. Now that chunk of real memory is free to use. When and if the time comes that the processor needs to get to that “chunk” of memory, the system again needs to find another “chunk” of real memory, copy that to the swap device then copy the original chunk back into real memory. This is a “swap”.
Rinse and repeat as necessary.
Think about the delays in doing the above. As
@Tech9 pointed out, the speed difference between real memory and an SSD/Disk/Storage is in the order of 100’s times!
And again, the processor can only run software and access data that is in real memory.
If the system gets to the point that this “swapping” happens often, the system slows to a crawl.
This is one of the reasons most Linux kernel people recommend against too large a swap space. The system begins to “thrash”. Either get more real memory (which can’t do done on our routers) or reduce the number of running programs or live with a very slow router…