Beyond your conventional memory size with swap file instead of a partition

CK CK 3788 
Created at  
1,139 0 0

In CentOS, swap is a portion of the hard drive designated as virtual memory, acting as an extension of RAM. When the system's RAM is full, inactive data from RAM is moved to the swap space, freeing up RAM for active processes. While slower than RAM, swap prevents system crashes due to memory exhaustion, though excessive swap usage indicates insufficient RAM and can lead to performance degradation.
 

Can I enable swap without creating an additional disk partition?

You can enable swap space without creating a partition by using a file; create a large file (e.g., using fallocate -l 2G /swapfile for 2 gigabytes) with appropriate permissions (e.g., chmod 600 /swapfile), format it as a swap area (mkswap /swapfile), and finally enable it using swapon /swapfile. Remember to add a line /swapfile none swap sw 0 0 to your /etc/fstab file to make the swap file persistent across reboots.
 

What is a swap file?

You can't enable swap *without* using some form of disk space. Swap space requires a dedicated area on a storage device, whether that's a partition, a file, or a dedicated portion of a logical volume. There's no magic to create swap out of thin air.
However, you *can* create a swap *file* instead of a partition. This avoids the need to alter your existing partition table. Here's how to do it (these instructions are generic; the exact commands might need slight adjustments depending on your Linux distribution):

 

How do I create a swap file?

1. Create the swap file: Use the fallocate command (preferred for speed and efficiency) or dd (slower but more universally available) to create a file of the desired size. Replace with the desired size in MB (e.g., 2048 for 2GB). Choose a location where you have enough free space (e.g., /var/swap or a similar directory you have permissions to write to).
* Using fallocate (recommended):

sudo fallocate -l M /path/to/swapfile

* Using dd (if fallocate isn't available):

sudo dd if=/dev/zero of=/path/to/swapfile bs=1M count=


2. Set permissions: Make sure the swap file has the correct permissions:

sudo chmod 600 /path/to/swapfile


3. Set up the swap file: Use mkswap to prepare the file as a swap area:

sudo mkswap /path/to/swapfile


4. Enable the swap file: Activate the swap file:

sudo swapon /path/to/swapfile


5. Make the swap file permanent: Add a line to your /etc/fstab file to make the swap file active on every boot. This line should look like this (replace /path/to/swapfile with the actual path):

/path/to/swapfile none swap sw 0 0

Important: Add this line carefully and double-check the path. A mistake here can lead to boot problems.
 

6. Verify: Check if the swap file is active using:

sudo swapon --show


You should see your swap file listed.

Remember to replace /path/to/swapfile with the actual path you chose. Choose a swap file size appropriate to your system's RAM; a common suggestion is 1-2 times the amount of RAM you have, but that's a guideline, not a hard rule. Too little swap can hurt performance, but too much is wasted space. After creating the swap file, you can remove it by running sudo swapoff /path/to/swapfile and then sudo rm /path/to/swapfile. Do *not* delete the swap file while it's active.
 

Tags Virtual Memory swap Facebook X
Comments 0
Similar posts
  1. Memory Problems with Python Arrays? Discover Dask Array
    321
  1. Why SPF and DKIM Are Essential for Linux Email Servers?
    24
  2. Upgrading to GCC 4.9.4 in CentOS 7 requires installing a newer version manually
    579
  3. Force no caching in Apache by modifying the .htaccess file using appropriate headers
    2,668
  4. Gmail requires all senders to authenticate with either SPF or DKIM.
    679
  5. Bash script to install postfix and sasl for sending email
    1,192
  6. Guide for tmux shortcuts - split current pane, toggle pane
    1,294
  7. Tistory remote blogging example based in CLI & PHP
    2,706
  8. Generate Let’s Encrypt Wildcard SSL Certificate
    625
  9. Preventing Unauthorized Image Crawling by Bots: A Clever Discovery and How to Address It
    1,726
  10. Network diagnostic tool for IPv4/IPv6 - traceroute/traceroute6
    1,916
  11. chmod by type (directory and file permission)
    733
  12. Check network port is open or not on CentOS 7
    1,186
  13. Change MySQL port number for security purpose
    815
  14. CentOS warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or directory on CentOS 7
    1,070
  15. Set up an SMTP server on CentOS 7
    726
  16. How to mount windows shared folder on Centos 6.8 - mount error 12 = Cannot allocate memory to the mount.cifs(8)
    911
  17. Configuring IP Address on Centos 6.7
    786
  18. Adding new cron job for automated processing on every 10 minutes on Centos 6.7
    1,329
Recently updated
  1. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    27
  2. Why SPF and DKIM Are Essential for Linux Email Servers?
    24
  3. How do I create animated background for Google Meet?
    19,735
  4. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    217
  5. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    86
  6. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    153
  7. Array of arguments passed to script in PHP
    85
  8. Invalid Auction Storms in Ad Tech
    93
  9. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    198
  10. The Two Clocks of Ad Server Architecture
    63
  11. Frequency Cap Architecture Overview
    509
  12. Collecting Proof-of-Play from 100,000 Screens
    141
  13. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    280
  14. What is RedisVL?
    105
  15. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    224
  16. TPU & GPU Cluster Management Tools
    234
  17. The Concept of Scaffolding in AI Agents
    222
  18. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    576
  19. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    253
  20. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    328
  21. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    198
  22. Machine Learning: Training vs. Ranking
    213
  23. The Continuous Learning Machine: Designing a Loop That Actually Turns
    217
  24. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    189
  25. Designing a Data Curation System Around a Vector Database
    215
  26. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    229
  27. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    649
  28. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    185
  29. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    207
  30. Understanding Data Lineage
    252