[info] Unloading freed RAM

rado84

Well-Known Member
Joined
Feb 25, 2019
Messages
1,083
Reaction score
921
Credits
8,303
I saw dos2unix mentioning a few useful things I already know but he forgot to mention one very annoying function in the linux kernel that is supposed to be a benefit but in fact it does the opposite - unloading free RAM. For instance, you've played a game that uses 16 GiB RAM, your distro normally uses 4 GiB and that's 20 GiB RAM. But when you close the game, the used RAM remains at 20 GiB. Why? Because of a function called "huge pages". This appeared somewhere in the kernel 6.x versions and its main purpose remains a mystery to me bc it creates more problems than it fixes.

But there's a very easy fix for that:
1. Open grub.cfg with your favorite text editor:
Code:
sudo pluma /boot/grub/grub.cfg

2. Find the 'linux' line. The easiest way for that is to use CTRL+F and paste this in the search field:
Code:
linux    /boot/vmlinuz-linux

Assuming you haven't added anything, it will look something like this:
Code:
linux    /boot/vmlinuz-linux root=UUID=1a2db951-3fc5-4880-9e2a-be94175245eb rw

3. AFTER the rw characters add space and then paste this:
Code:
transparent_hugepage=never
and then reboot.

Note: when you edit grub.cfg directly, you DON'T need to run the grub update command. System reboot is enough.
Once the hugepages function is disabled, you'll notice that when you close a game or a program, the free amount of RAM changes almost instantly, giving you a more precise information and control over what actually is using your memory.
 


I saw dos2unix mentioning a few useful things I already know but he forgot to mention one very annoying function in the linux kernel that is supposed to be a benefit but in fact it does the opposite - unloading free RAM. For instance, you've played a game that uses 16 GiB RAM, your distro normally uses 4 GiB and that's 20 GiB RAM. But when you close the game, the used RAM remains at 20 GiB. Why? Because of a function called "huge pages". This appeared somewhere in the kernel 6.x versions and its main purpose remains a mystery to me bc it creates more problems than it fixes.
It would be interesting to see some data from the system to clarify what is actually happening.

Huge pages just use larger memory page sizes above the default of 4KB. It means basically that the cpu gets an advantage by using fewer pages so it has fewer physical addresses in memory to deal with. Fewer operations make it more efficient.

The reason for wanting to look a little more at this is because the linux memory system is known to be very efficient in general, and that in particular, the kernel will free, or reclaim memory that is no longer in use. Unfortunately, some of the memory accounting tools may still show that memory is occupied when in reality, the kernel will use that memory for any future use that needs to be made of it. Tools like free and htop may show high memory usages that the kernel will however, reallocate to new processes on demand. Thus, relying on the outputs of those tools alone could be misleading.

What one would need to show to suggest that huge pages are hogging the memory, is when trying to run an application which consumes a like amount of memory as the original memory using application, such as a similar game, and finding it cannot run because of the lack of memory. This may actually be the case for the system mentioned in post #1, but it's not clear that it is. Of course the original game would need to have been completely stopped and exited.

One way to check whether huge pages have actually reserved memory which is hogged until released, is to inspect
/proc/meminfo:
Code:
[~]$ grep -i huge /proc/meminfo
AnonHugePages:     73728 kB
ShmemHugePages:        0 kB
FileHugePages:     51200 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
Hugetlb:               0 kB
The output here shows that no huge pages have been reserved since the Hugetlb is 0. It means that, though plenty of huge pages are in use, none are reserved. In this case the kernel will behave as expected and reclaim memory on demand. The Hugetlb value is a measure of memory carved out of normal memory management AIUI.
 
Last edited:


Follow Linux.org

Members online


Top