Shortage of RAM in Linux on a working PC: optimization and actions when it freezes

    On any operating system, often there is not enough RAM. We will consider how to save on increasing the hardware resources of a Linux machine and continue to use a Linux computer under more or less comfortable conditions in case of insufficient memory.

    This situation is typical: there is a swap (swap, swap partition), which begins to be used when there is not enough RAM, and it is located on the HDD, that is, a hard disk with a low reading speed. In such situations, the operating system begins to slow down, the mouse cursor freezes, it is difficult to switch to the neighboring tty, etc. Why? Because the Linux kernel scheduler cannot execute a request for some action in a running program until it gets access to its RAM, it cannot perform the next action either, a queue of requests to read from disk is formed, and the system freezes precisely because that queue processing is much slower than what the user wants.

    If at such a moment run htoporuptime, then the Load Average (LA) indicator will be very high, despite the low workload of the processor cores. The combination of a high load average and low processor load indicates a clogged processor queue.

    Often the Internet is advised to change the Linux kernel parameter vm.swappiness. You can find out its current value on your system as follows: The

    sysctl vm.swappiness

    answer will be 60 almost certainly. This means that the Linux kernel begins to swap rarely used pages of RAM, when the use of free RAM reaches 100% - 60 % = 40%. Often there are recommendations to set, for example, vm.swappiness = 10, so that the swap does not start to be used until the RAM load reaches 90%. In factno need to touch vm.swappiness, you are not smarter than Linux kernel developers who have set 60 by default for a reason . Why?

    Imagine that you have only 4 GB of RAM, of which 3 GB are currently occupied, vm.swappiness = 10, swap on the hard disk (HDD) is 0% busy, and you open a heavy site in the browser, which requires more, than the available free 1 GB, for example, 2 GB. The operating system begins to urgently send at least 0.5 GB (and in fact more) to the swap so that the necessary amount of RAM can be allocated to the browser. This procedure becomes the highest priority, and you will have to sacrifice even the movements of the mouse cursor to execute it as quickly as possible. Are you waiting. 5 minutes pass, and the system hangs because it has finished the procedure of 100% loading of the access queue to the slow hard disk, on which the RAM (swap) is located. With default vm.in advance , and a sharp hang for 5-10 minutes does not occur.
    UPD The comments suggest that this is not an exact description of vm.swappiness.

    zram and swap priorities


    I recommend enabling zram - transparent compression of RAM contents . In Ubuntu this is automated, just install the package:

    sudo apt install zram-config

    Hereinafter for Rosa, Fedora distributions everything is the same, but instead of zram-config -

    zram-start.

    The systemd service zram-configon Ubuntu will be automatically added to autoload when the package is installed and launched when the system reboots. To start manually:

    sudo systemctl start zram-config

    stop:

    sudo systemctl stop zram-config

    Delete from autorun:

    sudo systemctl disable zram-config

    Add to autorun:

    sudo systemctl enable zram-config

    When launched, zram-config takes a number equal to 50% of the total RAM, then makes one virtual device / dev / zramN, where N starts at 0, for each processor core, and the volume of each / dev / zramN is 50% of the total RAM memory divided by the number of processor cores. This is done to parallelize the compression of the contents of RAM on the processor cores; as far as I know, on modern Linux kernels one device / dev / zramN is enough, and it will parallelize itself, but I am completely satisfied with the zram-config sparkling work, and I prefer not to get into it with my hands.

    Commandswapon -sdisplays a list of all swaps involved, indicating their priority. The swap whose priority is higher is used first. If you already have a disk swap and zram is enabled, then in the case of the auto-configurator package described above, the priorities out of the box will be correct. For example, a disk swap will have -1, and all / dev / zramN will have 5. Thus, zram is used first, and only then is the disk.

    By the way, zram is often used on smartphones, it does not create any noticeable load on the processor with the default compression method lz4.

    You can also specify the priority of the swap in /etc/fstab. Let me show you an example of how this is done on my working computer with 6 GB of RAM.

    $ cat /etc/fstab | grep  swap
    # swap на SSD
    UUID=844fc9fb-509d-4dab-9ea5-a3d5142f76d8 none    swap    sw,pri=2      0       0
    # swap на HDD для гибернации
    UUID=b643c42a-0abd-4f35-8865-7a51be5769e8 none    swap    sw,pri=1     0       0
    

    The mount option sets swap pri=Xpriorities. If you also enable zram, then the picture will be like this:

    $ swapon -s
    Filename	 	  Type		Size	Used Priority
    /dev/sdb3         partition	1005564	0	2
    /dev/sda2         partition	6655996	0	1
    /dev/zram0        partition	690764	0	5
    /dev/zram1        partition	690764	0	5
    /dev/zram2        partition	690764	0	5
    /dev/zram3        partition	690764	0	5
    

    First of all, it will swap in zram, that is, it will be compressed inside the RAM without using an external device for swap, in the second - use a small swap on the SSD. Almost 6 GB of swap to HDD will almost never be used, however they will be needed if I want to send the computer to sleep mode in conditions of high RAM load. (Actually my zram is disabled).

    On office PCs with 4 GB of RAM (Xubuntu 16.04, 17.10) I always put the package zram-config. Chromium, according to observations, by eye, is very well compressed in RAM, as a result of which zram allows you to make work much more comfortable without upgrading the iron.

    Quickly knock out a program overloading RAM. RAM reserve for SSH


    It happens that even with vm.swappiness = 60 some feature, usually the browser, requires a lot of RAM, and the system freezes. It is solved very simply: the key combination Alt + SysRq (PrintScreen) + F forces oom_killer to force itself to turn off and kill the process, which at the time of the call takes up the most memory. Strictly 1 process for 1 call, and strictly something will be killed. If you click many times in a row, then most likely the graphical session will restart. A process kill event is shown in dmesgred.

    However, this thing called Magic SysRq is out of the box in most distributions, because an unprivileged user can kill absolutely any process. For this, the kernel parameterkernel.sysrq, To find out the current value can be as follows:

    sysctl kernel.sysrq.

    Alt + SysRq + F requires kernel.sysrq = 1 to work. To do this, we modify the kernel parameters located in the /etc/sysctl.conf files (usually the symlink on /etc/sysctl.d/99-sysctl.conf) and /etc/sysctl.d/*.conf. It is best to create a separate file:

    sudo nano /etc/sysctl.d/99-dumalogiya.conf

    In it we write:

    # включить все комбинации Alt+SysRq, в т.ч. Alt+SysRq+F для принудительного вызова OOM Killer
    kernel.sysrq = 1
    # с 8 МБ увеличим размер памяти, который будет гарантированно не занят в системе, чтобы у нас могли работать SSH и пр.
    vm.admin_reserve_kbytes = 60192
    

    Press Ctrl + O, Enter to save.

    In the case of the browser, Chromium Alt + SysRq (PrintScreen) + F will cut down one tab, without closing the browser itself, which is very convenient.



    Magic SysRq keyboard shortcuts are intercepted directly by the Linux kernel, so they work even when the X server hangs due to the processor queue.

    vm.admin_reserve_kbytes - this is the size of RAM in kilobytes, which will be kept guaranteed free for administrative needs, for example, SSH. The default is about 8 MB. It is advisable to increase the number 60192 almost from the bulldozer.

    I packaged the /etc/sysctl.d/99-dumalogiya.conf file into the dumalogiya-sysctl deb package, which I put into my repository and put it on all computers, it’s very convenient, you can centrally update the config, and you do not need to configure each machine manually. As an instruction for building simple deb packages “on the knee” I recommend https://debian.pro/1390 . The repository was created using aptly, which simply creates the structure of files and folders inside the web server directory.

    Also popular now: