LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-25-2019, 09:13 AM   #1
chrisguk
LQ Newbie
 
Registered: Aug 2014
Posts: 14

Rep: Reputation: Disabled
Reduce swap disk size


Hi,

I want to reduce my swap disk size without too much effort. Is there a way to do this on reboot with a script?

Thanks
 
Old 03-25-2019, 09:26 AM   #2
dc.901
Senior Member
 
Registered: Aug 2018
Location: Atlanta, GA - USA
Distribution: CentOS/RHEL, openSuSE/SLES, Ubuntu
Posts: 1,005

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
If you want to turn off swap completely, that can be done with command "swapoff". This does not require reboot, but swap will get turned back on after reboot. You can comment out the swap line in /etc/fstab if you do not want swap to get turned on after reboots.

To reduce swap partition, you will have to use parted/fdisk.

Last edited by dc.901; 03-25-2019 at 09:28 AM.
 
Old 03-25-2019, 09:28 AM   #3
chrisguk
LQ Newbie
 
Registered: Aug 2014
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by dc.901 View Post
If you want to turn off swap completely, that can be done with command "swapoff". This does not require reboot, but swap will get turned back on after reboot. You can comment out the swap line in /etc/fstab if you do not want swap to get turned on after reboots.
I dont want to turn it off complete I want to reduce the amount of swap being used for example from 90Gig to 6Gig. Without the system freezing up on me.
 
Old 03-25-2019, 10:14 AM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,699

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
What distribution/version are you running?

I think Ubuntu now creates/uses a swap file instead of a swap partition. Without freezing depends on what you have running on your system but you should be able to turn swap off (swapoff command). With little effort you can easily create a swap file and switch from default to a new swap file without rebooting.

If using a swap partition you can use mkswap specifying the size without having to first resize but take note of the warning. If you specify the wrong size you could overwrite data in the adjoining partition which could lead to data lose.

From the mkswap man page
Quote:
The size parameter is superfluous but retained for backwards compatibility. (It specifies
the desired size of the swap area in 1024-byte blocks. mkswap will use the entire
partition or file if it is omitted. Specifying it is unwise – a typo may destroy your
disk.)
 
Old 03-25-2019, 03:37 PM   #5
jefro
Moderator
 
Registered: Mar 2008
Posts: 21,978

Rep: Reputation: 3624Reputation: 3624Reputation: 3624Reputation: 3624Reputation: 3624Reputation: 3624Reputation: 3624Reputation: 3624Reputation: 3624Reputation: 3624Reputation: 3624
" Is there a way to do this on reboot with a script?" The simple answer is yes. I've never seen anyone create one but there are some super coders at LQ that could create one I suppose. Might be some web article on making one.

In general any command or set of commands can almost always be placed in a script. Once script may be used upon completion of some task to start a new task or write changes or whatnot.


Having too much swap isn't usually a terrible thing.
 
Old 03-25-2019, 03:40 PM   #6
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Quote:
Originally Posted by jefro View Post
Having too much swap isn't usually a terrible thing.
Having 90GiB of it, mentioned as an example by the OP above, might be a wee bit excessive though.
 
Old 03-25-2019, 09:24 PM   #7
jefro
Moderator
 
Registered: Mar 2008
Posts: 21,978

Rep: Reputation: 3624Reputation: 3624Reputation: 3624Reputation: 3624Reputation: 3624Reputation: 3624Reputation: 3624Reputation: 3624Reputation: 3624Reputation: 3624Reputation: 3624
Doooh! That is a bit much.

I've never tried this but I suppose one could create a new swap (file or partition), mount it then change swappiness to force new to basically take control then remove/reduce old one. I think there is some flush swap command that would insure that the old swap is almost or totally empty. Silly swap is an odd deal sometimes. Some bits get put there seemingly no matter how much ram you have.

One could use a live cd to change it or maybe single user mode to fix all of this.

Some distro's have installers or tools to do this task. I get the feeling I have tried this on Opensuse maybe or red hat.
 
Old 03-25-2019, 09:31 PM   #8
frankbell
LQ Guru
 
Registered: Jan 2006
Location: Virginia, USA
Distribution: Slackware, Ubuntu MATE, Mageia, and whatever VMs I happen to be playing with
Posts: 19,321
Blog Entries: 28

Rep: Reputation: 6141Reputation: 6141Reputation: 6141Reputation: 6141Reputation: 6141Reputation: 6141Reputation: 6141Reputation: 6141Reputation: 6141Reputation: 6141Reputation: 6141
If it were me, I'd boot to a Live CD of something (likely GPartED) and shrink the swap partition--after backing stuff up to external media, because, when you muck about with partitions, you never can predict . . . .

In these days of multi-GBs of RAM, 90GB is a bit high for a single user machine (I can't speak to a server in a thin-client environment--that's a whole nother horse). I've got 16GB RAM in this box and 4 GB swap, and swap is hardly ever touched.
 
Old 03-26-2019, 09:50 AM   #9
fatmac
LQ Guru
 
Registered: Sep 2011
Location: Upper Hale, Surrey/Hants Border, UK
Distribution: Mainly Devuan, antiX, & Void, with Tiny Core, Fatdog, & BSD thrown in.
Posts: 5,487

Rep: Reputation: Disabled
Backing up your data is always sensible before messing with filesystems.

What you need to do is first unmount your present swap partition, using
Code:
sudo swapoff /dev?
(enter your partition number).

Then using fdisk or similar program, alter the size of your swap partition on your disk, write it out to disk, then run
Code:
sudo mkswap /dev?
(your partition number), then
Code:
sudo swapon /dev?
(your partition number).

That should have you back up & running with your newly sized swap partition.

The free space created can then be merged into a present partition, (possible risk of data loss), or, easiest option, just create a new partition, & put a file system on it, which can then be added to your /etc/fstab file.

Last edited by fatmac; 03-26-2019 at 09:53 AM.
 
Old 03-29-2019, 04:47 PM   #10
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,800

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by frankbell View Post
I've got 16GB RAM in this box and 4 GB swap, and swap is hardly ever touched.
May depend a lot on what you're doing on the system. Here I have 8GB of physical memory and 4X that in swap space and swap can still get hammered at times. Not frequently but I'd rather have the swap space than receive an "uh uh... not enough memory" message. Disk space is cheap nowadays.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] how to resize/ shrink disk size the KVM disk size in proxmox? arsyam Linux - Server 1 01-06-2018 05:45 AM
How to reduce linux disk size before virtualizing it? ppmoore Linux - Newbie 5 03-21-2017 01:21 AM
any ideas to reduce log file size or make log file size managed? George2 Programming 2 08-13-2006 06:55 AM
Why should you give the size of the swap disk as twice the size of RAM vmniza Linux - Newbie 3 09-17-2004 02:13 AM
Knoppix Hdd installation -I want to reduce the size on Disk!! rayesteq Linux - General 2 09-13-2004 05:02 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration