element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Personal Blogs
  • Community Hub
  • More
Personal Blogs
Legacy Personal Blogs Increasing Linux Swap with a File
  • Blog
  • Documents
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: cstanton
  • Date Created: 17 Dec 2014 8:38 PM Date Created
  • Views 1929 views
  • Likes 4 likes
  • Comments 9 comments
  • android
  • debian
  • ram
  • openwrt
  • compile
  • unstable
  • swap
  • memory
  • sid
  • jessie
  • sudo_open
  • pagefile
  • swapfile
  • systemv
  • fstab
  • systemd
  • extra
  • linux
Related
Recommended

Increasing Linux Swap with a File

cstanton
cstanton
17 Dec 2014

I've been setting up a spare computer so that I can cross-compile system images for various platforms I have. The spare computer really isn't all that powerful, but I have zero issue with just leaving it to churn away until it's done (maybe I should have a beowulf cluster made from my old scrap), so I'll be compiling OpenWRT, Android (hahaha ... aaah.. oh dear) and U-Boot images.

 

I also used it as an opportunity to learn more about Debian, so after installing Jessie I pushed it further and installed Unstable which means we have access to more packages, such as being able to install packages from multiple architectures (such as armhf) using packages maintained for Debian (hopefully).

 

So I encountered a problem where by my swap partition is too small, it's set to 4gByte and my system RAM is also 4gByte - enter a problem when compiling Android which requires 16gByte to compile. Bum.

 

I learned of a solution which is to create a swap file, as this was temporary and I did not want to re-size the swap partition in Linux. Swapping, if you don't know, is when you don't have enough RAM in your computer, so it copies the contents to your hard drive instead (RAM being significantly faster than your hard drive) which means that it'll still operate properly but it'll slow down, a lot. This is actually one of the more frequent complaints about Microsoft Windows with computers supplied by your company's IT Support or purchased from the store - the fact that you often have significantly less RAM than you require so it hammers the swap/pagefile.

 

With creating a swapfile I had to do the following:

 

  1. Create the file
  2. Make it recognised as swap
  3. Mount the file

 

To create the file, a lot of people and tutorials may recommend that you use 'dd', but this can be slow (like really stupidly slow) so instead I used 'fallocate' which instead of filling the amount of space required with data, or zeroes, preallocates the blocks and marks them as uninitialised.

 

sudo fallocate -l 20G /extraswap

 

We specify the length with "- l" and in this case I set it to twenty gigabytes and I put the swapfile in root, because I couldn't think of somewhere better and I'm lazy. I discovered later through trial and error that we want this to now be seen as a swap file (by that I mean it needs a header) and to do that we mkswap!

 

sudo mkswap /extraswap

 

Still not done, we have a choice, before it can be activated the permissions should be changed because otherwise it will complain at us:

 

sudo chmod 0600 /extraswap

 

Now I had a choice. Either I activate the swap temporarily and only use it when I need it, or I have it activated from system boot.

 

At the very least I tested to see if it was ok:

 

sudo swapon /extraswap

 

Since I'm using Debian Unstable/Sid the system has switched from SystemV to Systemd - "What's the difference?" is a big contentious topic right now, let's just say it's moving away from Keeping it Simple, Stupid and moving towards abstraction. For now, we can still edit fstab!

 

sudo vi /etc/fstab

 

And in fstab, we add the following line ("but how do I use vi?"):

 

/extraswap     none     swap     sw     0     0

 

And 'Ding!' the swap file should be mounted the next time the system boots, automagically.

  • Sign in to reply

Top Comments

  • phoenixcomm
    phoenixcomm over 6 years ago +1
    That is interesting but easier way is to make sure when you install the distro you select manual for your disk drive layout. This way you can sort layout any way you need. I personally like the following…
  • cstanton
    cstanton over 6 years ago in reply to phoenixcomm +1
    Though that requires you to know your requirements on initial setup of the partition table. These days it's likely best that you simply use LVM and so you can more readily and dynamically allocate your…
  • phoenixcomm
    phoenixcomm over 6 years ago in reply to cstanton +1
    Duh RTFM (the target computer)
  • phoenixcomm
    phoenixcomm over 6 years ago in reply to cstanton

    In the overview of the distro it will always tell you "minimum requirements"  like my Dell laptop I have 3Gb of memory 1+2 and to change the 1Gb to a 2Gb is a pain as you have to tear the thing apart.

    I am running Debian 9. when Debian created the swap file it made it 3Gb as is the custom for Linux 1:1 Bull 2:1 is better so I used a 6Gb swap and if I have delays I will make it 12Gb. I have worked with Unix for over many years were 2:1 swap was the norm. I'm not worried about disk space as I have a 2Tb drive in it. When installing the system I put on both Cinnamon and Xfce desktops where Cinnamon drags Xfce excels even with eclipse running on top of it.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • cstanton
    cstanton over 6 years ago in reply to phoenixcomm

    Yes, I am aware of the term, reading the man pages is always the first step, do you have a recommendation as to which man pages to read for this?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • phoenixcomm
    phoenixcomm over 6 years ago in reply to cstanton

    RTFM::= READ THE F***ING MANUAL

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • cstanton
    cstanton over 6 years ago in reply to phoenixcomm

    Pardon?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • phoenixcomm
    phoenixcomm over 6 years ago in reply to cstanton

    Duh RTFM (the target computer)

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube