How much swap should I take for 1GB to 8TB of RAM on 14.04 or higher?












8














I'm going to install 64 bit 14.04 on a new machine with something like 1 to maybe 128GB RAM and even more RAM later. How much space should I allocate to the swap partition?



And especially: WHY those recommendations for these numbers?



(None of the existing answers here and here explain any of the why and are a bit dated if we look at 1GB or RAM and up)










share|improve this question




















  • 6




    Possible duplicate of I have 16GB RAM. Do I need 32GB swap? There are answers from 2017 (or updated in 2017), so it holds currently more recent recommendations than this one.
    – Melebius
    Sep 4 at 6:05








  • 1




    @Melebius: I disagree: Although this question is newer, it is more generic than the one you're trying to duplicate to. This Q&A encompasses the older one, but the reverse is not true! ;-)
    – Fabby
    Sep 4 at 7:35












  • @Fabby In that case, shouldn’t the other be marked as a duplicate of this one?
    – Melebius
    Sep 5 at 6:39






  • 1




    @Melebius If both questions could be used by reviewers as duplicate questions of other questions in different situations, then both questions could be kept open to handle these different situations since both questions have highly upvoted answers.
    – karel
    Sep 5 at 8:11








  • 2




    @Melebius We've had a discussion on this topic in meta already and the community decided to let each stand as is.
    – Fabby
    Sep 5 at 11:39
















8














I'm going to install 64 bit 14.04 on a new machine with something like 1 to maybe 128GB RAM and even more RAM later. How much space should I allocate to the swap partition?



And especially: WHY those recommendations for these numbers?



(None of the existing answers here and here explain any of the why and are a bit dated if we look at 1GB or RAM and up)










share|improve this question




















  • 6




    Possible duplicate of I have 16GB RAM. Do I need 32GB swap? There are answers from 2017 (or updated in 2017), so it holds currently more recent recommendations than this one.
    – Melebius
    Sep 4 at 6:05








  • 1




    @Melebius: I disagree: Although this question is newer, it is more generic than the one you're trying to duplicate to. This Q&A encompasses the older one, but the reverse is not true! ;-)
    – Fabby
    Sep 4 at 7:35












  • @Fabby In that case, shouldn’t the other be marked as a duplicate of this one?
    – Melebius
    Sep 5 at 6:39






  • 1




    @Melebius If both questions could be used by reviewers as duplicate questions of other questions in different situations, then both questions could be kept open to handle these different situations since both questions have highly upvoted answers.
    – karel
    Sep 5 at 8:11








  • 2




    @Melebius We've had a discussion on this topic in meta already and the community decided to let each stand as is.
    – Fabby
    Sep 5 at 11:39














8












8








8


8





I'm going to install 64 bit 14.04 on a new machine with something like 1 to maybe 128GB RAM and even more RAM later. How much space should I allocate to the swap partition?



And especially: WHY those recommendations for these numbers?



(None of the existing answers here and here explain any of the why and are a bit dated if we look at 1GB or RAM and up)










share|improve this question















I'm going to install 64 bit 14.04 on a new machine with something like 1 to maybe 128GB RAM and even more RAM later. How much space should I allocate to the swap partition?



And especially: WHY those recommendations for these numbers?



(None of the existing answers here and here explain any of the why and are a bit dated if we look at 1GB or RAM and up)







partitioning swap






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 20 at 19:06









Fabby

26.4k1360159




26.4k1360159










asked Mar 8 '15 at 1:48









Tim

7,94242103172




7,94242103172








  • 6




    Possible duplicate of I have 16GB RAM. Do I need 32GB swap? There are answers from 2017 (or updated in 2017), so it holds currently more recent recommendations than this one.
    – Melebius
    Sep 4 at 6:05








  • 1




    @Melebius: I disagree: Although this question is newer, it is more generic than the one you're trying to duplicate to. This Q&A encompasses the older one, but the reverse is not true! ;-)
    – Fabby
    Sep 4 at 7:35












  • @Fabby In that case, shouldn’t the other be marked as a duplicate of this one?
    – Melebius
    Sep 5 at 6:39






  • 1




    @Melebius If both questions could be used by reviewers as duplicate questions of other questions in different situations, then both questions could be kept open to handle these different situations since both questions have highly upvoted answers.
    – karel
    Sep 5 at 8:11








  • 2




    @Melebius We've had a discussion on this topic in meta already and the community decided to let each stand as is.
    – Fabby
    Sep 5 at 11:39














  • 6




    Possible duplicate of I have 16GB RAM. Do I need 32GB swap? There are answers from 2017 (or updated in 2017), so it holds currently more recent recommendations than this one.
    – Melebius
    Sep 4 at 6:05








  • 1




    @Melebius: I disagree: Although this question is newer, it is more generic than the one you're trying to duplicate to. This Q&A encompasses the older one, but the reverse is not true! ;-)
    – Fabby
    Sep 4 at 7:35












  • @Fabby In that case, shouldn’t the other be marked as a duplicate of this one?
    – Melebius
    Sep 5 at 6:39






  • 1




    @Melebius If both questions could be used by reviewers as duplicate questions of other questions in different situations, then both questions could be kept open to handle these different situations since both questions have highly upvoted answers.
    – karel
    Sep 5 at 8:11








  • 2




    @Melebius We've had a discussion on this topic in meta already and the community decided to let each stand as is.
    – Fabby
    Sep 5 at 11:39








6




6




Possible duplicate of I have 16GB RAM. Do I need 32GB swap? There are answers from 2017 (or updated in 2017), so it holds currently more recent recommendations than this one.
– Melebius
Sep 4 at 6:05






Possible duplicate of I have 16GB RAM. Do I need 32GB swap? There are answers from 2017 (or updated in 2017), so it holds currently more recent recommendations than this one.
– Melebius
Sep 4 at 6:05






1




1




@Melebius: I disagree: Although this question is newer, it is more generic than the one you're trying to duplicate to. This Q&A encompasses the older one, but the reverse is not true! ;-)
– Fabby
Sep 4 at 7:35






