-1

Currently I have an issue with programs not de-allocating their stack upon close. It appears to be a natural behaviour of Linux page caching.

When I open my desktop, it runs at ~300-500MB. After closing a browser to open perhaps another RAM intensive program like Ardour, I notice that the used RAM drops down but the allocated RAM remains largely the same or at best drops to around 2.2GB.


The same issue is discussed on this thread. The solution I found there was to run the following command which clears PageCache only:

sync; echo 1 > /proc/sys/vm/drop_caches

The following clears dentries and inodes:

sync; echo 2 > /proc/sys/vm/drop_caches

The following clears both PageCace AND dentries and inodes:

sync; echo 3 > /proc/sys/vm/drop_caches

However, I don't want to run this every time. I want to have my workspace free to utilize every bit of RAM between programs.

Is there a way to configure PageCache on a system level or do I need to do something janky like running a service with some kind of listener which then triggers the above command on program close?


See the following example, whilst Brave is open:

picture of htop whilst browser is open

And another after closing

picture of htop after closing browser

Here is the RAM on boot:

htop on boot

System info:

kernel: 5.10.56-1-lts

Flava: Arch

Desktop: i3-gaps

Programs tested: Brave, Chromium, Firefox, Ardour, Gimp, Inkscape

6
  • Why do you think that having caches is stopping your workspace from being able to use every bit of ram? The ram chips are always there and powered on. They have some values in them, might be all zeros or all ones or random. All that saying they are part of the pagecache means is that they are copies of disk blocks, so if you want that disk block it is already available in memory. Even with traditional SSDs this is thousands of times faster than reading from SSD. If your program wants some memory then using pagecache memory is fast. The cache clearing commands exist for benchmarking purposes.
    – icarus
    Aug 7, 2021 at 17:23
  • OK. Edit the question to make it clear what problem you are having. Aug 8, 2021 at 13:01
  • "Is there a way to configure PageCache on a system level or do I need to do something janky like running a service with some kind of listener which then triggers the above command on program close?" this was the question.
    – BitShift
    Aug 8, 2021 at 13:49
  • @icarus because it is... unused RAM might be "wasted RAM" but stealing all my RAM so that it can't be used by another program when the first one is shut down is equally wasteful. It's faulty reasoning to say that optimizing RAM this way is suitable for all use cases, when it's clearly meant for performance, storage, or speed enhancements and not ideal for hardware constrained systems. Like, we get it: cache memory is fast and that's fantastic. Right now I don't care about fast and I don't care about storage degradation.
    – BitShift
    Aug 8, 2021 at 15:13
  • 1
    Let me try again. You have a certain amount of ram chips which are powered on. The programs you are currently running need a certain amount, let us pick a figure of 40% of it. What do you want to be stored in the remaining 60%? You appear to want the answer to be "Nothing", so memory is available for your next program. The answer from the rest of the world is "Anything that might be useful as we can't power it off, just as long as it doesn't impact us using it for our next program if we need it".
    – icarus
    Aug 8, 2021 at 17:26

3 Answers 3

2

Programs aren’t “not de-allocating their stack upon close”. What you’re seeing is on-disk data being cached in RAM, and that’s normal — as explained in the answers to the question you linked to,

Unused RAM is wasted RAM.

You shouldn’t try to “fix it” by dropping the caches. If memory becomes scarce, the kernel will automatically reduce the amount of cached data as necessary; until then, having data in cache reduces the time it takes to access it.

To see this in action, try starting Brave, stopping it, then starting it again; now stop it, drop the caches, and start it again, and see how much longer it takes.

There is no way to empty the page cache automatically when a program exits; for one thing, the contents of the page cache aren’t tied to a given process. You could limit the amount of page cache used by a given process, by placing it in a cgroup and limiting the amount of memory the cgroup can use — this includes the page cache.

2
  • Whilst I appreciate your answer as "the proper way to do things" it wasn't the question asked. By the by, it doesn't de-allocate memory as and when is needed and I often have issues with a frozen system. As mentioned in another comment, I should have the freedom to configure this system feature to optimize for my hardware and workflow.
    – BitShift
    Aug 8, 2021 at 12:16
  • You’re right, I didn’t answer the question; I’ve updated my post to do so. Aug 8, 2021 at 17:58
1

What you are asking is for the computer to forget stuff now, just incase it needs to remember somethig later. It will forget cached data (stuf that it can get back from disk), when it needs to, but not earlier. Forgetting early is wast. Forgetting is cheep so can be done at the last possible moment.

You are missunderstanding the data, do not consider cache and buffer memory as "in use".

1
  • I understand it perfectly well and I agree with the design decision in certain circumstances, but I don't agree this is something that should be outside the configuration desires of the user. I should be able to decide when and where caching is used to optimize for my hardware and workflow. Also not the question asked.
    – BitShift
    Aug 8, 2021 at 12:17
-2

For anyone else who finds this question, the answer appears to be configuring hdparm and find the line that says write_cache = off and uncomment. Although I haven't tested yet and will need to return with results at a later date.

As you may have seen in other technically correct answers, write-read caching is there as a default for a reason and it suits most use cases. Messing with how Linux manages memory can cause data loss and premature degradation of storage mediums. Not to mention performance hits that everyone seems so eager to discuss.

For me none of that is a problem, in fact it's desirable to have that kind of disregard for non-critical memory.

2
  • 2
    Turning off the write cache for hard disks has almost nothing to do with the pagecache. It trades disk performance to reliability. For most people having faster disk is worth the very very small risk of corruption if the power fails unexpectedly but it is a trade-off.
    – icarus
    Aug 8, 2021 at 17:28
  • Ah ok, well this sucks. Looks like I've reached my limits with Linux. Time for a new daily driver.
    – BitShift
    Aug 9, 2021 at 13:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .