4

If RAM usage is higher than the level written in «swapiness», kernel will use SWAP. Ok. But how does the kernel «order» programs? Is it «first come, first served» for RAM? Or biggest programs in SWAP? Most used programs in RAM?

I would like to know how the kernel decide between RAM or SWAP in the case if RAM is full or almost.

1

1 Answer 1

7

The kernel memory management doesn't use the concept of "program" to organize the memory, but pages. The kernel decides based on a 'least recently used' (this may have changed, but the latest references I could find say so), in which when the kernel under memory pressure (by swappiness, it swaps out those pages which are rarely accessed to give space to more frequently accessed or new pages (or why not, disk cache).

So, none of your assumptions are correct. The kernel doesn't order programs to the swap, it statistically select which parts of the memory will commit to swap. The method is LRU. The size of anonymous or private memory committed by the program doesn't affect the way the kernel will select which pages will be committed.

You can see how many KiloBytes (or pages) have the kernel committed to swap using smem -s swap -t -k -n.

3
  • But how did the kernel knows what pages was recently used? Does CPU increment some counter for page on every byte access? 0_O Commented Jul 24, 2021 at 14:58
  • @BogdanMart not byte, but pages. Again, the page itself may have tracking that the kernel just scans looking for which pages to evict. Some details here lwn.net/Articles/257541 Commented Jul 24, 2021 at 15:08
  • @BogdanMart there are some updates here too lwn.net/Articles/856931 Commented Jul 24, 2021 at 15:11

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.