@Melebius: I disagree: Although this question is newer, it is more generic than the one you're trying to duplicate to. This Q&A encompasses the older one, but the reverse is not true! ;-)
– Fabby
Sep 4 at 7:35














@Fabby In that case, shouldn’t the other be marked as a duplicate of this one?
– Melebius
Sep 5 at 6:39




@Fabby In that case, shouldn’t the other be marked as a duplicate of this one?
– Melebius
Sep 5 at 6:39




1




1




@Melebius If both questions could be used by reviewers as duplicate questions of other questions in different situations, then both questions could be kept open to handle these different situations since both questions have highly upvoted answers.
– karel
Sep 5 at 8:11






@Melebius If both questions could be used by reviewers as duplicate questions of other questions in different situations, then both questions could be kept open to handle these different situations since both questions have highly upvoted answers.
– karel
Sep 5 at 8:11






2




2




@Melebius We've had a discussion on this topic in meta already and the community decided to let each stand as is.
– Fabby
Sep 5 at 11:39




@Melebius We've had a discussion on this topic in meta already and the community decided to let each stand as is.
– Fabby
Sep 5 at 11:39










2 Answers
2






active

oldest

votes


















21














The short answer:



Set your swap file to:





  • round(sqrt(RAM)) if you don't use hibernation


  • RAM+round(sqrt(RAM)) if you do use hibernation


Set your swappiness to 10 on a desktop, but not on a server!



The long answer:



In the past:



The rule of thumb in use for the last 25 years has been a minimum of 1xRAM and maximum 2xRAM so that is what you'll see quoted all the time.



That minimum was set back in the stone age when I was a teenager and dinosaurs still roamed the Earth and because RAM was just too expensive and you absolutely needed that swap space to be able to accomplish anything.



The maximum was set at that time because of diminishing returns: it's just too slow to have to swap so much memory as HDD access is a factor of 1000 slower then RAM: good in an emergency, but not really good for everyday use! At the time, when you ran out of swap space, it was time to add more RAM! (which is still true today).



In the present:




  1. If you do not use hibernation and your memory is in excess of 1GByte the new rule of thumb is round(sqrt(RAM)) where RAM is obviously your RAM size in GB.


  2. If you use hibernation, you need to be able to swap the entire amount of RAM+already swapped RAM to disk, thus the formula becomes: RAM+round(sqrt(RAM))


  3. The rule of diminishing returns still holds today for the maximum, but unless you test your actual usage, taking 2xRAM is just a waste of disk space, so don't use the maximum Unless you run out of swap space using the other methodologies.



All of these together give you the following table: (last 3 columns denoting swap space)



    RAM   No hibernation    With Hibernation    Maximum
1GB 1GB 2GB 2GB
2GB 1GB 3GB 4GB
3GB 2GB 5GB 6GB
4GB 2GB 6GB 8GB
5GB 2GB 7GB 10GB
6GB 2GB 8GB 12GB
8GB 3GB 11GB 16GB
12GB 3GB 15GB 24GB
16GB 4GB 20GB 32GB
24GB 5GB 29GB 48GB
32GB 6GB 38GB 64GB
64GB 8GB 72GB 128GB
128GB 11GB 139GB 256GB
256GB 16GB 272GB 512GB
512GB 23GB 535GB 1TB
1TB 32GB 1056GB 2TB
2TB 46GB 2094GB 4TB
4TB 64GB 4160GB 8TB
8TB 91GB 8283GB 16TB


The above is just a rule of thumb; it's not the law of gravity!

You can break this rule (unlike the law of gravity) if your particular use case is different!



Pro tip: Always allocate SWAP at the start of a HDD as the heads need to move less on the inside of the disk.
Yes: On SSDs, it doesn't really matter any more where you locate the swap area as they use quantum-tunnelling instead of moving heads and modern SSDs use all of their memory cells (even the unallocated space) to prevent quantum degradation.



How to test if your usage of swap is different from the "generic" rule:



Just execute:



