Why Linux does not clean up disk caches and swap automatically?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
-1
down vote

favorite












For example, when I archive a few gigs of files (using tar), Linux uses quite a lot of disk caching (and some swap) but never cleans it up when the operation has completed. As a result, because there's no free memory Linux will try to swap out something from memory which in its turn creates an additional load on CPU.



Of course, I can clean up caches by running echo 1 > /proc/sys/vm/drop_caches but isn't that stupid that I have to do that?



Even worse with swap, there's no command to clean up unused swap, I have to disable/enable it completely which I don't think is a safe thing to do at all.



UPD:



I've run a few tests and found out a few things:



  1. The swapped out memory pages during the archive command not related to archived files, it seems it's just a usual swapping out process caused by decreased free memory (because disk caching ate it all) according to swappiness


  2. Running swapoff -a is actually safe, meaning swapped pages will move back to memory


My current solution is to limit archive command memory usage via cgroups (I run docker container with -m flag). If you don't use docker, there's a project https://github.com/Feh/nocache that might help.



The remaining question is when will Linux clean up disk caching and will it at all? If not, is it a good practice to manually clean up disk cache (echo 1 > /proc/sys/vm/drop_caches)?







share|improve this question

















  • 1




    Note the CPU time used simply by swapping is normally insignificant; if there's a slowdown the normal problem is the disk time.
    – sourcejedi
    Jun 22 at 10:41











  • @sourcejedi thanks, I wasn't aware about swap cache
    – csandanov
    Jun 22 at 11:58










  • Nice hack with docker! If you don't specifically want to run inside docker, you could also use a cgroup manually. systemd-run should be able to apply systemd.resource-control properties to any command (these are also implemented using cgroups). As a non-root user, you can use systemd-run --user
    – sourcejedi
    Jun 22 at 12:18















up vote
-1
down vote

favorite












For example, when I archive a few gigs of files (using tar), Linux uses quite a lot of disk caching (and some swap) but never cleans it up when the operation has completed. As a result, because there's no free memory Linux will try to swap out something from memory which in its turn creates an additional load on CPU.



Of course, I can clean up caches by running echo 1 > /proc/sys/vm/drop_caches but isn't that stupid that I have to do that?



Even worse with swap, there's no command to clean up unused swap, I have to disable/enable it completely which I don't think is a safe thing to do at all.



UPD:



I've run a few tests and found out a few things:



  1. The swapped out memory pages during the archive command not related to archived files, it seems it's just a usual swapping out process caused by decreased free memory (because disk caching ate it all) according to swappiness


  2. Running swapoff -a is actually safe, meaning swapped pages will move back to memory


My current solution is to limit archive command memory usage via cgroups (I run docker container with -m flag). If you don't use docker, there's a project https://github.com/Feh/nocache that might help.



The remaining question is when will Linux clean up disk caching and will it at all? If not, is it a good practice to manually clean up disk cache (echo 1 > /proc/sys/vm/drop_caches)?







share|improve this question

















  • 1




    Note the CPU time used simply by swapping is normally insignificant; if there's a slowdown the normal problem is the disk time.
    – sourcejedi
    Jun 22 at 10:41











  • @sourcejedi thanks, I wasn't aware about swap cache
    – csandanov
    Jun 22 at 11:58










  • Nice hack with docker! If you don't specifically want to run inside docker, you could also use a cgroup manually. systemd-run should be able to apply systemd.resource-control properties to any command (these are also implemented using cgroups). As a non-root user, you can use systemd-run --user
    – sourcejedi
    Jun 22 at 12:18













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











For example, when I archive a few gigs of files (using tar), Linux uses quite a lot of disk caching (and some swap) but never cleans it up when the operation has completed. As a result, because there's no free memory Linux will try to swap out something from memory which in its turn creates an additional load on CPU.



Of course, I can clean up caches by running echo 1 > /proc/sys/vm/drop_caches but isn't that stupid that I have to do that?



Even worse with swap, there's no command to clean up unused swap, I have to disable/enable it completely which I don't think is a safe thing to do at all.



UPD:



I've run a few tests and found out a few things:



  1. The swapped out memory pages during the archive command not related to archived files, it seems it's just a usual swapping out process caused by decreased free memory (because disk caching ate it all) according to swappiness


  2. Running swapoff -a is actually safe, meaning swapped pages will move back to memory


My current solution is to limit archive command memory usage via cgroups (I run docker container with -m flag). If you don't use docker, there's a project https://github.com/Feh/nocache that might help.



The remaining question is when will Linux clean up disk caching and will it at all? If not, is it a good practice to manually clean up disk cache (echo 1 > /proc/sys/vm/drop_caches)?







share|improve this question













