Given that we know exactly what's running inside JVM with limited heap size, httpd, monitoring tools, And I think that in a virtual environment, you already have the overhead of the hypervisor.
And as a result swap in such an environment does not speed up things much. So, if you can, it is better to rely on RAM. On a physical server, swap space may have more sense. This is not something worth spending much thought about.
When I have looked at what my system is actually using with a monitoring tool, swap only rarely comes into play. Given the slow speed of hard drives, that much swapping, regardless of RAM size, would cripple performance. I maintain a similar philosophy today, but with larger numbers due to much much larger RAM sizes and much much faster storage. I will typically allocate GB of RAM to swap, primarily because I'd rather suffer a performance hit from swapping than have the kernel start terminating processes when memory runs out.
But I still believe that if you encounter any significant amount of swapping, then you really need more RAM. When creating virtual machines, that's a little different. I often create them with limited RAM GB because they're not running a GUI generally accessed via ssh sessions , run minimal daemons in the background and I don't want to take more than necessary from the host environment.
I will create a large 8GB swap partition in order to handle a worst-case scenario where I've woefully underestimated my RAM requirements and monitor the amount of swapping that takes place. I'll increase the RAM allocation as necessary to keep swapping to a minimum under expected loads and leave the swapfile to be available when loads are higher than expected. Either equal to the amount of installed RAM or none.
There are two factors to consider, when making this decision. I also prefer to fully shut down instead of ever hibernating.
I suppose it could be useful on a system with a less or slower RAM, but realistically, I just don't think it's necessary for the majority of use cases. Otherwise sistem will not hibernate or suspend correctly. I still almost double the ram: even with 32Gb of ram I often run out of memory while loading heavy 3d scenes. The only situation where I see swap space a must is editing large photos, like big panorama, with gimp: a lot of ram is needed, and if you don't have enough swap space, your system freezes and you are going to loose all your work.
At university, at the computer laboratory where we spent our time analysing DNA sequences, there was a big DNA sequence alignment experiment I was trying to perform for phylogeny production. The lab server computer had about 19GB of SWAP and probably 12GB of RAM I had helped set up months before and could not increase it further for a series of reasons at the time namely, I was not able to increase swap space with my then-Linux-knowledge and could not reinstall everything because the server had Windows on it, with lots of data from other people But I remember that the experiment algorithm I was using to process that data needed just a little more than 20 GB If we had , my experiment could have turned out to work out eventually I use a swapfile instead, which can grow dynamically.
My swapfile is mounted on my root partition, which I have given 64GB to. Of course, I never go that high, probably the most resource-intensive thing I ever run on my computer it's not even a computer, it's just a crappy tablet with a keyboard from my school with 4GB RAM is Android Studio.
Suspend to RAM is sufficient and works really well. Interesting article. I now know that I have 2G of Swap on Ubuntu Is that too little? I have since using Windows XP, disabled swap, never used it because I knew how to manage memory, Linux, gosh it never needed because it was so efficient at handling the memory, thus some of our friends point out under 8GB, and yeah maybe, those browsers are very memory hungry but I ran small Nix servers that use no more than Mb, sometimes growing to Mb, thus I feel if you sysadmin your machines correctly you can get away with very little ram and never even think of swap.
Some feel in the Microsoft world you need to enable swap to allow kernel swapping and some page functions require it Now for the non sysadmin and technical people, leave the swap on, let the system manage it for them because really life is busy enough, though the security issues that are associated with it sometimes cause more issues, in today it's a security nightmare since the average Joe will not enable encryption or flush the swap file at each reboot.
Anyway my 2 cents. If you already have swap space assigned and need more, it's easy enough to add another partition. The proper swappiness setting eliminates any thrashing. As Pierre suggested, I reset system swappiness to a more reasonable value than the default of Pierre uses 1.
Setting it to 0 effectively shuts off swapping. I usually use 10, but YMMV. I haven't used MS Windows in almost 20 years but, when I did, I set up a swap partition instead of a file. Today, with all the SSDs commonly found in new systems, you could avoid trouble by placing the partition on a standard HDD instead of the SSD, as many systems come with or can be upgraded to one of each.
Thank you, David Both, for a well-written post that clears up some confusion that many beginners and even some old-timers have about swap space. I have often checked and I have never seen any swap in use. Based on that I think 0. That sounds good, but then we want a rule linking swap space to workload, or better, linking RAM size to workload.
The reasoning would be that your swap space should be big enough to handle the maximum possible demand, whereas the amount of memory is linked to your 'working set size'. A reasonable measure is to look at your uptime run queue length. If that is 20 long-term average fior a system with infinite RAM that means your working set is 20 threads and you would also need that number of processor cores.
A first approximation would be to figure out what programs those threads might belong to, and the amount of memory they need and install roughly that amount of RAM. A better approach takes the size of those programs and the task switching frequency into account. Or simper:: just shrink the RAM to the point where the effective throughput and response-time is the required level, for instance, half the performance of a machine with unlimited RAM.
With SSDs, you would install rather less RAM than with magnetic discs, because the latency of your swap device is so much lower. A simple rule of thumb is that you need at least the amount of RAM for swap space in order to store a crash dump. When you start any significant swapping things usually so down so much it appears your app has "hung". Maybe for a server not running interactive code, but for me when sitting at the keyboard using swap is death. Why would I possibly want to have virtual memory that puts additional wear on my SSD?
I believe that the memory pages for executable code are "backed" on "disk" by the actual executable file. So there is "some" virtual memory pages already. But read only, not putting wear on the SSD. My rationale is this: RAM is cheap so use enough that you don't need a swap. I work almost exclusively from a laptop which is usually hibernated, rebooting preferably only once every few months usually for kernel upgrades due to security concerns.
This makes swap requirements very different from someone who never hibernates and reboots every day. After a week or two of run time, Chromium web browser can easily hit GB of memory usage by itself.
Add in Firefox with lots of tabs , a couple of active virtual machines, some bioinformatics analysis code running in background these often make poor use of memory , two or three active IDE's like Rstudio, PyCharm or if I'm feeling particularly masochistic , Eclipse, and I am well past the 32 GB of RAM on my laptop.
There is little to no swapping going on because while some of the software I use is well written and very efficient with it's use of memory, other programs are not, eating lots of storage because they keep things in memory that they no longer have any use for. This means I need lots of swap space for storing the "junk" that these programs keep lying around and thrashing usually isn't an issue. I currently have configured 48 GB of swap to go with my 32 GB of RAM to allow for hibernation and "junk", but with my increasing encounters with memory wasting code, 64 GB of swap may be more appropriate.
In my experience, it's all about the purpose of the box. Now that we know our available storage space, we can go about creating a swap file within our filesystem. The file must allocate the amount of space that we want for our swap file, and it should be created in one contiguous block. The best way to do this is to use the dd utility.
This command will create a 4 gigabyte file:. After entering your password to authorize sudo privileges, the swap file will be created. This can take a few moments, then the prompt will be returned to you. We can verify that the correct amount of space was reserved for swap by using ls :. Right now, our file is created, but our system does not know that this is supposed to be used for swap.
We need to tell our system to format this file as swap and then enable it. Allowing other users to read or write to this file would be a huge security risk. We can lock down the permissions with chmod :. This will restrict both read and write permissions to the root account only. We can verify that the swap file has the correct permissions by using ls -lh again:.
Now that our swap file is more secure, we can tell our system to set up the swap space for use by typing:. To verify that the procedure was successful, we can check whether our system reports swap space now:.
This output confirms that we have a new swap file. We can use the free utility again to corroborate our findings:. Our swap file is enabled at the moment, but when we reboot, the server will not automatically enable the file for use.
We can change that by modifying the fstab file, which is a table that manages filesystems and partitions. At the bottom of the file, you need to add a line that will tell the operating system to automatically use the swap file that you created:. When you are finished adding the line, you can save and close the file. The server will check this file on each bootup, so the swap file will be ready for use from now on.
These configurations are optional in most cases, and the changes that you make will depend on your application needs and your personal preference. The swappiness parameter determines how often your system swaps data out of memory to the swap space. This is a value between 0 and that represents the percentage of memory usage that will trigger the use of swap. With values close to zero, the system will not swap data to the drive unless absolutely necessary. Telling the system not to rely on the swap as much will generally make your system faster.
Values that are closer to will try to put more data into swap in an effort to keep more memory free. We can see the current swappiness value by reading the swappiness configuration file:.
CentOS 7 defaults to a swappiness setting of 30, which is a fair middle ground for most desktops and local servers. We can set the swappiness to a different value by using the sysctl command. For instance, to set the swappiness to 10, we could type:. This setting will persist until the next reboot. To allow for hibernation, however, you need to edit the swap space in the custom partitioning stage. Recommendations in Table Failure to allocate sufficient swap space on these systems can cause issues such as instability or even render the installed system unbootable.
At the border between each range listed in Table If your system resources allow for it, increasing the swap space may lead to better performance. Note that distributing swap space over multiple storage devices also improves swap space performance, particularly on systems with fast drives, controllers, and interfaces.
File systems and LVM2 volumes assigned as swap space should not be in use when being modified. Any attempts to modify swap fail if a system process or the kernel is using swap space. When prompted to mount the file system, select Skip.
Adding Swap Space. Sometimes it is necessary to add more swap space after installation. It might be advantageous to increase the amount of swap space to 4 GB if you perform memory-intense operations or run applications that require a large amount of memory.
0コメント