for szFile in /proc/*/status ; do 
awk '/VmSwap|Name/{printf $2 "t" $3}END{ print "" }' $szFile
done | sort --key 2 --numeric --reverse | more


which will give you a list of all running programs that are swapped out (with the one using the most swap space on top)



If you're using more then a few KB: resize to more then the minimum, otherwise, don't bother...



If you're on a server, stop reading now: you're all set!





If you're on a desktop/laptop client (not server), you want your GUI to be as responsive as possible and only swap when you really need to. Ubuntu has been optimised to swap early for server use, but on your client you want editing that huge 250 Mega-pixel raw picture in gimp to be speedy, so setting your swappiness to 10 will keep the kernel from swapping too early, while ensuring it doesn't swap too late:



sudo nano /etc/sysctl.conf


and add:



# change "swappiness" from default 60 to 10 
# (theoretically only swap when RAM usage reaches around 80 or 90 percent)
vm.swappiness = 10


to the end of the file, save the file (Ctrl+XY+Enter in nano) and execute a:



sysctl --system


to reload the parameter or take the Window$ approach and reboot... :-)






share|improve this answer



















  • 1




    Thanks. What do you mean by "diminishing return"? If both the RAM and the swap are fully used, then it is impossible to hibernate even if the swap is 2*RAM size, isn't it? (Because hibernation will need the size of the swap to be RAM + swap, which is impossible?)
    – Tim
    Mar 9 '15 at 0:11












  • Yes, but I've never had this happen yet... (and I'm old! therefore 2* RAM: When all of the RAM is swapped, you can still hibernate...
    – Fabby
    Mar 9 '15 at 8:15






  • 2




    "Diminishing returns" just means that HDD access is a factor of 1000 slower then RAM, so if you need to swap out twice your RAM you'll have died of old age before it completes... It's just there to hibernate when you're already swapping...
    – Fabby
    Mar 9 '15 at 8:28






  • 1




    @Fabby It's CV reviewing time on the original question again: askubuntu.com/review/close/889454. See Yufenyuy Veyeh Dider's comment under the question.
    – karel
    Oct 21 at 1:41










  • Voted to keep open and upvoted your comment so the next person in the review queue will see this as well. (and thanks for the heads-up)
    – Fabby
    Oct 21 at 15:27



















5














Necessity for swap



Swap is useful for 3 reasons:





  1. Removing clutter from RAM. From the article on kernel.org:




    "A significant number of the pages referenced by a process early in
    its life may only be used for initialisation and then never used
    again. It is better to swap out those pages and create more disk
    buffers than leave them resident and unused"




    In other words, the data stored in RAM may only be useful when a process has been started (like the startup process), and then simply reside there in RAM, cluttering space which can be used for better purposes.



  2. It expands the amount of memory that processes may use. This is well understood. You don't want the system to run out of memory and crash due to lack of it. For this specific reason, swap must exist , serving as a protective feature.


  3. Hibernation: When a computer hibernates, the contents of RAM go to disk.



Swap amount



Knowing the purpose for swap and depending on your machine's RAM amount , you can choose a swap size that fits your purpose. There aren't specific guidelines, only rules of thumb which you can use to tailor your swap size to fit your system.



For instance, linux.com recommends:




A rule of thumb is as follows: 1) for a desktop system, use a swap
space of double system memory, as it will allow you to run a large
number of applications (many of which may will be idle and easily
swapped), making more RAM available for the active applications; 2)
for a server, have a smaller amount of swap available (say half of
physical memory) so that you have some flexibility for swapping when
needed, but monitor the amount of swap space used and upgrade your RAM
if necessary; 3) for older desktop machines (with say only 128MB), use
as much swap space as you can spare, even up to 1GB.




From personal experience, here's what I'd do:



Considering a machine with 1 - 2 GB that doesn't need to hibernate and for casual usage, you can have 1GB to 2GB swap space. My PC with 14.04 Ubuntu has 1GB of RAM and 2GB swap. The swap amount never crosses the 800MB border, and I use my PC only for surfing the web and scripting.



With a PC that has over 2GB and you don't need to hibernate, you may keep swap from 512MB to 1GB. I have a laptop with 15.04 which I don't hibernate ever and which has a swap file (not a partition) of 512 MB. RAM itself is 6 GB but doesn't cross 2-3 GB ever. Again: casual usage.



Cosmoscalibur and Fabby already addressed swap in case you need to hibernate, so I won't go into that too extensively. My humble opinion is this:



If you plan on hibernating, RAM contents will need to be swapped out, hence you need it at least the same size as your RAM.






share|improve this answer























  • :-) Only noticed now you posted something. Edited and upvoted! (As ist now is a good answer after my edit! :D :D :D )
    – Fabby
    Oct 23 '16 at 18:12













Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
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',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f594054%2fhow-much-swap-should-i-take-for-1gb-to-8tb-of-ram-on-14-04-or-higher%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









21














The short answer:



Set your swap file to:





  • round(sqrt(RAM)) if you don't use hibernation


  • RAM+round(sqrt(RAM)) if you do use hibernation


Set your swappiness to 10 on a desktop, but not on a server!



The long answer:



In the past:



The rule of thumb in use for the last 25 years has been a minimum of 1xRAM and maximum 2xRAM so that is what you'll see quoted all the time.



That minimum was set back in the stone age when I was a teenager and dinosaurs still roamed the Earth and because RAM was just too expensive and you absolutely needed that swap space to be able to accomplish anything.



The maximum was set at that time because of diminishing returns: it's just too slow to have to swap so much memory as HDD access is a factor of 1000 slower then RAM: good in an emergency, but not really good for everyday use! At the time, when you ran out of swap space, it was time to add more RAM! (which is still true today).



In the present:




  1. If you do not use hibernation and your memory is in excess of 1GByte the new rule of thumb is round(sqrt(RAM)) where RAM is obviously your RAM size in GB.


  2. If you use hibernation, you need to be able to swap the entire amount of RAM+already swapped RAM to disk, thus the formula becomes: RAM+round(sqrt(RAM))


  3. The rule of diminishing returns still holds today for the maximum, but unless you test your actual usage, taking 2xRAM is just a waste of disk space, so don't use the maximum Unless you run out of swap space using the other methodologies.



All of these together give you the following table: (last 3 columns denoting swap space)



    RAM   No hibernation    With Hibernation    Maximum
1GB 1GB 2GB 2GB
2GB 1GB 3GB 4GB
3GB 2GB 5GB 6GB
4GB 2GB 6GB 8GB
5GB 2GB 7GB 10GB
6GB 2GB 8GB 12GB
8GB 3GB 11GB 16GB
12GB 3GB 15GB 24GB
16GB 4GB 20GB 32GB
24GB 5GB 29GB 48GB
32GB 6GB 38GB 64GB
64GB 8GB 72GB 128GB
128GB 11GB 139GB 256GB
256GB 16GB 272GB 512GB
512GB 23GB 535GB 1TB
1TB 32GB 1056GB 2TB
2TB 46GB 2094GB 4TB
4TB 64GB 4160GB 8TB
8TB 91GB 8283GB 16TB


The above is just a rule of thumb; it's not the law of gravity!

You can break this rule (unlike the law of gravity) if your particular use case is different!



Pro tip: Always allocate SWAP at the start of a HDD as the heads need to move less on the inside of the disk.
Yes: On SSDs, it doesn't really matter any more where you locate the swap area as they use quantum-tunnelling instead of moving heads and modern SSDs use all of their memory cells (even the unallocated space) to prevent quantum degradation.



How to test if your usage of swap is different from the "generic" rule:



Just execute:



for szFile in /proc/*/status ; do 
awk '/VmSwap|Name/{printf $2 "t" $3}END{ print "" }' $szFile
done | sort --key 2 --numeric --reverse | more


which will give you a list of all running programs that are swapped out (with the one using the most swap space on top)



If you're using more then a few KB: resize to more then the minimum, otherwise, don't bother...



If you're on a server, stop reading now: you're all set!





