🪟 Windows TippsHandy zu langsam? Diese Einstellungen kosten unnötig Leistung(16.09.2026 um 15:30 Uhr)
🪟 Windows TippsUmrüstung der Beleuchtung der Bundespressekonferenz auf LED(16.09.2026 um 15:36 Uhr)
🤖 Android TippsUmrüstung der Beleuchtung der Bundespressekonferenz auf LED(16.09.2026 um 15:36 Uhr)
🔧 ProgrammierungRobot Fleet Management Software: A Complete Guide(16.09.2026 um 15:24 Uhr)
🕵️ SicherheitslückenKnown MCP Vulnerabilities and How an MCP Gateway Blocks Them(16.09.2026 um 15:21 Uhr)
🪟 Windows TippsHandy zu langsam? Diese Einstellungen kosten unnötig Leistung(16.09.2026 um 15:30 Uhr)
🪟 Windows TippsUmrüstung der Beleuchtung der Bundespressekonferenz auf LED(16.09.2026 um 15:36 Uhr)
🤖 Android TippsUmrüstung der Beleuchtung der Bundespressekonferenz auf LED(16.09.2026 um 15:36 Uhr)
🔧 ProgrammierungRobot Fleet Management Software: A Complete Guide(16.09.2026 um 15:24 Uhr)
🕵️ SicherheitslückenKnown MCP Vulnerabilities and How an MCP Gateway Blocks Them(16.09.2026 um 15:21 Uhr)

🔧 Programmierung 🕛 vor 1 Jahr 5 Min Lesezeit
0

How To Add Swap Space on Ubuntu 22.04

↗ Quelle (dev.to)
🗣️ Stimme:

One way to guard against out-of-memory errors in applications is to add some swap space to your server. In this guide, we will cover how to add a swap file to an Ubuntu 22.04 server.



Swap is a portion of hard drive storage that has been set aside for the operating system to temporarily store data that it can no longer hold in RAM. This lets you increase the amount of information that your server can keep in its working memory, with some caveats. The swap space on the hard drive will be used mainly when there is no longer sufficient space in RAM to hold in-use application data.



The information written to disk will be significantly slower than information kept in RAM, but the operating system will prefer to keep running application data in memory and use swap for the older data. Overall, having swap space as a fallback for when your system’s RAM is depleted can be a good safety net against out-of-memory exceptions on systems with non-SSD storage available.



We can see if the system has any configured swap by typing:




CODE
sudo swapon --show






If it has one, we're going to want to get rid of it and create a new one. You can do that with the following command:




CODE
sudo swapoff -v /swapfile






And then remove the file




CODE
sudo rm /swapfile






Now we want to create a new swap file. We'll set the size parameter to 8G as we want to use some VMware in Ubuntu




CODE
sudo fallocate -l 8G /swapfile






Make the file only accessible to root by typing:




CODE
sudo chmod 600 /swapfile






We can now mark the file as swap space by typing:




CODE
sudo mkswap /swapfile






After marking the file, we can enable the swap file, allowing our system to start using it:




CODE
sudo swapon /swapfile






Our recent changes have enabled the swap file for the current session. However, if we reboot, the server will not retain the swap settings automatically. We can change this by adding the swap file to our /etc/fstab file.



Back up the /etc/fstab file in case anything goes wrong:




CODE
sudo cp /etc/fstab /etc/fstab.bak






Add the swap file information to the end of your /etc/fstab file by typing:




CODE
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab






There are a few options that you can configure that will have an impact on your system’s performance when dealing with swap.



The swappiness parameter configures how often your system swaps data out of RAM to the swap space. This is a value between 0 and 100 that represents a percentage.



With values close to zero, the kernel will not swap data to the disk unless absolutely necessary. Remember, interactions with the swap file are “expensive” in that they take a lot longer than interactions with RAM and they can cause a significant reduction in performance. Telling the system not to rely on the swap much will generally make your system faster.



Values that are closer to 100 will try to put more data into swap in an effort to keep more RAM space free. Depending on your applications’ memory profile or what you are using your server for, this might be better in some cases.



We can see the current swappiness value by typing:




CODE
cat /proc/sys/vm/swappiness






For a Desktop, a swappiness setting of 60 is not a bad value. For a server, you might want to move it closer to 0.



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:




CODE
sudo sysctl vm.swappiness=10






This setting will persist until the next reboot. We can set this value automatically at restart by adding the line to our /etc/sysctl.conf file:




CODE
sudo nano /etc/sysctl.conf






At the bottom, you can add vm.swappiness=10. Save and close the file when you are finished (in nano, press Ctrl+x and it will prompt you to save before exiting).



Another related value that you might want to modify is the vfs_cache_pressure. This setting configures how much the system will choose to cache inode and dentry information over other data.



Basically, this is access data about the filesystem. This is generally very costly to look up and very frequently requested, so it’s an excellent thing for your system to cache. You can see the current value by querying the proc filesystem again:




CODE
cat /proc/sys/vm/vfs_cache_pressure






As it is currently configured (100), our system removes inode information from the cache too quickly. We can set this to a more conservative setting like 50 by typing:




CODE
sudo sysctl vm.vfs_cache_pressure=50






Again, this is only valid for our current session. We can change that by adding it to our configuration file like we did with our swappiness setting:




CODE
sudo nano /etc/sysctl.conf






At the bottom, add the line that specifies your new value:




CODE
vm.vfs_cache_pressure=50






Save and close the file when you are finished.

Vollständiger Original-Artikel
Den kompletten Beitrag mit allen Details direkt auf dev.to lesen.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
2 Quellen
Umrüstung der Beleuchtung der Bundespressekonferenz auf LED
1 Quelle
Die langlebigsten Autos laut Studie: Diese Marke schlägt alle anderen Autohersteller
1 Quelle
Handy zu langsam? Diese Einstellungen kosten unnötig Leistung
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten How To Add Swap Space on Ubuntu 22.04

Thematisch verwandte Begriffe: Swap, Space, Ubuntu, 2204 · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...