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

Clash 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:
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
Running
swapoff -ais 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)?
linux memory swap disk caching
add a comment |Â
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:
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
Running
swapoff -ais 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)?
linux memory swap disk caching
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-runshould be able to applysystemd.resource-controlproperties to any command (these are also implemented using cgroups). As a non-root user, you can usesystemd-run --user
â sourcejedi
Jun 22 at 12:18
add a comment |Â
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:
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
Running
swapoff -ais 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)?
linux memory swap disk caching
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:
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
Running
swapoff -ais 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)?
linux memory swap disk caching
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-runshould be able to applysystemd.resource-controlproperties to any command (these are also implemented using cgroups). As a non-root user, you can usesystemd-run --user
â sourcejedi
Jun 22 at 12:18
add a comment |Â
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-runshould be able to applysystemd.resource-controlproperties to any command (these are also implemented using cgroups). As a non-root user, you can usesystemd-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
add a comment |Â
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.
add a comment |Â
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.
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
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
(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.
edited Jun 24 at 17:41
answered Jun 22 at 12:12
sourcejedi
18.1k22375
18.1k22375
add a comment |Â
add a comment |Â
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.
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
add a comment |Â
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.
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
add a comment |Â
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.
This is the expected behavior. You can adjust swap ussage using the vm.swappines sysctl and tune it according to your needs.
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
add a comment |Â
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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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-runshould be able to applysystemd.resource-controlproperties to any command (these are also implemented using cgroups). As a non-root user, you can usesystemd-run --userâ sourcejedi
Jun 22 at 12:18