If you're on a desktop/laptop client (not server), you want your GUI to be as responsive as possible and only swap when you really need to. Ubuntu has been optimised to swap early for server use, but on your client you want editing that huge 250 Mega-pixel raw picture in gimp to be speedy, so setting your swappiness to 10 will keep the kernel from swapping too early, while ensuring it doesn't swap too late:



sudo nano /etc/sysctl.conf


and add:



# change "swappiness" from default 60 to 10 
# (theoretically only swap when RAM usage reaches around 80 or 90 percent)
vm.swappiness = 10


to the end of the file, save the file (Ctrl+XY+Enter in nano) and execute a:



sysctl --system


to reload the parameter or take the Window$ approach and reboot... :-)






share|improve this answer



















  • 1




    Thanks. What do you mean by "diminishing return"? If both the RAM and the swap are fully used, then it is impossible to hibernate even if the swap is 2*RAM size, isn't it? (Because hibernation will need the size of the swap to be RAM + swap, which is impossible?)
    – Tim
    Mar 9 '15 at 0:11












  • Yes, but I've never had this happen yet... (and I'm old! therefore 2* RAM: When all of the RAM is swapped, you can still hibernate...
    – Fabby
    Mar 9 '15 at 8:15






  • 2




    "Diminishing returns" just means that HDD access is a factor of 1000 slower then RAM, so if you need to swap out twice your RAM you'll have died of old age before it completes... It's just there to hibernate when you're already swapping...
    – Fabby
    Mar 9 '15 at 8:28






  • 1




    @Fabby It's CV reviewing time on the original question again: askubuntu.com/review/close/889454. See Yufenyuy Veyeh Dider's comment under the question.
    – karel
    Oct 21 at 1:41










  • Voted to keep open and upvoted your comment so the next person in the review queue will see this as well. (and thanks for the heads-up)
    – Fabby
    Oct 21 at 15:27
















21














The short answer:



Set your swap file to:





  • round(sqrt(RAM)) if you don't use hibernation


  • RAM+round(sqrt(RAM)) if you do use hibernation


Set your swappiness to 10 on a desktop, but not on a server!



The long answer:



In the past:



The rule of thumb in use for the last 25 years has been a minimum of 1xRAM and maximum 2xRAM so that is what you'll see quoted all the time.



That minimum was set back in the stone age when I was a teenager and dinosaurs still roamed the Earth and because RAM was just too expensive and you absolutely needed that swap space to be able to accomplish anything.



The maximum was set at that time because of diminishing returns: it's just too slow to have to swap so much memory as HDD access is a factor of 1000 slower then RAM: good in an emergency, but not really good for everyday use! At the time, when you ran out of swap space, it was time to add more RAM! (which is still true today).



In the present:




  1. If you do not use hibernation and your memory is in excess of 1GByte the new rule of thumb is round(sqrt(RAM)) where RAM is obviously your RAM size in GB.


  2. If you use hibernation, you need to be able to swap the entire amount of RAM+already swapped RAM to disk, thus the formula becomes: RAM+round(sqrt(RAM))


  3. The rule of diminishing returns still holds today for the maximum, but unless you test your actual usage, taking 2xRAM is just a waste of disk space, so don't use the maximum Unless you run out of swap space using the other methodologies.



All of these together give you the following table: (last 3 columns denoting swap space)



    RAM   No hibernation    With Hibernation    Maximum
1GB 1GB 2GB 2GB
2GB 1GB 3GB 4GB
3GB 2GB 5GB 6GB
4GB 2GB 6GB 8GB
5GB 2GB 7GB 10GB
6GB 2GB 8GB 12GB
8GB 3GB 11GB 16GB
12GB 3GB 15GB 24GB
16GB 4GB 20GB 32GB
24GB 5GB 29GB 48GB
32GB 6GB 38GB 64GB
64GB 8GB 72GB 128GB
128GB 11GB 139GB 256GB
256GB 16GB 272GB 512GB
512GB 23GB 535GB 1TB
1TB 32GB 1056GB 2TB
2TB 46GB 2094GB 4TB
4TB 64GB 4160GB 8TB
8TB 91GB 8283GB 16TB


The above is just a rule of thumb; it's not the law of gravity!

You can break this rule (unlike the law of gravity) if your particular use case is different!



Pro tip: Always allocate SWAP at the start of a HDD as the heads need to move less on the inside of the disk.
Yes: On SSDs, it doesn't really matter any more where you locate the swap area as they use quantum-tunnelling instead of moving heads and modern SSDs use all of their memory cells (even the unallocated space) to prevent quantum degradation.



How to test if your usage of swap is different from the "generic" rule:



Just execute:



for szFile in /proc/*/status ; do 
awk '/VmSwap|Name/{printf $2 "t" $3}END{ print "" }' $szFile
done | sort --key 2 --numeric --reverse | more


which will give you a list of all running programs that are swapped out (with the one using the most swap space on top)



If you're using more then a few KB: resize to more then the minimum, otherwise, don't bother...



If you're on a server, stop reading now: you're all set!





If you're on a desktop/laptop client (not server), you want your GUI to be as responsive as possible and only swap when you really need to. Ubuntu has been optimised to swap early for server use, but on your client you want editing that huge 250 Mega-pixel raw picture in gimp to be speedy, so setting your swappiness to 10 will keep the kernel from swapping too early, while ensuring it doesn't swap too late:



sudo nano /etc/sysctl.conf


and add:



# change "swappiness" from default 60 to 10 
# (theoretically only swap when RAM usage reaches around 80 or 90 percent)
vm.swappiness = 10


to the end of the file, save the file (Ctrl+XY+Enter in nano) and execute a:



sysctl --system


to reload the parameter or take the Window$ approach and reboot... :-)






share|improve this answer



















  • 1




    Thanks. What do you mean by "diminishing return"? If both the RAM and the swap are fully used, then it is impossible to hibernate even if the swap is 2*RAM size, isn't it? (Because hibernation will need the size of the swap to be RAM + swap, which is impossible?)
    – Tim
    Mar 9 '15 at 0:11












  • Yes, but I've never had this happen yet... (and I'm old! therefore 2* RAM: When all of the RAM is swapped, you can still hibernate...
    – Fabby
    Mar 9 '15 at 8:15






  • 2




    "Diminishing returns" just means that HDD access is a factor of 1000 slower then RAM, so if you need to swap out twice your RAM you'll have died of old age before it completes... It's just there to hibernate when you're already swapping...
    – Fabby
    Mar 9 '15 at 8:28






  • 1




    @Fabby It's CV reviewing time on the original question again: askubuntu.com/review/close/889454. See Yufenyuy Veyeh Dider's comment under the question.
    – karel
    Oct 21 at 1:41










  • Voted to keep open and upvoted your comment so the next person in the review queue will see this as well. (and thanks for the heads-up)
    – Fabby
    Oct 21 at 15:27














21












21








21






The short answer:



Set your swap file to:





  • round(sqrt(RAM)) if you don't use hibernation


  • RAM+round(sqrt(RAM)) if you do use hibernation


Set your swappiness to 10 on a desktop, but not on a server!



The long answer:



In the past:



The rule of thumb in use for the last 25 years has been a minimum of 1xRAM and maximum 2xRAM so that is what you'll see quoted all the time.



That minimum was set back in the stone age when I was a teenager and dinosaurs still roamed the Earth and because RAM was just too expensive and you absolutely needed that swap space to be able to accomplish anything.



The maximum was set at that time because of diminishing returns: it's just too slow to have to swap so much memory as HDD access is a factor of 1000 slower then RAM: good in an emergency, but not really good for everyday use! At the time, when you ran out of swap space, it was time to add more RAM! (which is still true today).



In the present:




  1. If you do not use hibernation and your memory is in excess of 1GByte the new rule of thumb is round(sqrt(RAM)) where RAM is obviously your RAM size in GB.


  2. If you use hibernation, you need to be able to swap the entire amount of RAM+already swapped RAM to disk, thus the formula becomes: RAM+round(sqrt(RAM))


  3. The rule of diminishing returns still holds today for the maximum, but unless you test your actual usage, taking 2xRAM is just a waste of disk space, so don't use the maximum Unless you run out of swap space using the other methodologies.



All of these together give you the following table: (last 3 columns denoting swap space)



    RAM   No hibernation    With Hibernation    Maximum
1GB 1GB 2GB 2GB
2GB 1GB 3GB 4GB
3GB 2GB 5GB 6GB
4GB 2GB 6GB 8GB
5GB 2GB 7GB 10GB
6GB 2GB 8GB 12GB
8GB 3GB 11GB 16GB
12GB 3GB 15GB 24GB
16GB 4GB 20GB 32GB
24GB 5GB 29GB 48GB
32GB 6GB 38GB 64GB
64GB 8GB 72GB 128GB
128GB 11GB 139GB 256GB
256GB 16GB 272GB 512GB
512GB 23GB 535GB 1TB
1TB 32GB 1056GB 2TB
2TB 46GB 2094GB 4TB
4TB 64GB 4160GB 8TB
8TB 91GB 8283GB 16TB


The above is just a rule of thumb; it's not the law of gravity!

You can break this rule (unlike the law of gravity) if your particular use case is different!



Pro tip: Always allocate SWAP at the start of a HDD as the heads need to move less on the inside of the disk.
Yes: On SSDs, it doesn't really matter any more where you locate the swap area as they use quantum-tunnelling instead of moving heads and modern SSDs use all of their memory cells (even the unallocated space) to prevent quantum degradation.



How to test if your usage of swap is different from the "generic" rule:



Just execute:



for szFile in /proc/*/status ; do 
awk '/VmSwap|Name/{printf $2 "t" $3}END{ print "" }' $szFile
done | sort --key 2 --numeric --reverse | more


which will give you a list of all running programs that are swapped out (with the one using the most swap space on top)



If you're using more then a few KB: resize to more then the minimum, otherwise, don't bother...



If you're on a server, stop reading now: you're all set!





If you're on a desktop/laptop client (not server), you want your GUI to be as responsive as possible and only swap when you really need to. Ubuntu has been optimised to swap early for server use, but on your client you want editing that huge 250 Mega-pixel raw picture in gimp to be speedy, so setting your swappiness to 10 will keep the kernel from swapping too early, while ensuring it doesn't swap too late:



sudo nano /etc/sysctl.conf


and add:



# change "swappiness" from default 60 to 10 
# (theoretically only swap when RAM usage reaches around 80 or 90 percent)
vm.swappiness = 10


to the end of the file, save the file (Ctrl+XY+Enter in nano) and execute a:



sysctl --system


to reload the parameter or take the Window$ approach and reboot... :-)






share|improve this answer














The short answer:



Set your swap file to:





  • round(sqrt(RAM)) if you don't use hibernation


  • RAM+round(sqrt(RAM)) if you do use hibernation


Set your swappiness to 10 on a desktop, but not on a server!



The long answer:



In the past:



The rule of thumb in use for the last 25 years has been a minimum of 1xRAM and maximum 2xRAM so that is what you'll see quoted all the time.



That minimum was set back in the stone age when I was a teenager and dinosaurs still roamed the Earth and because RAM was just too expensive and you absolutely needed that swap space to be able to accomplish anything.



The maximum was set at that time because of diminishing returns: it's just too slow to have to swap so much memory as HDD access is a factor of 1000 slower then RAM: good in an emergency, but not really good for everyday use! At the time, when you ran out of swap space, it was time to add more RAM! (which is still true today).



In the present:




  1. If you do not use hibernation and your memory is in excess of 1GByte the new rule of thumb is round(sqrt(RAM)) where RAM is obviously your RAM size in GB.


  2. If you use hibernation, you need to be able to swap the entire amount of RAM+already swapped RAM to disk, thus the formula becomes: RAM+round(sqrt(RAM))


  3. The rule of diminishing returns still holds today for the maximum, but unless you test your actual usage, taking 2xRAM is just a waste of disk space, so don't use the maximum Unless you run out of swap space using the other methodologies.



