Results 1 to 6 of 6

Thread: Shrink swap file

  1. #1
    Join Date
    Aug 2016
    Location
    El Lago, Texas
    Beans
    475
    Distro
    Ubuntu Mate

    Shrink swap file

    I want to shrink my swap file from 2 Gb to 1 Gb.

    I thought Gpart would do it.

    How do I do that ?
    Ubuntu Mate 18.04 Bionic
    Mate Desktop

  2. #2
    Join Date
    Jun 2016
    Beans
    1,781
    Distro
    Xubuntu 18.04 Bionic Beaver

    Re: Shrink swap file

    1) Temporarily stop the system using the swapfile
    Code:
    sudo swapoff -a
    2) delete the swapfile
    Code:
    sudo rm -v /path/to/your/swapfile
    3) create a new file of 1 GB size in exactly the same place as your old one was
    Code:
    sudo dd if=/dev/zero of=/path/to/your/swapfile bs=1M count=1024
    4) use mkswap to turn the file from (3) into a swapfile
    Code:
    sudo mkswap /path/to/your/swapfile
    5) For added security, set permissions on swapfile
    Code:
    sudo chmod 0600 /path/to/your/swapfile
    6) reboot


    Replace /path/to/your/swapfile with the actual full path to your swapfile.

    Does it work?
    Xubuntu 18.04/MacBookPro9,1 ♦ Debian 9/Mageia 6/Xubuntu 16.04/VirtualBox
    If your questions are resolved to your satisfaction, please use Thread Tools > "Mark this thread as solved..."

  3. #3
    Join Date
    Aug 2016
    Location
    Wandering
    Beans
    Hidden!
    Distro
    Xubuntu Development Release

    Re: Shrink swap file

    You will have to remove the old file first:
    Run the following commands:
    Code:
    sudo swapoff -a -v
    sudo rm /swapfile
    #back up /etc/fstab:
    sudo cp /etc/fstab /etc/fstab.bak
    sudo sed -i '/\/swapfile/d' /etc/fstab
    Now add the 1 Gig file
    Adjust this to meet the needs of your own system: (I set this as 1Gig)

    Code:
    sudo fallocate -l 1G /swapfile
    verify that the correct amount of space was reserved by :

    Code:
    ls -lh /swapfile
    Make the file only accessible to root by typing:

    Code:
    sudo chmod 600 /swapfile
    Verify the permissions change by typing:

    Code:
    ls -lh /swapfile
    
    -rw------- 1 root root 1.0G Apr 25 11:14 /swapfile
    As you can see, only the root user has the read and write flags enabled.

    now mark the file as swap space by:

    Code:
    sudo mkswap /swapfile
    After marking the file, we can enable the swap file, allowing our system to start utilizing it:

    Code:
    sudo swapon /swapfile
    We can verify that the swap is available by typing:

    Code:
    sudo swapon --show
    NAME      TYPE       SIZE USED PRIO
    /dev/sda2 partition    3G   0B   -2
    /swapfile file      1024M   0B   -3
    As you can see i never used a swapfile instead I used a partition and just now added a swap file.

    Make the Swap File Permanent:
    NOTE:Back up the /etc/fstab file in case anything goes wrong:

    Code:
    sudo cp /etc/fstab /etc/fstab.bak
    You can 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
    With realization of one's own potential and self-confidence in one's ability, one can build a better world.
    Dalai Lama>>
    Code Tags

  4. #4
    Join Date
    Aug 2016
    Location
    El Lago, Texas
    Beans
    475
    Distro
    Ubuntu Mate

    Re: Shrink swap file

    What I did was comment out the swap file line in fstab and rebooted.

    Swap now shows as zero.

    Is that method ok. ?
    Ubuntu Mate 18.04 Bionic
    Mate Desktop

  5. #5
    Join Date
    Aug 2016
    Location
    Wandering
    Beans
    Hidden!
    Distro
    Xubuntu Development Release

    Re: Shrink swap file

    If you have enough RAM.
    I often run with no swap enabled. Its just a choice only you can make with your system.
    With realization of one's own potential and self-confidence in one's ability, one can build a better world.
    Dalai Lama>>
    Code Tags

  6. #6
    Join Date
    Aug 2016
    Location
    El Lago, Texas
    Beans
    475
    Distro
    Ubuntu Mate

    Re: Shrink swap file

    Yes, I have 8 Gb.

    Never have come close to using more than a small percentage.
    Ubuntu Mate 18.04 Bionic
    Mate Desktop

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •