Swap priority gets set to -1 on each boot

20,936

Solution 1

Please follow my instructions that I have used and got a successful result.

  1. Power on the PC and log on to the desktop.
  2. Open a terminal and achieve root privilege. (sudo -s)
  3. Run fdisk -l to list disk partition table. Note your swap partition. Here your's /dev/sda7
  4. Run blkid /dev/sda7 to get the block id of the partition. Copy the UUID. The output is something like this

    /dev/sda7: UUID="918d334c-ca76-4e6a-b950-d44b4671dbc5" TYPE="swap" PARTUUID="7b892b40-0b"
    
  5. Run swapoff -a to off the swap partition.

  6. Run vim /etc/fstab. There, comment your current fstab entry (with #) and paste the following and ensure it with your UUID:

    UUID="918d334c-ca76-4e6a-b950-d44b4671dbc5"    none    swap    sw,pri=100    0    0 :wq 
    
  7. Save and exit

  8. Run swapon -a to enable swap partition
  9. Run swapon -s to display swap summary
  10. Reboot the pc and ensure again that your swap partition is enabled and with the same priority.

Solution 2

The swap partition is not being used ... What should I do to make the system use swap effectively?

The free command output shows that you have a 7.9GB swap partition set up and in use. The amount of space occupied at the time of that free command was 0 bytes. That is, the programs you were running at the time fitted into available memory and nothing needed to be paged out to the swap space. This is a desirable condition -- your system will run much faster when no swapping is occurring, vs when it is.

Regarding priority, -1 is an ok number. If you only have one swap device allocated, in general its priority makes no difference; when there's only one swap unit, that's the unit that will be used for swapping.

If you have multiple swap units and want to use one before another, either specify higher priority numbers (as described in previous answer and in man swapon) for units to be used sooner, and lower priority numbers for units to be used later, or in /etc/fstab just list the devices in order in the file. Earlier-listed means earlier-used for entries given without priority numbers.

Solution 3

for the answer to: how it's a negative priority, see the last point


If you have enough RAM, your system probably doesn't need the swap at the moment. There is not much to do, no options to change. You should only worry if you're getting out of space errors.
So the answer is; your settings look good, let it be.

There are few cases, where changing setting, like priority, from default (set at install time) might be useful. If you have multiple space volumes, and your fastest is of limited size. Or if you're occasionally out of memory, but it's not worth to make the main swap bigger. or a case where a NAS server (the one I have) has swap in raid1 mirrored to all disks, so any could be taken out on the run, but to not waste too much space the ultimate big swap is a file on just one selected volume.

but I actually wanted to add some side points:

  • /etc/fstab does not need to contain your swap volume setting. it might be set by a command in one of the init files.
  • cat /proc/swaps might be a better way to get the swap info, or swapon --summary which should show the same on some swapon versions

  • and for the dilemma of negative priority.
    as per manual: priority is a value between 0 and 32767.
    how come there are negative numbers in /proc/swaps?
    it just means, the swap was set without specifying an explicit priority

    If you don't add a priority yourself, the kernel generates a number for you. And those numbers are negative.

https://lists.debian.org/debian-user/2002/09/msg01075.html

your -1 priority is ok and because it's the only swap entry, the priority has no effect.

Share:
20,936

Related videos on Youtube

axolotl
Author by

axolotl

Updated on September 18, 2022

Comments

  • axolotl
    axolotl over 1 year

    I have a swap partition of size 7.9 GB. When I use the command free, it shows this:

    NAME      TYPE      SIZE USED PRIO
    /dev/sda7 partition 7.9G   0B   -1
    

    The swap partition is not being used and its priority is set to a negative value (which maybe why).

    I tried editing /etc/fstab to set priority to some positive integer, say 100. But this doesn't affect the current instance. So after rebooting I expect it to use the new config, but it resets the priority to original. The fstab file still shows p=100 but in reality it isn't.

    I tried turning swapoff, making swap again and swapon on the same partition, but no use.

    What should I do to make the system use swap effectively?

  • JackHasaKeyboard
    JackHasaKeyboard over 6 years
    Trailing :wq. It happens, haha.