For example, when I archive a few gigs of files (using tar), Linux uses quite a lot of disk caching (and some swap) but never cleans it up when the operation has completed. As a result, because there's no free memory Linux will try to swap out something from memory which in its turn creates an additional load on CPU.



Of course, I can clean up caches by running echo 1 > /proc/sys/vm/drop_caches but isn't that stupid that I have to do that?



Even worse with swap, there's no command to clean up unused swap, I have to disable/enable it completely which I don't think is a safe thing to do at all.



UPD:



I've run a few tests and found out a few things:



  1. The swapped out memory pages during the archive command not related to archived files, it seems it's just a usual swapping out process caused by decreased free memory (because disk caching ate it all) according to swappiness


  2. Running swapoff -a is actually safe, meaning swapped pages will move back to memory


My current solution is to limit archive command memory usage via cgroups (I run docker container with -m flag). If you don't use docker, there's a project https://github.com/Feh/nocache that might help.



The remaining question is when will Linux clean up disk caching and will it at all? If not, is it a good practice to manually clean up disk cache (echo 1 > /proc/sys/vm/drop_caches)?









share|improve this question












share|improve this question




share|improve this question








edited Jun 22 at 11:56
























asked Jun 22 at 10:17









csandanov

113




113







  • 1




    Note the CPU time used simply by swapping is normally insignificant; if there's a slowdown the normal problem is the disk time.
    – sourcejedi
    Jun 22 at 10:41











  • @sourcejedi thanks, I wasn't aware about swap cache
    – csandanov
    Jun 22 at 11:58










  • Nice hack with docker! If you don't specifically want to run inside docker, you could also use a cgroup manually. systemd-run should be able to apply systemd.resource-control properties to any command (these are also implemented using cgroups). As a non-root user, you can use systemd-run --user
    – sourcejedi
    Jun 22 at 12:18













  • 1




    Note the CPU time used simply by swapping is normally insignificant; if there's a slowdown the normal problem is the disk time.
    – sourcejedi
    Jun 22 at 10:41











  • @sourcejedi thanks, I wasn't aware about swap cache
    – csandanov
    Jun 22 at 11:58










  • Nice hack with docker! If you don't specifically want to run inside docker, you could also use a cgroup manually. systemd-run should be able to apply systemd.resource-control properties to any command (these are also implemented using cgroups). As a non-root user, you can use systemd-run --user
    – sourcejedi
    Jun 22 at 12:18








1




1




Note the CPU time used simply by swapping is normally insignificant; if there's a slowdown the normal problem is the disk time.
– sourcejedi
Jun 22 at 10:41





Note the CPU time used simply by swapping is normally insignificant; if there's a slowdown the normal problem is the disk time.
– sourcejedi
Jun 22 at 10:41













@sourcejedi thanks, I wasn't aware about swap cache
– csandanov
Jun 22 at 11:58




@sourcejedi thanks, I wasn't aware about swap cache
– csandanov
Jun 22 at 11:58












Nice hack with docker! If you don't specifically want to run inside docker, you could also use a cgroup manually. systemd-run should be able to apply systemd.resource-control properties to any command (these are also implemented using cgroups). As a non-root user, you can use systemd-run --user
– sourcejedi
Jun 22 at 12:18





Nice hack with docker! If you don't specifically want to run inside docker, you could also use a cgroup manually. systemd-run should be able to apply systemd.resource-control properties to any command (these are also implemented using cgroups). As a non-root user, you can use systemd-run --user
– sourcejedi
Jun 22 at 12:18











2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










(Nitpick: the CPU time used simply by swapping is normally insignificant. When the system is slow to respond during swapping, the normal problem is the disk time).




Even worse with swap, there's no command to clean up unused swap




Swap space is reclaimed on demand. But when a swap page is read back in, it is not specifically erased, and it is still kept track of - this means, if the page is to be swapped out again without it having been changed in between, the page does not need to be written again.



http://linux-tutorial.info/modules.php?name=MContent&pageid=314




The remaining question is when will Linux clean up disk caching and will it at all? If not, is it a good practice to manually clean up disk cache (echo 1 > /proc/sys/vm/drop_caches)?




It will clean up disk cache on demand only. I.e. inactive disk cache pages will be evicted when memory is needed. AIUI, vm.swappiness allows setting some bias between reclaiming inactive file cache, and reclaiming inactive "anonymous" (swap-backed) program memory, with the default being biased against swapping. (Make or force tmpfs to swap before the file cache)



There is no general recommendation to run drop_caches; it is more of a debugging tool. The official documentation of it is




This file is not a means to control the growth of the various kernel caches
(inodes, dentries, pagecache, etc...) These objects are automatically
reclaimed by the kernel when memory is needed elsewhere on the system.