All of these together give you the following table: (last 3 columns denoting swap space)



    RAM   No hibernation    With Hibernation    Maximum
1GB 1GB 2GB 2GB
2GB 1GB 3GB 4GB
3GB 2GB 5GB 6GB
4GB 2GB 6GB 8GB
5GB 2GB 7GB 10GB
6GB 2GB 8GB 12GB
8GB 3GB 11GB 16GB
12GB 3GB 15GB 24GB
16GB 4GB 20GB 32GB
24GB 5GB 29GB 48GB
32GB 6GB 38GB 64GB
64GB 8GB 72GB 128GB
128GB 11GB 139GB 256GB
256GB 16GB 272GB 512GB
512GB 23GB 535GB 1TB
1TB 32GB 1056GB 2TB
2TB 46GB 2094GB 4TB
4TB 64GB 4160GB 8TB
8TB 91GB 8283GB 16TB


The above is just a rule of thumb; it's not the law of gravity!

You can break this rule (unlike the law of gravity) if your particular use case is different!



Pro tip: Always allocate SWAP at the start of a HDD as the heads need to move less on the inside of the disk.
Yes: On SSDs, it doesn't really matter any more where you locate the swap area as they use quantum-tunnelling instead of moving heads and modern SSDs use all of their memory cells (even the unallocated space) to prevent quantum degradation.



How to test if your usage of swap is different from the "generic" rule:



Just execute:



