How can I clear my SWAP?

9,359

Have you tried turning it off and on again?

As funny as it may seem. You can forcefully clear your swap by turning swap off and on again:

sudo swapoff -a
sudo swapon -a

This will mark all swap partitions as unused by swap (then re-marking them used). Note that this will force all swap memory into physical memory - meaning some bad things might happen if you don't have enough physical memory.

If you're using swap up, you might have a memory leak as someone else has suggested.

Share:
9,359

Related videos on Youtube

Badum
Author by

Badum

Updated on September 18, 2022

Comments

  • Badum
    Badum over 1 year

    I can't clean SWAP memory. It cause that my Linux lag. I try clean but I got an output: you just be superuser.

    I using jupyter notebook to learning convolution neural network. At the end of each epoch my SWAP is increasing.

    • goo
      goo about 5 years
      Swap space is used for paging out memory that has been allocated by a program, but is not in current use. Swap is not slowing you down, but it looks like there's a memory leak in "learning convolution neural network".
    • Badum
      Badum about 5 years
      What can I do? Switch off SWAP?
    • user68186
      user68186 about 5 years
      Which version of Ubuntu are you using? How much RAM does your computer have?
    • Soren A
      Soren A about 5 years
      If swap is slowing your system down, it is because you are using more RAM than are available, Either reduce the usage or buy more memory.
    • Badum
      Badum about 5 years
      Ubuntu 18.04 16GB RAM
  • AlwaysTalkingAboutMyDog
    AlwaysTalkingAboutMyDog about 5 years
    You should explain the commands in your answer (incase the link dies).
  • Badum
    Badum about 5 years
    YES. I tried it but I can't do this, because I have full memory and I can't use cammand swapoff.
  • Badum
    Badum about 5 years
    I closed all programs and I think I have a lot of physical memory. 16GB.
  • marosg
    marosg about 5 years
    We really cannot tell you more without more details, it may be perfectly ok what is happening in your system and also there may be a problem. Check the memory status before you start with free program, then do the same during the run, then during the peak and then after it finishes peak. Post it here and maybe we can find what is going on
  • AlwaysTalkingAboutMyDog
    AlwaysTalkingAboutMyDog about 5 years
    @Badum then you are clearly lagging because your applications are using up too much memory. I'd ask another question about your memory usage problems (as opposed to unable to clear swap) and post some details about your system, how much ram you have, and what programs you are running.