5

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.

4
  • 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? Commented Jun 22, 2014 at 4: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. Commented Jun 22, 2014 at 4: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) Commented Jun 22, 2014 at 5: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. Commented Jul 21, 2022 at 18:39

2 Answers 2

8

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).

Sign up to request clarification or add additional context in comments.

4 Comments

Note, I looked into using this, and after hunting around found I had to include both <sys/mman.h> and <linux/mman.h>.
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)
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.
According to 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.
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).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.