Gentoo Archives: gentoo-user

From: Rich Freeman <rich0@g.o>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] Limiting amount of memory a program can use.
Date: Sun, 28 Aug 2022 21:13:13
Message-Id: CAGfcS_=-UHNkPMm0vQH2tb877YzO76x+ubtkDKP6yqCF+aLmRA@mail.gmail.com
In Reply to: Re: [gentoo-user] Limiting amount of memory a program can use. by Wols Lists
1 On Sun, Aug 28, 2022 at 11:32 AM Wols Lists <antlists@××××××××××××.uk> wrote:
2 >
3 > On 28/08/2022 15:21, Rich Freeman wrote:
4 > > Something I wish linux supported was discardable memory, for
5 > > caches/etc. A program should be able to allocate memory while passing
6 > > a hint to the kernel saying that the memory is discardable.
7 >
8 > Linux DOES have that ...
9 >
10 > I'm not sure how to use it, but you can pass a flag to open(), which
11 > says "do not cache this file".
12
13 That isn't what I was proposing. That has been around for a while.
14 However, it only impacts caching/buffering of open files, which is
15 kernel memory, and not memory held by a process itself.
16
17 You wouldn't want to limit kernel file caching of a torrent file,
18 since those files are pretty ideal candidates for caching (since the
19 client is likely to send data not long after receiving it). There
20 isn't really any benefit to this either in this case as the kernel
21 already automatically drops file cache memory as needed under memory
22 pressure.
23
24 I was referring to application caching, which is internal storage used
25 by an application that may or may not have anything to do with open
26 files. A browser cache is a good example of something like this
27 (though that extends to disk caching often and those files would also
28 get cached by the kernel). The kernel can't simply drop this memory
29 when it needs memory since there is no way for it to tell which memory
30 an application owns is used for this purpose, and no way currently to
31 handle the resulting segfaults when the application goes to read that
32 memory. Reading memory is a CPU instruction, not a system call, so it
33 would trigger an exception at the CPU level which must be handled.
34
35 I'm sure something could be engineered but it is more complex and I'm
36 guessing nobody has seen the need for it yet. Ideally you'd also have
37 ways for the OS to hint how much memory applications ought to consume
38 in this way. After all, you might have two applications running at
39 the same time, which both want to use lots of extra RAM for caching if
40 it is available, but neither has any way of knowing that this
41 situation exists and it is undesirable if all the memory goes to the
42 first application that asks, and also undesirable if after everything
43 is done grabbing memory if there is a bunch of it sitting around doing
44 nothing.
45
46 TL;DR: cache is not necessarily the same as kernel file cache.
47
48 --
49 Rich