Use of this file can cause performance problems. Since it discards cached
objects, it may cost a significant amount of I/O and CPU to recreate the
dropped objects, especially if they were under heavy use. Because of this,
use outside of a testing or debugging environment is not recommended.







share|improve this answer






























    up vote
    1
    down vote













    This is the expected behavior. You can adjust swap ussage using the vm.swappines sysctl and tune it according to your needs.






    share|improve this answer

















    • 1




      Also, notice that we WANT to use memory. Do not be fooled by the "Free memory" count. That is memory the system is not using and is wasted. You do want to use memory and keep it full of hot objects ready to be retrieved.
      – Dolapevich
      Jun 24 at 17:32










    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f451263%2fwhy-linux-does-not-clean-up-disk-caches-and-swap-automatically%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    (Nitpick: the CPU time used simply by swapping is normally insignificant. When the system is slow to respond during swapping, the normal problem is the disk time).




    Even worse with swap, there's no command to clean up unused swap




    Swap space is reclaimed on demand. But when a swap page is read back in, it is not specifically erased, and it is still kept track of - this means, if the page is to be swapped out again without it having been changed in between, the page does not need to be written again.



    http://linux-tutorial.info/modules.php?name=MContent&pageid=314




    The remaining question is when will Linux clean up disk caching and will it at all? If not, is it a good practice to manually clean up disk cache (echo 1 > /proc/sys/vm/drop_caches)?




    It will clean up disk cache on demand only. I.e. inactive disk cache pages will be evicted when memory is needed. AIUI, vm.swappiness allows setting some bias between reclaiming inactive file cache, and reclaiming inactive "anonymous" (swap-backed) program memory, with the default being biased against swapping. (Make or force tmpfs to swap before the file cache)



    There is no general recommendation to run drop_caches; it is more of a debugging tool. The official documentation of it is




    This file is not a means to control the growth of the various kernel caches
    (inodes, dentries, pagecache, etc...) These objects are automatically
    reclaimed by the kernel when memory is needed elsewhere on the system.



    Use of this file can cause performance problems. Since it discards cached
    objects, it may cost a significant amount of I/O and CPU to recreate the
    dropped objects, especially if they were under heavy use. Because of this,
    use outside of a testing or debugging environment is not recommended.







    share|improve this answer



























      up vote
      1
      down vote



      accepted










      (Nitpick: the CPU time used simply by swapping is normally insignificant. When the system is slow to respond during swapping, the normal problem is the disk time).




      Even worse with swap, there's no command to clean up unused swap




      Swap space is reclaimed on demand. But when a swap page is read back in, it is not specifically erased, and it is still kept track of - this means, if the page is to be swapped out again without it having been changed in between, the page does not need to be written again.



      http://linux-tutorial.info/modules.php?name=MContent&pageid=314




      The remaining question is when will Linux clean up disk caching and will it at all? If not, is it a good practice to manually clean up disk cache (echo 1 > /proc/sys/vm/drop_caches)?




      It will clean up disk cache on demand only. I.e. inactive disk cache pages will be evicted when memory is needed. AIUI, vm.swappiness allows setting some bias between reclaiming inactive file cache, and reclaiming inactive "anonymous" (swap-backed) program memory, with the default being biased against swapping. (Make or force tmpfs to swap before the file cache)



      There is no general recommendation to run drop_caches; it is more of a debugging tool. The official documentation of it is




      This file is not a means to control the growth of the various kernel caches
      (inodes, dentries, pagecache, etc...) These objects are automatically
      reclaimed by the kernel when memory is needed elsewhere on the system.



      Use of this file can cause performance problems. Since it discards cached
      objects, it may cost a significant amount of I/O and CPU to recreate the
      dropped objects, especially if they were under heavy use. Because of this,
      use outside of a testing or debugging environment is not recommended.







      share|improve this answer

























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        (Nitpick: the CPU time used simply by swapping is normally insignificant. When the system is slow to respond during swapping, the normal problem is the disk time).




        Even worse with swap, there's no command to clean up unused swap




        Swap space is reclaimed on demand. But when a swap page is read back in, it is not specifically erased, and it is still kept track of - this means, if the page is to be swapped out again without it having been changed in between, the page does not need to be written again.



        http://linux-tutorial.info/modules.php?name=MContent&pageid=314




        The remaining question is when will Linux clean up disk caching and will it at all? If not, is it a good practice to manually clean up disk cache (echo 1 > /proc/sys/vm/drop_caches)?




        It will clean up disk cache on demand only. I.e. inactive disk cache pages will be evicted when memory is needed. AIUI, vm.swappiness allows setting some bias between reclaiming inactive file cache, and reclaiming inactive "anonymous" (swap-backed) program memory, with the default being biased against swapping. (Make or force tmpfs to swap before the file cache)



        There is no general recommendation to run drop_caches; it is more of a debugging tool. The official documentation of it is




        This file is not a means to control the growth of the various kernel caches
        (inodes, dentries, pagecache, etc...) These objects are automatically
        reclaimed by the kernel when memory is needed elsewhere on the system.



        Use of this file can cause performance problems. Since it discards cached
        objects, it may cost a significant amount of I/O and CPU to recreate the
        dropped objects, especially if they were under heavy use. Because of this,
        use outside of a testing or debugging environment is not recommended.







        share|improve this answer















        (Nitpick: the CPU time used simply by swapping is normally insignificant. When the system is slow to respond during swapping, the normal problem is the disk time).




        Even worse with swap, there's no command to clean up unused swap




        Swap space is reclaimed on demand. But when a swap page is read back in, it is not specifically erased, and it is still kept track of - this means, if the page is to be swapped out again without it having been changed in between, the page does not need to be written again.



        http://linux-tutorial.info/modules.php?name=MContent&pageid=314




        The remaining question is when will Linux clean up disk caching and will it at all? If not, is it a good practice to manually clean up disk cache (echo 1 > /proc/sys/vm/drop_caches)?




        It will clean up disk cache on demand only. I.e. inactive disk cache pages will be evicted when memory is needed. AIUI, vm.swappiness allows setting some bias between reclaiming inactive file cache, and reclaiming inactive "anonymous" (swap-backed) program memory, with the default being biased against swapping. (Make or force tmpfs to swap before the file cache)



        There is no general recommendation to run drop_caches; it is more of a debugging tool. The official documentation of it is




        This file is not a means to control the growth of the various kernel caches
        (inodes, dentries, pagecache, etc...) These objects are automatically
        reclaimed by the kernel when memory is needed elsewhere on the system.



        Use of this file can cause performance problems. Since it discards cached
        objects, it may cost a significant amount of I/O and CPU to recreate the
        dropped objects, especially if they were under heavy use. Because of this,
        use outside of a testing or debugging environment is not recommended.








        share|improve this answer















        share|improve this answer



        share|improve this answer








        edited Jun 24 at 17:41


























        answered Jun 22 at 12:12









        sourcejedi

        18.1k22375




        18.1k22375






















            up vote
            1
            down vote













            This is the expected behavior. You can adjust swap ussage using the vm.swappines sysctl and tune it according to your needs.






            share|improve this answer

















            • 1




              Also, notice that we WANT to use memory. Do not be fooled by the "Free memory" count. That is memory the system is not using and is wasted. You do want to use memory and keep it full of hot objects ready to be retrieved.
              – Dolapevich
              Jun 24 at 17:32














            up vote
            1
            down vote













            This is the expected behavior. You can adjust swap ussage using the vm.swappines sysctl and tune it according to your needs.






            share|improve this answer

















            • 1




              Also, notice that we WANT to use memory. Do not be fooled by the "Free memory" count. That is memory the system is not using and is wasted. You do want to use memory and keep it full of hot objects ready to be retrieved.
              – Dolapevich
              Jun 24 at 17:32












            up vote
            1
            down vote










            up vote
            1
            down vote









            This is the expected behavior. You can adjust swap ussage using the vm.swappines sysctl and tune it according to your needs.






            share|improve this answer













            This is the expected behavior. You can adjust swap ussage using the vm.swappines sysctl and tune it according to your needs.







            share|improve this answer













            share|improve this answer



            share|improve this answer











            answered Jun 22 at 11:37









            Dolapevich

            144




            144







            • 1




              Also, notice that we WANT to use memory. Do not be fooled by the "Free memory" count. That is memory the system is not using and is wasted. You do want to use memory and keep it full of hot objects ready to be retrieved.
              – Dolapevich
              Jun 24 at 17:32












            • 1




              Also, notice that we WANT to use memory. Do not be fooled by the "Free memory" count. That is memory the system is not using and is wasted. You do want to use memory and keep it full of hot objects ready to be retrieved.
              – Dolapevich
              Jun 24 at 17:32







            1




            1




            Also, notice that we WANT to use memory. Do not be fooled by the "Free memory" count. That is memory the system is not using and is wasted. You do want to use memory and keep it full of hot objects ready to be retrieved.
            – Dolapevich
            Jun 24 at 17:32




            Also, notice that we WANT to use memory. Do not be fooled by the "Free memory" count. That is memory the system is not using and is wasted. You do want to use memory and keep it full of hot objects ready to be retrieved.
            – Dolapevich
            Jun 24 at 17:32












             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f451263%2fwhy-linux-does-not-clean-up-disk-caches-and-swap-automatically%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Peggy Mitchell

            Palaiologos

            The Forum (Inglewood, California)