for szFile in /proc/*/status ; do 
awk '/VmSwap|Name/{printf $2 "t" $3}END{ print "" }' $szFile
done | sort --key 2 --numeric --reverse | more


which will give you a list of all running programs that are swapped out (with the one using the most swap space on top)



If you're using more then a few KB: resize to more then the minimum, otherwise, don't bother...



If you're on a server, stop reading now: you're all set!





If you're on a desktop/laptop client (not server), you want your GUI to be as responsive as possible and only swap when you really need to. Ubuntu has been optimised to swap early for server use, but on your client you want editing that huge 250 Mega-pixel raw picture in gimp to be speedy, so setting your swappiness to 10 will keep the kernel from swapping too early, while ensuring it doesn't swap too late:



sudo nano /etc/sysctl.conf


and add:



# change "swappiness" from default 60 to 10 
# (theoretically only swap when RAM usage reaches around 80 or 90 percent)
vm.swappiness = 10


to the end of the file, save the file (Ctrl+XY+Enter in nano) and execute a:



sysctl --system


to reload the parameter or take the Window$ approach and reboot... :-)







share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 19 at 0:29

























answered Mar 8 '15 at 23:50









Fabby

26.4k1360159




26.4k1360159








  • 1




    Thanks. What do you mean by "diminishing return"? If both the RAM and the swap are fully used, then it is impossible to hibernate even if the swap is 2*RAM size, isn't it? (Because hibernation will need the size of the swap to be RAM + swap, which is impossible?)
    – Tim
    Mar 9 '15 at 0:11












  • Yes, but I've never had this happen yet... (and I'm old! therefore 2* RAM: When all of the RAM is swapped, you can still hibernate...
    – Fabby
    Mar 9 '15 at 8:15






  • 2




    "Diminishing returns" just means that HDD access is a factor of 1000 slower then RAM, so if you need to swap out twice your RAM you'll have died of old age before it completes... It's just there to hibernate when you're already swapping...
    – Fabby
    Mar 9 '15 at 8:28






  • 1




    @Fabby It's CV reviewing time on the original question again: askubuntu.com/review/close/889454. See Yufenyuy Veyeh Dider's comment under the question.
    – karel
    Oct 21 at 1:41










  • Voted to keep open and upvoted your comment so the next person in the review queue will see this as well. (and thanks for the heads-up)
    – Fabby
    Oct 21 at 15:27














  • 1




    Thanks. What do you mean by "diminishing return"? If both the RAM and the swap are fully used, then it is impossible to hibernate even if the swap is 2*RAM size, isn't it? (Because hibernation will need the size of the swap to be RAM + swap, which is impossible?)
    – Tim
    Mar 9 '15 at 0:11












  • Yes, but I've never had this happen yet... (and I'm old! therefore 2* RAM: When all of the RAM is swapped, you can still hibernate...
    – Fabby
    Mar 9 '15 at 8:15






  • 2




    "Diminishing returns" just means that HDD access is a factor of 1000 slower then RAM, so if you need to swap out twice your RAM you'll have died of old age before it completes... It's just there to hibernate when you're already swapping...
    – Fabby
    Mar 9 '15 at 8:28






  • 1




    @Fabby It's CV reviewing time on the original question again: askubuntu.com/review/close/889454. See Yufenyuy Veyeh Dider's comment under the question.
    – karel
    Oct 21 at 1:41










  • Voted to keep open and upvoted your comment so the next person in the review queue will see this as well. (and thanks for the heads-up)
    – Fabby
    Oct 21 at 15:27








1




1




Thanks. What do you mean by "diminishing return"? If both the RAM and the swap are fully used, then it is impossible to hibernate even if the swap is 2*RAM size, isn't it? (Because hibernation will need the size of the swap to be RAM + swap, which is impossible?)
– Tim
Mar 9 '15 at 0:11






Thanks. What do you mean by "diminishing return"? If both the RAM and the swap are fully used, then it is impossible to hibernate even if the swap is 2*RAM size, isn't it? (Because hibernation will need the size of the swap to be RAM + swap, which is impossible?)
– Tim
Mar 9 '15 at 0:11














Yes, but I've never had this happen yet... (and I'm old! therefore 2* RAM: When all of the RAM is swapped, you can still hibernate...
– Fabby
Mar 9 '15 at 8:15




Yes, but I've never had this happen yet... (and I'm old! therefore 2* RAM: When all of the RAM is swapped, you can still hibernate...
– Fabby
Mar 9 '15 at 8:15




2




2




"Diminishing returns" just means that HDD access is a factor of 1000 slower then RAM, so if you need to swap out twice your RAM you'll have died of old age before it completes... It's just there to hibernate when you're already swapping...
– Fabby
Mar 9 '15 at 8:28




"Diminishing returns" just means that HDD access is a factor of 1000 slower then RAM, so if you need to swap out twice your RAM you'll have died of old age before it completes... It's just there to hibernate when you're already swapping...
– Fabby
Mar 9 '15 at 8:28




1




1




@Fabby It's CV reviewing time on the original question again: askubuntu.com/review/close/889454. See Yufenyuy Veyeh Dider's comment under the question.
– karel
Oct 21 at 1:41




@Fabby It's CV reviewing time on the original question again: askubuntu.com/review/close/889454. See Yufenyuy Veyeh Dider's comment under the question.
– karel
Oct 21 at 1:41












Voted to keep open and upvoted your comment so the next person in the review queue will see this as well. (and thanks for the heads-up)
– Fabby
Oct 21 at 15:27




Voted to keep open and upvoted your comment so the next person in the review queue will see this as well. (and thanks for the heads-up)
– Fabby
Oct 21 at 15:27













5














Necessity for swap



Swap is useful for 3 reasons:





  1. Removing clutter from RAM. From the article on kernel.org:




    "A significant number of the pages referenced by a process early in
    its life may only be used for initialisation and then never used
    again. It is better to swap out those pages and create more disk
    buffers than leave them resident and unused"




    In other words, the data stored in RAM may only be useful when a process has been started (like the startup process), and then simply reside there in RAM, cluttering space which can be used for better purposes.



  2. It expands the amount of memory that processes may use. This is well understood. You don't want the system to run out of memory and crash due to lack of it. For this specific reason, swap must exist , serving as a protective feature.


  3. Hibernation: When a computer hibernates, the contents of RAM go to disk.



Swap amount



Knowing the purpose for swap and depending on your machine's RAM amount , you can choose a swap size that fits your purpose. There aren't specific guidelines, only rules of thumb which you can use to tailor your swap size to fit your system.



For instance, linux.com recommends:




A rule of thumb is as follows: 1) for a desktop system, use a swap
space of double system memory, as it will allow you to run a large
number of applications (many of which may will be idle and easily
swapped), making more RAM available for the active applications; 2)
for a server, have a smaller amount of swap available (say half of
physical memory) so that you have some flexibility for swapping when
needed, but monitor the amount of swap space used and upgrade your RAM
if necessary; 3) for older desktop machines (with say only 128MB), use
as much swap space as you can spare, even up to 1GB.




From personal experience, here's what I'd do:



Considering a machine with 1 - 2 GB that doesn't need to hibernate and for casual usage, you can have 1GB to 2GB swap space. My PC with 14.04 Ubuntu has 1GB of RAM and 2GB swap. The swap amount never crosses the 800MB border, and I use my PC only for surfing the web and scripting.



With a PC that has over 2GB and you don't need to hibernate, you may keep swap from 512MB to 1GB. I have a laptop with 15.04 which I don't hibernate ever and which has a swap file (not a partition) of 512 MB. RAM itself is 6 GB but doesn't cross 2-3 GB ever. Again: casual usage.



Cosmoscalibur and Fabby already addressed swap in case you need to hibernate, so I won't go into that too extensively. My humble opinion is this:



If you plan on hibernating, RAM contents will need to be swapped out, hence you need it at least the same size as your RAM.






share|improve this answer























  • :-) Only noticed now you posted something. Edited and upvoted! (As ist now is a good answer after my edit! :D :D :D )
    – Fabby
    Oct 23 '16 at 18:12


















5














Necessity for swap



Swap is useful for 3 reasons:





  1. Removing clutter from RAM. From the article on kernel.org:




    "A significant number of the pages referenced by a process early in
    its life may only be used for initialisation and then never used
    again. It is better to swap out those pages and create more disk
    buffers than leave them resident and unused"




    In other words, the data stored in RAM may only be useful when a process has been started (like the startup process), and then simply reside there in RAM, cluttering space which can be used for better purposes.



  2. It expands the amount of memory that processes may use. This is well understood. You don't want the system to run out of memory and crash due to lack of it. For this specific reason, swap must exist , serving as a protective feature.


  3. Hibernation: When a computer hibernates, the contents of RAM go to disk.



Swap amount



Knowing the purpose for swap and depending on your machine's RAM amount , you can choose a swap size that fits your purpose. There aren't specific guidelines, only rules of thumb which you can use to tailor your swap size to fit your system.



For instance, linux.com recommends:




A rule of thumb is as follows: 1) for a desktop system, use a swap
space of double system memory, as it will allow you to run a large
number of applications (many of which may will be idle and easily
swapped), making more RAM available for the active applications; 2)
for a server, have a smaller amount of swap available (say half of
physical memory) so that you have some flexibility for swapping when
needed, but monitor the amount of swap space used and upgrade your RAM
if necessary; 3) for older desktop machines (with say only 128MB), use
as much swap space as you can spare, even up to 1GB.




From personal experience, here's what I'd do:



Considering a machine with 1 - 2 GB that doesn't need to hibernate and for casual usage, you can have 1GB to 2GB swap space. My PC with 14.04 Ubuntu has 1GB of RAM and 2GB swap. The swap amount never crosses the 800MB border, and I use my PC only for surfing the web and scripting.



With a PC that has over 2GB and you don't need to hibernate, you may keep swap from 512MB to 1GB. I have a laptop with 15.04 which I don't hibernate ever and which has a swap file (not a partition) of 512 MB. RAM itself is 6 GB but doesn't cross 2-3 GB ever. Again: casual usage.



Cosmoscalibur and Fabby already addressed swap in case you need to hibernate, so I won't go into that too extensively. My humble opinion is this:



If you plan on hibernating, RAM contents will need to be swapped out, hence you need it at least the same size as your RAM.






share|improve this answer























  • :-) Only noticed now you posted something. Edited and upvoted! (As ist now is a good answer after my edit! :D :D :D )
    – Fabby
    Oct 23 '16 at 18:12
















5












5








5






Necessity for swap



Swap is useful for 3 reasons:





  1. Removing clutter from RAM. From the article on kernel.org:




    "A significant number of the pages referenced by a process early in
    its life may only be used for initialisation and then never used
    again. It is better to swap out those pages and create more disk
    buffers than leave them resident and unused"




    In other words, the data stored in RAM may only be useful when a process has been started (like the startup process), and then simply reside there in RAM, cluttering space which can be used for better purposes.



  2. It expands the amount of memory that processes may use. This is well understood. You don't want the system to run out of memory and crash due to lack of it. For this specific reason, swap must exist , serving as a protective feature.


  3. Hibernation: When a computer hibernates, the contents of RAM go to disk.



Swap amount



Knowing the purpose for swap and depending on your machine's RAM amount , you can choose a swap size that fits your purpose. There aren't specific guidelines, only rules of thumb which you can use to tailor your swap size to fit your system.



For instance, linux.com recommends:




A rule of thumb is as follows: 1) for a desktop system, use a swap
space of double system memory, as it will allow you to run a large
number of applications (many of which may will be idle and easily
swapped), making more RAM available for the active applications; 2)
for a server, have a smaller amount of swap available (say half of
physical memory) so that you have some flexibility for swapping when
needed, but monitor the amount of swap space used and upgrade your RAM
if necessary; 3) for older desktop machines (with say only 128MB), use
as much swap space as you can spare, even up to 1GB.




From personal experience, here's what I'd do:



Considering a machine with 1 - 2 GB that doesn't need to hibernate and for casual usage, you can have 1GB to 2GB swap space. My PC with 14.04 Ubuntu has 1GB of RAM and 2GB swap. The swap amount never crosses the 800MB border, and I use my PC only for surfing the web and scripting.



With a PC that has over 2GB and you don't need to hibernate, you may keep swap from 512MB to 1GB. I have a laptop with 15.04 which I don't hibernate ever and which has a swap file (not a partition) of 512 MB. RAM itself is 6 GB but doesn't cross 2-3 GB ever. Again: casual usage.



Cosmoscalibur and Fabby already addressed swap in case you need to hibernate, so I won't go into that too extensively. My humble opinion is this:



If you plan on hibernating, RAM contents will need to be swapped out, hence you need it at least the same size as your RAM.






share|improve this answer














Necessity for swap



Swap is useful for 3 reasons:





  1. Removing clutter from RAM. From the article on kernel.org:




    "A significant number of the pages referenced by a process early in
    its life may only be used for initialisation and then never used
    again. It is better to swap out those pages and create more disk
    buffers than leave them resident and unused"




    In other words, the data stored in RAM may only be useful when a process has been started (like the startup process), and then simply reside there in RAM, cluttering space which can be used for better purposes.



  2. It expands the amount of memory that processes may use. This is well understood. You don't want the system to run out of memory and crash due to lack of it. For this specific reason, swap must exist , serving as a protective feature.


  3. Hibernation: When a computer hibernates, the contents of RAM go to disk.



Swap amount



Knowing the purpose for swap and depending on your machine's RAM amount , you can choose a swap size that fits your purpose. There aren't specific guidelines, only rules of thumb which you can use to tailor your swap size to fit your system.



For instance, linux.com recommends:




A rule of thumb is as follows: 1) for a desktop system, use a swap
space of double system memory, as it will allow you to run a large
number of applications (many of which may will be idle and easily
swapped), making more RAM available for the active applications; 2)
for a server, have a smaller amount of swap available (say half of
physical memory) so that you have some flexibility for swapping when
needed, but monitor the amount of swap space used and upgrade your RAM
if necessary; 3) for older desktop machines (with say only 128MB), use
as much swap space as you can spare, even up to 1GB.




From personal experience, here's what I'd do:



Considering a machine with 1 - 2 GB that doesn't need to hibernate and for casual usage, you can have 1GB to 2GB swap space. My PC with 14.04 Ubuntu has 1GB of RAM and 2GB swap. The swap amount never crosses the 800MB border, and I use my PC only for surfing the web and scripting.



With a PC that has over 2GB and you don't need to hibernate, you may keep swap from 512MB to 1GB. I have a laptop with 15.04 which I don't hibernate ever and which has a swap file (not a partition) of 512 MB. RAM itself is 6 GB but doesn't cross 2-3 GB ever. Again: casual usage.



Cosmoscalibur and Fabby already addressed swap in case you need to hibernate, so I won't go into that too extensively. My humble opinion is this:



If you plan on hibernating, RAM contents will need to be swapped out, hence you need it at least the same size as your RAM.







share|improve this answer














share|improve this answer



share|improve this answer








edited Oct 23 '16 at 18:11









Fabby

26.4k1360159




26.4k1360159










answered Aug 9 '15 at 23:23









Sergiy Kolodyazhnyy

69.3k9144304




69.3k9144304












  • :-) Only noticed now you posted something. Edited and upvoted! (As ist now is a good answer after my edit! :D :D :D )
    – Fabby
    Oct 23 '16 at 18:12




















  • :-) Only noticed now you posted something. Edited and upvoted! (As ist now is a good answer after my edit! :D :D :D )
    – Fabby
    Oct 23 '16 at 18:12


















:-) Only noticed now you posted something. Edited and upvoted! (As ist now is a good answer after my edit! :D :D :D )
– Fabby
Oct 23 '16 at 18:12






:-) Only noticed now you posted something. Edited and upvoted! (As ist now is a good answer after my edit! :D :D :D )
– Fabby
Oct 23 '16 at 18:12




















draft saved

draft discarded




















































Thanks for contributing an answer to Ask Ubuntu!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f594054%2fhow-much-swap-should-i-take-for-1gb-to-8tb-of-ram-on-14-04-or-higher%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How did Captain America manage to do this?

迪纳利

南乌拉尔铁路局