4

In the case that memory is allocated and its known that it (almost certainly / probably) won't be used for a long time, it could be useful to tag this memory to be more aggressively moved into swap-space.

Is there some command to tell the kernel of this?

Failing that, it may be better to dump these out to temp files, but I was curious about the ability to send-to-swap (or something similar).


Of course if there is no swap-space, this would do nothing, and in that case writing temp files may be better.

ideasman42
  • 42,413
  • 44
  • 197
  • 320
  • If you *know* a part of the memory isn't used, why not write it to a file? What are the advantages of swapping you're interested in? – Reut Sharabani Jun 22 '14 at 04:16
  • Is this really necessary? If the memory is allocated, but not accessed, will there really be any pages (primary or swap) allocated? It is my understanding that there won't, with the normal Linux memory overcommit. If you are asking about memory where you write data, and then don't read it for a long time, it would be different. – Thomas Padron-McCarthy Jun 22 '14 at 04:17
  • 2
    @Reut Sharabani, Its not an 100% certainty the memory wont be used again, But the chances of it being used again soon is very low. (Edited the question to make that clear) – ideasman42 Jun 22 '14 at 05:12
  • `madvise(base, length, MADV_COLD);` or `madvise(base, length, MADV_PAGEOUT);` might be the syscall you're looking for. Check the return value to see if the value was supported by the current kernel. – Mikko Rantalainen Jul 21 '22 at 18:39

2 Answers2

7

You can use the madvise call to tell the kernel what you will likely be doing with the memory in the future. For example:

madvise(base, length, MADV_SOFT_OFFLINE);

tells the kernel that you wont need the memory in quesion any time soon, so it can be flushed to backing store (or just dropped if it was mapped from a file and is unchanged).

There's also MADV_DONTNEED which allows the kernel to drop the contents even if modified (so when you next access the memory, if you do, it might be zeroed or reread from the original mapped file).

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
  • Note, I looked into using this, and after hunting around found I had to include both `` and ``. – ideasman42 Jun 22 '14 at 09:25
  • If you have the relevant manpages installed, you can always just run `$man madvise` (or whatever the name of the C function is) to go to the manpage which will also tell you what you need to include. If you instead get the man page for a program instead of the function, just explicitly tell `man` that you want to see the manpage for a syscall (e.g. `$man 2 madvise`) or a library call (e.g. `$man 3 printf`) – user986730 Nov 17 '17 at 16:44
  • The manpage says *This feature is intended for testing of memory error-handling code; it is available only if the kernel was configured with CONFIG_MEMORY_FAILURE.* so it does not seem like something one could use in regular applications. – the8472 Jun 22 '18 at 01:12
  • According to https://man7.org/linux/man-pages/man2/madvise.2.html Linux 5.4 and greater should support MADV_COLD and MADV_PAGEOUT. The MADV_COLD tells kernel that the memory area should be considered inactive and MADV_PAGEOUT is basically the same but kernel is expected to immediately start swapping the area out. These should not require CONFIG_MEMORY_FAILURE to work but you need Linux kernel version 5.4 or newer. I think you can blindly try to set these and check the return value to see if the kernel supported the value regardless of kernel version. – Mikko Rantalainen Jul 21 '22 at 18:36
0

The closest thing I can think of would be mmap see: Memory-mapped I/O. This does not write to the linux swap partition, but allows for paging (complete pages of memory) to disk for access. Temporary files and directories are also available with tempfile, mkstemp and mkdtemp, but again, this does not write to the swap partition, but instead it occurs on the normal filesystem.

Other than features similar to the above, I do not believe there is anything that allows direct access to the swap partition (other than exhausting system memory).

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85